Skip to content

Instantly share code, notes, and snippets.

@Nadav-Ruskin
Forked from abayer/jenkins-git-backup.sh
Created October 30, 2017 08:01
Show Gist options
  • Save Nadav-Ruskin/27d8d7e8921ce4352890fa7117531fdb to your computer and use it in GitHub Desktop.
Save Nadav-Ruskin/27d8d7e8921ce4352890fa7117531fdb to your computer and use it in GitHub Desktop.
Example of a script for backing up Jenkins config in git.
# This script assumes you have a repository that contains a directory called jenkins_home.
# It goes to a specified backup workspace, checks the repository from github, copies only wanted files from jenkins_home,
# copies all files in the repository's root (probably scripts), commits only existing files, pushes the changes, and removes the backup directory.
# Set this script up to run automatically as follows:
# sudo crontab -e
# 29 0 * * * ">>insert the script's location, perferrably it's inside REPOSITORY_HOME<<"
set -ex
git config --global user.name ">>insert bot's user name<<"
git config --global user.email ">>insert bot's email in quotation marks<<"
REPOSITORY_HOME=">>insert local repository path here<<"
GIT_REMOTE_URL=">>insert git url here<<"
REPOSITORY_NAME=">>insert repository name here<<"
cd ">>insert some place you have access to here<<"
rm -rf backup
mkdir backup
cd backup
git clone --depth 1 ${GIT_REMOTE_URL}
cd ${REPOSITORY_NAME}/jenkins_home
rm -rf *
rm -rf .[^.] .??*
rsync -av --delete \
--exclude="jobConfigHistory" \
--exclude="war" \
--exclude="config-history" \
--exclude=".hudson" \
--exclude=".ivy2" \
--exclude=".m2" \
--exclude="lost+found" \
--exclude="/plugins" \
--exclude="/jobs/*/builds" \
--include="*/" \
--include="*config.xml" \
--include="/jobs/*/config.xml" \
--include="/jobs/*/nextBuildNumber" \
--include="/users/*" \
--include="*.hpi" \
--include="*.jpi" \
--include="*pinned" \
--include="*disabled" \
--include="/scriptler" \
--include="/secrets" \
--include="*.xml" \
--include="*.key" \
--include=".ssh/*" \
--exclude="*" \
--prune-empty-dirs ${REPOSITORY_HOME}/jenkins_home/ .
cd ..
rsync -av \
--exclude=".git" \
--exclude="jenkins_home" \
--prune-empty-dirs ${REPOSITORY_HOME}/ .
git add -A
git commit -m "Jenkins JENKINS_MASTER config backup for $(date)"
git push origin master
cd ../..
rm -rf backup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment