Skip to content

Instantly share code, notes, and snippets.

@abayer
Last active January 8, 2023 09:19
Show Gist options
  • Star 71 You must be signed in to star a gist
  • Fork 35 You must be signed in to fork a gist
  • Save abayer/527063a4519f205efc74 to your computer and use it in GitHub Desktop.
Save abayer/527063a4519f205efc74 to your computer and use it in GitHub Desktop.
Example of a script for backing up Jenkins config in git.
#!/bin/bash
#
# 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
fi
ROOT_DIR=$1
JENKINS_MASTER=$2
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-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
@JayTeeSF
Copy link

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)?

@choldrim
Copy link

eh, I don't think it is good enough, so I fork it to my repos, and improve it plus my mind.
https://gist.github.com/choldrim/b0862264bda0f6a2e4ae

@auwing
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?

@jayantnd
Copy link

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
https://gist.github.com/jayantnd/d7acc0b1536fd70bb838e9612b75dbb0

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