Skip to content

Instantly share code, notes, and snippets.

@zenoando
Forked from abayer/jenkins-git-backup.sh
Last active November 24, 2015 13:48
Show Gist options
  • Save zenoando/b33ce2ae85c30212d5b9 to your computer and use it in GitHub Desktop.
Save zenoando/b33ce2ae85c30212d5b9 to your computer and use it in GitHub Desktop.
Example of a script for backing up Jenkins config in git.
#!/bin/bash -ex
#
# Copies certain kinds of known files and directories from a given Jenkins master directory
# into a git repo, removing any old ones, adds 'em, commits 'em, pushes 'em.
#
if [ $# -ne 2 ]; then
echo usage: $0 jenkins_root_dir jenkins_master
exit 1
fi
ROOT_DIR=/var/backup/jenkins
JENKINS_MASTER=ctrlaltreboot
GIT_SERVER=
ORIG_DIR=$PWD
mkdir -p $ROOT_DIR && cd $ROOT_DIR
rm -rf *-jenkins-git
git clone git@${GIT_SERVER}:${JENKINS_MASTER}-jenkins-git.git
cd $ROOT_DIR/${JENKINS_MASTER}-jenkins-git
rsync-nv() {
IGNOREEXIT=24
IGNOREOUT='^(file has vanished: |rsync warning: some files vanished before they could be transferred)'
set -o pipefail
rsync "${@}" 2>&1 | (egrep -v "$IGNOREOUT" || true)
ret=$?
if [[ $ret == $IGNOREEXIT ]]; then
ret=0
fi
exit $ret
}
# rsync-nv (rsync-no-vanished) is just a wrapper making sure rsync doesn't fail on vanished files during the process.
# Jenkins master is in a subdirectory of /var/lib/jenkins.
rsync-nv -av --delete --exclude="jobConfigHistory" \
--exclude="war" \
--exclude="config-history" \
--exclude=".hudson" \
--exclude=".ivy2" \
--exclude=".m2" \
--exclude="lost+found" \
--include="*/" \
--include="*config.xml" \
--include="users/*" \
--include="*.hpi" \
--include="*.jpi" \
--include="*pinned" \
--include="*disabled" \
--exclude="*" \
--prune-empty-dirs /var/lib/jenkins/${JENKINS_MASTER}/ .
[[ -d /var/lib/jenkins/${JENKINS_MASTER}/scriptler ]] && rsync-nv -av --delete /var/lib/jenkins/${JENKINS_MASTER}/scriptler .
[[ -d /var/lib/jenkins/${JENKINS_MASTER}/secrets ]] && rsync-nv -av --delete /var/lib/jenkins/${JENKINS_MASTER}/secrets .
cp /var/lib/jenkins/${JENKINS_MASTER}/*.xml .
cp /var/lib/jenkins/${JENKINS_MASTER}/*.key .
git add -A
git commit -m "Jenkins ${JENKINS_MASTER} config backup for $(date)"
git push origin master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment