Commit 248290af authored by Afraz Ahmadzadeh's avatar Afraz Ahmadzadeh

Initial commit

parents
# Gitlab Backups
Simple cron and script to enable backups to S3
## Prereqs
This script presumes:
1. An S3 backup bucket, the name of which is stored in an SSM Parameter called "S3_BACKUP_BUCKET"
2. Basic packages present in our (yet to be release!) base image:
a. jq
b. Python 3 (and set as default Python)
c. awscli (and therefore Pip)
3. rw permissions to the backup bucket
4. r permission to SSM Parameters (or at least the "S3_BACKUP_BUCKET" parameter)
#!/bin/bash
LOGFILE=/tmp/backup-gitlab-to-s3.log
GITLAB_BACKUP_FOLDER=/var/opt/gitlab/backups
S3_FILE_PREFIX=gitlab
REGION=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone)
REGION=${REGION::-1}
S3_BUCKET=$(aws --region $REGION ssm get-parameters --name "S3_BACKUP_BUCKET" | jq -r .Parameters[0].Value)
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
}
echo "Backing up gitlab, with log output located at $LOGFILE"
/usr/bin/gitlab-rake gitlab:backup:create STRATEGY=copy > $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/${S3_FILE_PREFIX}_$BACKUPFILE
if [ $? -eq 0 ]; then
echo "Okay, backed up gitlab to S3!"
echo "Removing backup file: $GITLAB_BACKUP_FOLDER/$BACKUPFILE"
rm -f "$GITLAB_BACKUP_FOLDER/$BACKUPFILE"
exit 0
fi
fi
echo "Error: We were unable to upload the $BACKUP_FILESIZE_HUMAN file from $GITLAB_BACKUP_FOLDER/$BACKUPFILE into S3, please fix me"
exit 1
10 0 * * * root /usr/local/bin/backup-gitlab-to-s3.sh > /var/log/backup-gitlab-to-s3.sh 2>&1
#!/bin/bash
# Install awscli
update-alternatives --install /usr/bin/python python /usr/bin/python3 1
curl https://bootstrap.pypa.io/get-pip.py -o /tmp/get-pip.py
python /tmp/get-pip.py
pip install --upgrade awscli
apt-get update
apt-get install -y jq
cd /tmp/gitlab-backups
cp backup-gitlab-to-s3.sh /var/log/backup-gitlab-to-s3.sh
chmod +x /var/log/backup-gitlab-to-s3.sh
cp gitlab-backups.cron /etc/cron.d/gitlab-backups
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment