Backup Wordpress site data to Amazon S3

Dilip Kola
Tensult Blogs
Published in
3 min readNov 16, 2019

I am writing a series of blogs on setting up backup for WordPress site. As part of the series, I have written a blog “ Setup Fallback site for WordPress using Route53 “. Please have to look at that blog, you may find it relevant.

In this blog, I am going to explain how to take a backup of WordPress site code and data into an S3 bucket.

Prerequisites

WordPress S3 Backup process steps

1. Create an S3 bucket with a name: <your-wordpress-site-name>-backup.
Amazon S3 is a cheap and reliable storage solution and makes it ideal for keeping WordPress Site backup files.

2. Create an IAM user with a name: wordrpess-s3-backup-user and attach the following policy to this user.

{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:ListBucketMultipartUploads",
"s3:AbortMultipartUpload",
"s3:ListMultipartUploadParts"
],
"Resource": [
"arn:aws:s3:::<your-wordpress-site-name>-backup",
"arn:aws:s3:::<your-wordpress-site-name>-backup/*"
],
"Condition": {
"IpAddress": {
"aws:SourceIp": "<your-wordpress-server-ip>"
}
}
}
]
}

Please replace the placeholders in the IAM user polic. Also don’t use this user credentails for any other tasks then you will keep adding more permission. Then it will become a voilation of least previlages principal.

3. Create a Read-only MySQL DB user with SELECT permission on all WordPress DB tables. To do so, connect to MySQL DB and execute the following commands:

CREATE USER 'wordpress_backupuser'@'localhost' IDENTIFIED BY '<Pasword>'; GRANT SELECT ON * . * TO 'wordpress_backupuser'@'localhost';

Please do not ignore the above step, you should always follow the principal of least previalges to prevent accidental data loses and misuse.

4. SSH to WordPress server and install AWS CLI. And also configure the CLI with credentials created in Step 2. We recommend installing it in the user directory using the following command.

pip3 install awscli --upgrade --user

Please make sure test the AWS CLI installation by executing the following command:

touch test-aws-access.txt 
aws s3 cp test-aws-access.txt s3://<your-wordpress-site-name>-backup

5. Create the following S3 backup script with a name: wordpress-s3-backup.sh

# MySQL Dump mysqldump --single-transaction -u wordpress_backupuser -p<Password> -h localhost <Your-wordpress-DB-name> > /tmp/wordpress-dump.sql # Zip wordpress code 
rm -rf /tmp/wordpress-code.zip
zip -r /tmp/wordpress-code.zip /path/to/your/wordpress-root-dir
dy=`date --date="-1 day" '+%Y'` dm=`date --date="-1 day" '+%m'` dd=`date --date="-1 day" '+%d'` # Copy to S3 /path/to/aws s3 cp /tmp/wordpress-dump.sql s3://<your-wordpress-site-name>-backup/$dy/$dm/$dd/db/ /path/to/aws s3 cp /tmp/wordpress-code.zip s3://<your-wordpress-site-name>-backup/$dy/$dm/$dd/code/

Please replace the placeholders in the backup script.

6. Setup Cronjobs to take backup periodically with the following Cron expression:

crontab -e 
# Executes backup script midnight of everyday.
0 0 * * * /path/to/wordpress-s3-backup.sh

7. Setup an S3 bucket life cycle rule to delete older backup files. This is an important setup and will help you to keep tabs on S3 storage costs.

Conclusion

I hope you have understood, why and how to set up a backup for your WordPress site. Please try these steps on your existing WordPress setup and let me know if you face any issues. Also, subscribe to our newsletter for more interesting updates.

Originally published at https://blogs.tensult.com on November 16, 2019.

--

--

Dilip Kola
Tensult Blogs

Spirtual Seeker | Mentor | Learner | Ex-Amazon | Ex-AWS | IIT Kanpur