Backup DB Server to S3 Encrypted Archive

The following script provides a handy way to perform a periodic backup of your databases to a S3 bucket in the form of a mysql dump encrypted in a password protected zip.

Pre-requisites

  • Install the aws cli via pip:¬†https://docs.aws.amazon.com/cli/latest/userguide/installing.html
  • Create an s3 bucket
  • Assign a role with access to the s3 bucket to your ec2 instance

 

fileName="dump$(date +%y-%m-%d-%H-%M-%S)"

cd /db/bkp

mysqldump -u backup --all-databases > $fileName.sql

zip $fileName.zip $fileName.sql -me9D -P <YOUR_SECRET_PASSWORD>

aws s3 cp $fileName.zip s3://<YOUR_BUCKET_NAME>/$fileName.zip