Created
May 3, 2022 22:00
-
-
Save rafalsep/804aee1998f2e759a9272e2eb09384e2 to your computer and use it in GitHub Desktop.
Jenkins DSL script that will add backup and restore jobs for Jenkins. Backups all configs and jobs to Google Cloud Storage. <<BUCKET_NAME>> needs to be replaced with actual bucket name.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
job('jenkins-backup') { | |
logRotator(30, -1, 30, -1) | |
label('master') | |
steps { | |
shell(""" | |
#!/bin/bash | |
cd \$JENKINS_HOME | |
NOW_DATE=\$(date +%Y-%m-%d) | |
gsutil -q -m rsync -C -r userContent gs://<<BUCKET_NAME>>/\$NOW_DATE/userContent || true | |
gsutil -q -m rsync -C -r -x "(?!^.*\\.jpi\$)" plugins gs://<<BUCKET_NAME>>/\$NOW_DATE/plugins || true | |
gsutil -q rsync -C -x "(?!^secret\\.key\$)" . gs://<<BUCKET_NAME>>/\$NOW_DATE || true | |
gsutil -q rsync -C -r secrets gs://<<BUCKET_NAME>>/\$NOW_DATE/secrets || true | |
gsutil -q -m rsync -C -r users gs://<<BUCKET_NAME>>/\$NOW_DATE/users || true | |
gsutil -q -m rsync -C -x "(?!^.*\\.xml\$)" . gs://<<BUCKET_NAME>>/\$NOW_DATE || true | |
gsutil -q -m rsync -C -r -x ".*htmlreports.*|.*archive.*" jobs gs://<<BUCKET_NAME>>/\$NOW_DATE/jobs || true | |
""") | |
} | |
properties { | |
disableConcurrentBuilds { | |
abortPrevious(true) | |
} | |
} | |
triggers { | |
cron('@daily') | |
} | |
} | |
job('jenkins-restore') { | |
logRotator(30, -1, 30, -1) | |
label('master') | |
description('This job is used to restore last backup of jenkins master') | |
steps { | |
shell(""" | |
#!/bin/bash | |
cd \$JENKINS_HOME | |
LATEST_BACKUP_FOLDER_NAME=\$(gsutil ls gs://<<BUCKET_NAME>>/ | grep -e "/\$" | sort | tail -n 1 | grep -o -E '[0-9]+-[0-9]+-[0-9]+') | |
gsutil -m cp -r gs://<<BUCKET_NAME>>/\$LATEST_BACKUP_FOLDER_NAME . | |
""") | |
} | |
properties { | |
disableConcurrentBuilds { | |
abortPrevious(true) | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment