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

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.

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.

If we are using IN as the region then use 's3.ap-south-1.amazonaws.com' 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

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

Step 5: Setup the Backup Script

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

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

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 backup.sh 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

Hourly Backup

This will run our backup script every hour.

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

Weekly Backup

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

Monthly Backup

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

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

 

Categories
Most Popular

Leave a Reply

Your email address will not be published. Required fields are marked *