AWS Example of Backing Up A Database And Image File

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


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

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

#use aws-cli to upload to DreamObjects
aws --endpoint-url s3 sync /home/ s3://bucketname/
echo "Backups are in AWS bucket."

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

Before running this script you should:
#Set up the AWS bucket for your backups.
#Create a directory called 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 ‘’‘’’’ 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:

1 Like