Skip to content

Instantly share code, notes, and snippets.

@mrrooijen
Last active July 15, 2016 14:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mrrooijen/1d1c893b9d91f7e31f00ee87f3d6953f to your computer and use it in GitHub Desktop.
Save mrrooijen/1d1c893b9d91f7e31f00ee87f3d6953f to your computer and use it in GitHub Desktop.
Dumps, compresses, encrypts, transfers, and rotates Rancher backups to/on Amazon S3.
#! /bin/bash
# Dumps, compresses, encrypts, transfers, and rotates Rancher backups to/on Amazon S3.
# Requirements:
#
# 1. Docker
#
# $ curl -fsSL https://get.docker.com/ | sh
#
# 2. Rancher
#
# $ docker run -d \
# --name=rancher \
# --restart=always \
# -v /var/lib/mysql:/var/lib/mysql \
# -p 8080:8080 \
# rancher/server:stable
#
# 3. s3cmd
#
# $ apt-get install python-pip
# $ pip install s3cmd
# $ s3cmd --configure
#
# 3. gpg public key
# Installation:
#
# 1. Place this script in
#
# /usr/local/bin/backup
#
# 2. Make this script executable
#
# $ chmod a+x /usr/local/bin/backup
#
# 3. Optional: Add to crontab (`0 0 * * * /usr/local/bin/backup`)
#
# $ crontab -e
# Usage:
#
# $ /usr/local/bin/backup (or just `backup` if in $PATH)
# Usage (Restore):
#
# $ docker exec -i rancher mysql < 123456.dump
# $ docker restart rancher
### BEGIN CONFIGURATION
bucket=my-s3-bucket
bucket_path=s3-bucket-path
gpg_public_key=me@example.com
keep=7
### END CONFIGURATION
export PATH=/usr/local/bin:$PATH
container=rancher
backup_path=/var/backups/rancher
backup_file=`date +%s`.dump.gz.gpg
mkdir -p $backup_path
echo "Dumping, compressing, encrypting and transfering database to S3"
docker exec -i $container mysqldump -A \
| gzip \
| gpg -e -r $gpg_public_key \
| s3cmd put - s3://$bucket/$bucket_path/$backup_file
echo "Rotating backups on S3 (keep $keep latest backups)"
s3cmd ls s3://$bucket/$bucket_path/ \
| grep -e gpg$ \
| awk '{print $4}' \
| head -n -$keep \
| xargs s3cmd del
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment