Last active
June 14, 2021 09:20
-
-
Save ChrisWelsh-mis/c55cf5fe207b16f35bf779102b31485d to your computer and use it in GitHub Desktop.
Backup Jenkins thinBackup to S3
This file contains hidden or 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
#!/bin/bash | |
# lock script to prevent parallel running | |
scriptname=$(basename "$0") | |
pidfile="/tmp/${scriptname}-lock.pid" | |
exec 200>"$pidfile" | |
flock -n 200 || exit 1 | |
pid=$$ | |
echo $pid 1>&200 | |
# error handling | |
function error_handler() { | |
echo "Error occurred in script at line: ${1}" | |
echo "Line exited with status: ${2}" | |
} | |
trap 'error_handler ${LINENO} $?' ERR | |
set -o errexit | |
set -o errtrace | |
set -o nounset | |
# set backups variable | |
NOW=$(date +%F-%a) # year-month-date-day format | |
BACKUPDIR="/var/lib/jenkins/backups" | |
JENKINS_TAR="jenkins-$NOW.tar.bz2" | |
S3_BUCKET="s3://<BUCKET_NAME>/jenkins/" # where <BUCKET_NAME> is your S3 bucket name | |
# check for backup directory | |
if [[ ! -d "$BACKUPDIR" ]] | |
then | |
mkdir -p "$BACKUPDIR" | |
fi | |
if ls "$BACKUPDIR"/FULL-* &> /dev/null; then | |
# archive backup | |
cd "$BACKUPDIR" | |
tar -cjf "$JENKINS_TAR" FULL-* | |
# copy to S3 bucket | |
aws s3 cp "$BACKUPDIR"/"$JENKINS_TAR" "$S3_BUCKET" | |
# cleanup disk space | |
rm -rf "${BACKUPDIR:?}"/* | |
else | |
clear | |
echo "Backup from thinBackup on Jenkins not present. Please login to Jenkins and run manually..." | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment