-
-
Save thiagozs/f4e439a6d66e614d1de894ac6aea7ac9 to your computer and use it in GitHub Desktop.
Dumps, compresses, encrypts, transfers, and rotates Rancher backups to/on Amazon 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 | |
# 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 | |
# | |
# 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