Skip to content

Instantly share code, notes, and snippets.

@jayantnd
Created December 19, 2021 01:47
Show Gist options
  • Save jayantnd/d7acc0b1536fd70bb838e9612b75dbb0 to your computer and use it in GitHub Desktop.
Save jayantnd/d7acc0b1536fd70bb838e9612b75dbb0 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -ex
if [ $# -ne 3 ]; then
echo usage: $0 jenkins_home git_repos_url backup_folder
exit 1
fi
# Jenkins home folder
JENKINS_HOME=$1
# git url when the jenkins backup will checkin
GIT_REMOTE_URL=$2
# folder name where backup will be stored
BACKUP_FOLDER=$3
# git repo name
GIT_REPOS_NAME=$(basename ${GIT_REMOTE_URL} .git)
if [[ ! -e ${GIT_REPOS_NAME} ]]; then
# if folder not existed then clone it
git clone ${GIT_REMOTE_URL}
elif [[ ! -d ${GIT_REPOS_NAME} ]]; then
# if folder name exists but not a folder, delete the file then clone it
rm ${GIT_REPOS_NAME}
git clone ${GIT_REMOTE_URL}
else
cd ${GIT_REPOS_NAME}
git pull --rebase
cd ..
fi
mkdir -p "${GIT_REPOS_NAME}/${BACKUP_FOLDER}"
cd "${GIT_REPOS_NAME}/${BACKUP_FOLDER}"
rsync -av --delete \
--exclude="jobConfigHistory" \
--exclude="war" \
--exclude="config-history" \
--exclude=".hudson" \
--exclude=".ivy2" \
--exclude=".m2" \
--exclude="lost+found" \
--exclude="fingerprints" \
--exclude="jobs/*/*/*/builds" \
--exclude="log" \
--exclude="logs" \
--exclude="users" \
--exclude="workspace" \
--prune-empty-dirs \
${JENKINS_HOME}/ .
git add -A
git commit -m "Jenkins ${JENKINS_MASTER} config backup for $(date)"
git push origin master
## example to use in build execute shell
# set -ex
# $JENKINS_HOME/jenkins-git-backup.sh $JENKINS_HOME git-repo-url jenkins-aws-mac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment