boriszweimueller
8/5/2015 - 10:23 PM

sql dump inside docker and upload to s3

sql dump inside docker and upload to s3

#!/bin/sh

# This script makes a backup of the configured MySQL docker container and pushes the backup to Amazon S3.
# Important: Make sure, awscli is installed and configured correctly!

# The final name of the backup: db_backup_YYMMDD-HHMM.sql
FILENAME=db_backup_$(date +"%Y%m%d-%H%M").sql

# The name of the mysql container we want to back up.
# This name is used to link against the database container.
DB_CONTAINER=mysql-demo-server:mysql-demo-server

DB_HOST=mysql-demo-server
DB_PORT=3306
DB_PASS=rootpw
DATABASE=database_to_export

COMMAND='mysqldump -P'$DB_PORT' -h'$DB_HOST' -p'$DB_PASS' '$DATABASE' > backup/'$FILENAME''

AWS_BUCKET=demo-db-backup

# Do the backup and copy it to the host
sudo docker run --rm --link $DB_CONTAINER -v $(pwd):/backup mysql sh -c "$COMMAND"

# Save the backup on S3
aws s3 cp $FILENAME s3://$AWS_BUCKET/$FILENAME
aws s3 ls s3://$AWS_BUCKET