Skip to content

Instantly share code, notes, and snippets.

@choldrim
Forked from abayer/jenkins-git-backup.sh
Last active January 31, 2024 13:50
Show Gist options
  • Save choldrim/b0862264bda0f6a2e4ae to your computer and use it in GitHub Desktop.
Save choldrim/b0862264bda0f6a2e4ae 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 3 ]; then
echo usage: $0 jenkins_home git_repos_url git_repos_name
exit 1
fi
JENKINS_HOME=$1
GIT_REMOTE_URL=$2
GIT_REPOS_NAME=$3
rm -rf ${GIT_REPOS_NAME}
git clone ${GIT_REMOTE_URL}
cd ${GIT_REPOS_NAME}
rsync -av --delete --exclude="jobConfigHistory" \
--exclude="war" \
--exclude="config-history" \
--exclude=".hudson" \
--exclude=".ivy2" \
--exclude=".m2" \
--exclude="lost+found" \
--include="*config.xml" \
--include="jobs/*/builds/*/log" \
--include="users/*" \
--include="*.hpi" \
--include="*.jpi" \
--include="*pinned" \
--include="*disabled" \
--include="scriptler/*" \
--include="secrets/*" \
--include="*.xml" \
--include="*.key" \
--exclude="*" \
--prune-empty-dirs ${JENKINS_HOME}/ .
git add -A
git commit -m "Jenkins ${JENKINS_MASTER} config backup for $(date)"
git push origin master
## example
# ./jenkins-git-backup.sh [email protected]:/var/lib/jenkins [email protected]:choldrim/deepin-jenkins-git.git deepin-jenkins-git
@Lake-end
Copy link

rm -rf ${GIT_REPOS_NAME}
You are one messed up paramater away from deleting your Jenkins_home

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