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