Last active
December 14, 2015 07:37
-
-
Save miklund/0d94b4a9c772a94c4e7b to your computer and use it in GitHub Desktop.
2015-12-12 Backup database in a rails docker setup
This file contains hidden or 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
# Backup database in a rails docker setup | |
# http://blog.mikaellundin.name/2015/12/12/backup-database-in-a-rails-docker-setup.html |
This file contains hidden or 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 | |
# Set environment variables | |
export DATABASE_URL=postgres://postgres:<obfuscated>@postgres:5432/r3pl4y_production | |
export AWS_ACCESS_KEY_ID=<obfuscated> | |
export AWS_SECRET_ACCESS_KEY=<obfuscated> | |
export AWS_DEFAULT_REGION=eu-west-1 | |
echo $(date +"%Y-%m-%d %T"): START r3pl4y database backup | |
# get a dump of database | |
echo $(date +"%Y-%m-%d %T"): Dump the database | |
pg_dump --dbname=$DATABASE_URL | gzip > /var/backups/r3pl4y_production-$(date +%Y%m%d).psql.gz | |
# remove all but 5 latest backups | |
echo $(date +"%Y-%m-%d %T"): Remove old backups | |
ls -1trd /var/backups/* | head -n -5 | xargs rm -f | |
# synchronize folder to s3 backup bucket | |
echo $(date +"%Y-%m-%d %T"): Push new backup to Amazon S3 | |
aws s3 sync /var/backups s3://replay-files.mikaellundin.name/backup | |
echo $(date +"%Y-%m-%d %T"): END r3pl4y database backup |
This file contains hidden or 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
# replay-backup | |
FROM zsoltm/debian-armhf:jessie | |
MAINTAINER Mikael Lundin <[email protected]> | |
# Install prerequisites | |
RUN apt-get update && apt-get install -y cron awscli postgresql-client-9.4 | |
# ADD backup script to container | |
ADD backup-replay-database.sh /backup-replay-database.sh | |
ADD backup-replay-cron /etc/cron.d/backup-replay | |
# Make script files executable | |
RUN chmod 755 /backup-replay-database.sh | |
RUN chmod 755 /etc/cron.d/backup-replay | |
# VOLUME where the backups reside | |
VOLUME /var/backups | |
VOLUME /var/log/cron | |
# start the application | |
CMD ["cron", "-f"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment