Scott Alan Henry

Share what you think you know…

Amazon S3 Database Backups

NOTE: Once this is done the first time you can just copy the script and s3/ to the new site directory and modify the files.

1
2
3
4
5
6
cd /opt
sudo wget https://github.com/s3tools/s3cmd/archive/master.zip
sudo unzip master.zip
cd s3cmd-master/
sudo python setup.py install
which s3cmd (/usr/local/bin/s3cmd)

Just above Drupal root:

1
2
mkdir s3; cd s3;
nano s3/mysql.cnf

Add this to the mysql.cnf file:

1
2
3
[client]
user=‘DB_USER'
password=‘DB_PASSWORD'

Make a log directory:

1
mkdir log

You may need the following:

1
sudo apt-get install python-dateutil python-magic;

Now we can configure s3

1
sudo s3cmd —configure;

(You need the Access Key and the Secret Key from s3.) You should see output like: Configuration saved to ‘/home/[USER]/.s3cfg’ Move the .s3cfg file to your s3 directory

Add the script just above Drupal root:

1
nano sqlbackup-s3.sh;

Add the following contents and make changes to variables where necessary:

If sendmail doesn’t send, make sure the log file has a subject line. Should be added by the script:

1
subject: S3 database backup

Run the script and make sure the db uploaded to S3.

Now we can setup a cron job.

In the CLI:

1
$ crontab -e

Add the following replacing the path to the script:

1
2
# Backup the database at 12am and 12pm everyday.
0 0,12 * * * /PATH/TO/DIR/ABOVE/ROOT/sqlbackup-s3.sh

Comments