Documentation / Tutorials / Backup

WordPress Backup to Amazon S3 using s3cmd

/ Backup / WordPress Backup to Amazon S3 using s3cmd

In this tutorial, we will show how to use s3cmd to set up an incremental backup for all your WordPress site hosted on a VPS server. We will be backing up the WordPressfiles to Amazon s3.

Step 1: Setup s3cmd

To setup s3cmd, we will need to setup and install its dependency

sudo apt-get install python python-setuptools
sudo locale-gen en_US en_US.UTF-8
sudo dpkg-reconfigure locales

Once python is installed, go to GitHub repo of s3cmd, and copy the latest release URL, replace the URL with wget command below.


After download of the s3cmd source is complete, run the following set of commands to setup it up.

tar xzvf s3cmd-2.0.1.tar.gz
cd s3cmd-2.0.1
sudo python install

Now, s3cmd is set up on your VPS server, run the following command to configure it. You will need your s3 Access Key and Secret Key, you can get it from 'Security Credentials' section on the S3 console.

s3cmd --configure

If we are using IN as the region then use '' as the base URL.

We have now configured the first piece of our backup strategy.

Step 2: Create the s3 Bucket

Got to your S3 console and create a bucket where you want to keep your backup files.


The only thing to remember when you are creating the bucket is to enable the versioning. This will ensure that you have backups of the files that are deleted on the server when you need it.

Also, we will need to set up a lifecycle rule to delete the old version of files. I prefer to delete them after 180 days. See the image below to get an idea.

We have now configured the s3cmd and we have also set up the S3 bucket we need to keep the backups, now lets set up the backup shell script.

Step 4: Setup the Mailutils

apt-get install mailutils

This is needed to send simple email after s3 file sync .

Step 5: Setup the Backup Script

Create file in /usr/local/sbin directory on VPS. Put the following code in the newly created file.

You can also download the directly from the actual gist. now let's make the script executable by running following command

chmod +x

In the script, you will need to setup few variables to make it work for you. In this script, we have assumed that database name and folder follow a similar naming convention.

SITESTORE: is the base path from where s3cmd will start the sync
S3DIR: is the S3 bucket path from which backup will be stored.
MYSQL_USER: name of the mysql user with enough privileges to take the dump of the database.
MYSQL_PASS: password of the user specified above.

Now if you run the script it will take the database dump of all the databases and sync the files to your s3. A good backup strategy for a WordPress site does not just depend on the manual backups but schedules auto backups, and for that, we need to setup cron.

Step 6: Setup the Cron

Depending on your needs you can set up an hourly, daily, weekly or monthly backups.  below are the cron commands you will need.

Open a crontab editor using

crontab -e

Hourly Backup

This will run our backup script every hour.

0 * * * * /usr/local/sbin/ > /dev/null 2>&1

Daily Backup

This will run our backup script every day at midnight, you can change the hour value to change what time everyday backup happens

0 0 * * * /usr/local/sbin/ > /dev/null 2>&1

Weekly Backup

This will run every week on Sunday at midnight. You can change both the time and day of the week for backup.

0 0 * * 0 /usr/local/sbin/ > /dev/null 2>&1

Monthly Backup

This will run the backup 1st of every month at midnight. You can change the date and time of the backup

0 0 1 * * /usr/local/sbin/ > /dev/null 2>&1

If you need to change the schedule, check out more crontab examples.

Restore the backup

Assuming s3cmd is setup properly, you can use the following command to restore the backup files

s3cmd sync s3://bucket-name/ /var/www/


Most Popular

Leave a Reply

Your email address will not be published.