AWS Example of Backing Up A Database And Image File

The following shell script can be executed from the command line or via Cron.

#!/bin/sh

cd /home/temp.directory
/usr/bin/mysqldump -u dbrootuser -pdbrootpw --host mysql.my.host.name --single-transaction databasename > databasename.txt
nowdate=$(date +"%Y-%m-%d")
mv databasename.txt $nowdate.databasename.txt
echo
echo "databasename database backup completed."
echo

tar -zcf website.image.tgz ~/website/images/ 
mv website.image.tgz $nowdate.website.image.tgz
echo
echo "website image directory backup completed."
echo

#use aws-cli to upload to DreamObjects
aws --endpoint-url https://objects-us-east-1.dream.io s3 sync /home/temp.directory s3://bucketname/
echo
echo "Backups are in AWS bucket."
echo

#delete backups from web server
rm -f /home/temp.directory/* 

Before running this script you should:
#Set up the AWS bucket for your backups.
#Create a directory called temp.directory in your user root.
#Perform the following substatutions in the above script:
##Replace ‘’‘dbrootuser’’’ with the database root user ID.
##Replace ‘’‘dbrootpw’’’ with the database root user password.
##Replace ‘’‘mysql.my.host.name’’’ with a host URL for your database server.
##Replace ‘’‘databasename’’’ with the name of your database.
##Replace ‘’‘website’’’ with the absolute location of your website’s main directory.

Nice script. One suggestion is to move the DB password out of the script. Recent version of mysqldump will emit a warning about passwords on the command line (which are particularly insecure on shared hosting):

$ mysqldump -u dbrootuser -pdbrootpw ...
mysqldump: [Warning] Using a password on the command line interface can be insecure.

The password can be stored in a local file, ideally an encrypted .mylogin.cnf file created with the mysql_config_editor command. For example, setting the password with:

mysql_config_editor set --login-path=mysqldump --password

More details at: https://dev.mysql.com/doc/refman/5.7/en/password-security-user.html

1 Like