Skip to content

Instantly share code, notes, and snippets.

@AndrewFarley
Created September 5, 2018 11:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndrewFarley/5bc575120a79d1277800c6e10f37e1a9 to your computer and use it in GitHub Desktop.
Save AndrewFarley/5bc575120a79d1277800c6e10f37e1a9 to your computer and use it in GitHub Desktop.
A Shell Script to Backup Gitlab to S3 (with slack notifications)
#!/bin/bash
LOGFILE=/tmp/backup-gitlab-to-s3.log
GITLAB_BACKUP_FOLDER=/var/opt/gitlab/backups
S3_FILE_PREFIX=gitlab
S3_BUCKET_PATH=bucket-name-goes-here/gitlab-rotating-backups
SLACK_USERNAME="Backup Gitlab Daily - `hostname`"
SLACK_CHANNEL="#farley-testing"
# SLACK_CHANNEL="#monitoring"
SLACK_ICON="https://s3.amazonaws.com/kudelabs-archives/harddrive256.png"
BACKUPFILE="this-file-doesnt-exist"
function bytes_for_humans {
local -i bytes=$1;
if [[ $bytes -lt 1048576 ]]; then
echo "$(( (bytes + 1023)/1024 ))KB"
else
echo "$(( (bytes + 1048575)/1048576 ))MB"
fi
}
/usr/local/bin/send_notification_via_slack.py -i "$SLACK_ICON" -u "$SLACK_USERNAME" -c "$SLACK_CHANNEL" -s "ok" "Initializing daily offsite backup..."
# Sleep to avoid spamming slack, just incase this executes too fast...
sleep 2
echo "Backing up gitlab, with log output located at $LOGFILE"
/usr/bin/gitlab-rake gitlab:backup:create > $LOGFILE
if [ ! -s "$LOGFILE" ]; then
echo "Error, the log file doesn't exist..."
else
BACKUPFILE=`cat $LOGFILE | grep "Creating backup archive: " | grep "... done" | awk '{ print $4 }'`
echo "Received backup file $GITLAB_BACKUP_FOLDER/$BACKUPFILE , checking..."
fi
if [ -s "$GITLAB_BACKUP_FOLDER/$BACKUPFILE" ]; then
echo "Backup file appears okay, appending gitlab and gitlab runner configs..."
cd /etc/
tar -czf /tmp/gitlab-config.tar.gz gitlab gitlab-runner
cd /tmp/
tar --append --file=$GITLAB_BACKUP_FOLDER/$BACKUPFILE gitlab-config.tar.gz
rm -f /tmp/gitlab-config.tar.gz
BACKUP_FILESIZE=`stat --printf="%s" "$GITLAB_BACKUP_FOLDER/$BACKUPFILE"`
BACKUP_FILESIZE_HUMAN=`bytes_for_humans $BACKUP_FILESIZE`
echo "File is size: $BACKUP_FILESIZE_HUMAN"
echo "Uploading to s3..."
aws s3 cp $GITLAB_BACKUP_FOLDER/$BACKUPFILE s3://$S3_BUCKET_PATH/${S3_FILE_PREFIX}_$BACKUPFILE
if [ $? -eq 0 ]; then
echo "Okay, backed up gitlab to S3!"
echo "Removing backup file: $GITLAB_BACKUP_FOLDER/$BACKUPFILE"
/usr/local/bin/send_notification_via_slack.py -i "$SLACK_ICON" -u "$SLACK_USERNAME" -c "$SLACK_CHANNEL" -s "ok" "Successfully backed up $BACKUP_FILESIZE_HUMAN to s3://$S3_BUCKET_PATH/${S3_FILE_PREFIX}_$BACKUPFILE"
rm -f "$GITLAB_BACKUP_FOLDER/$BACKUPFILE"
exit 0
fi
fi
/usr/local/bin/send_notification_via_slack.py -u "$SLACK_USERNAME" -c "$SLACK_CHANNEL" -s "error" "ERROR: <!here|here> <@farley> We were unable to upload the $BACKUP_FILESIZE_HUMAN file from $GITLAB_BACKUP_FOLDER/$BACKUPFILE into S3, please fix me"
echo "Error: We were unable to upload the $BACKUP_FILESIZE_HUMAN file from $GITLAB_BACKUP_FOLDER/$BACKUPFILE into S3, please fix me"
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment