-
-
Save mshahomar/17ed30aa2c8f7dc71417a8c60985b78c to your computer and use it in GitHub Desktop.
Gitlab Backup to S3
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
#!/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/folder-here | |
SLACK_USERNAME="Backup Gitlab Daily - `hostname`" | |
SLACK_CHANNEL="#od-infra-monitoring" | |
SLACK_ICON="https://s3.amazonaws.com/kudelabs-archives/harddrive256.png" | |
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 | |
BACKUPFILE=`cat $LOGFILE | grep "Creating backup archive: " | grep "... done" | awk '{ print $4 }'` | |
echo "Received backup file $GITLAB_BACKUP_FOLDER/$BACKUPFILE , checking..." | |
if [ -s "$GITLAB_BACKUP_FOLDER/$BACKUPFILE" ]; then | |
echo "Backup file appears okay" | |
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..." | |
/usr/local/bin/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: <@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