Skip to content

Instantly share code, notes, and snippets.

@hayderimran7
Last active June 15, 2018 10:42
Show Gist options
  • Save hayderimran7/621e26612fd850221f34871b1724e39f to your computer and use it in GitHub Desktop.
Save hayderimran7/621e26612fd850221f34871b1724e39f to your computer and use it in GitHub Desktop.
Jenkins backup using a job

Jenkins backup using a Jenkins job.

The following script was taken from https://cloud.google.com/solutions/images/auto-customize-back-script.png which was mentioned in this article https://cloud.google.com/solutions/automated-build-images-with-jenkins-kubernetes .

It creates a tar of jenkins master important files as backup.
It must be run on jenkins master where $JENKINS_HOME is set and points to Jenkins home directory.

rm -rf *
mkdir -p $BUILD_ID/jobs
cp $JENKINS_HOME/*.xml $BUILD_ID/

# secrets
cp $JENKINS_HOME/*.key $BUILD_ID/ 2>/dev/null || :
cp $JENKINS_HOME/*.key.* $BUILD_ID/ 2>/dev/null || :
cp $JENKINS_HOME/secrets $BUILD_ID/ 2>/dev/null || :

#Users
cp $JENKINS_HOME/users $BUILD_ID/ 2>/dev/null || :

#Jobs and NO history
rsync -am --include="config.xml" \
  --include="*/" \
  --exclude='*' \
  --prune-empty-dirs \
  $JENKINS_HOME/jobs/ $BUILD_ID/jobs/

# archive and clean
tar cvf $BUILD_ID.tar.gz $BUILD_ID/
cp $BUILD_ID.tar.gz LATEST.tar.gz
rm -rf $BUILD_ID
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment