Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Example of a script for backing up Jenkins config in git.
# 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.
set -ex
if [ $# -ne 2 ]; then
echo usage: $0 root_dir jenkins_master
exit 1
mkdir -p $ROOT_DIR
rm -rf *-jenkins-git
git clone git@GIT_SERVER:${JENKINS_MASTER}-jenkins-git.git
cd $ROOT_DIR/${JENKINS_MASTER}-jenkins-git
# 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.
$ORIG_DIR/rsync-no-vanished -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 ] && $ORIG_DIR/rsync-no-vanished -av --delete /var/lib/jenkins/${JENKINS_MASTER}/scriptler .
[ -d /var/lib/jenkins/${JENKINS_MASTER}/secrets ] && $ORIG_DIR/rsync-no-vanished -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
Copy link

ericlong commented Jun 26, 2014

Thanks for sharing. You just saved me a lot of time.

Copy link

JayTeeSF commented Jan 28, 2015

First (and foremost) Thanks for this.

I realize it is an old post, so I'll completely understand if you don't reply. Nevertheless, I would appreciate:

  1. confirmation that root_dir is just any (empty) directory where I choose to store the backup repository that this script creates for my server's jenkins config

  2. for a non-standard jenkins install (i.e. there's no /var/lib/jenkins on my jenkins server), how could I determine the equivalent of jerkins_master? Is there a particular top-level directory entry I can look for? (i.e. is it most-likely the directory containing my jenkins.war?)

and, last, but not least:
3) what steps do you follow to restore jenkins (i.e. if/when your ec2 instance dies)?

Copy link

choldrim commented Dec 16, 2015

eh, I don't think it is good enough, so I fork it to my repos, and improve it plus my mind.

Copy link

auwing commented Jan 10, 2018

Thanks for all your sharing. Wonder if there is a way to backup a specific plan/project and how to retrieve it?

Copy link

jayantnd commented Dec 19, 2021

eh, I don't think it is good enough, so I fork it to my repos, and improve it plus my mind. 😉

just kidding, this is perfect, I have just modified to work on my Jenkins on Mac. and all credit to this repo owner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment