How to Automate WordPress Backups to Amazon S3 with a Bash Script

Tech

I’ve been working with WordPress for almost 20 years now. Hosting has always been a pain in my side to properly handle scaling, caching, and delivery. Recently, I started using an Open Source product called WordOps and have been very impressed with it.

The following WordPress backup script is tailored to the file structure of WordOps, but it can easily be customized to fit any need.

To give you some basics of how I adapted this script.

All my sites live in /var/www, such as /var/www/domain.com and furthermore, the main directories live in htdocs. For example, /var/www/domain.com/htdocs which is where WordPress actually lives, with the exception of wp-config.php which lives one level above the web-accessible layer.

Instructions

  1. I first created an IAM user on AWS with full permissions to S3 — only. Nothing else, only S3.
  2. I created a new user on Ubuntu called “wpbak” which is designated only for this task of baking up WordPress.
  3. I enabled sudo access for this user and ensured that sudo could be run without a password.
  4. Lastly, I installed awscli and configured it using this new user that I created in AWS. Make sure you set your zone correctly as well.

Now I can run this script using sudo ./backup.sh and runs just as I expect it to.

Here is the Gist of it.

There are some optional tasks in the script, such as deleting local files, that you can toggle at your pleasure.

HT to Mike at WP Bullet for publishing the previous version. And a huge thanks to ShellCheck.net for bash validation, which helped make it easier to spot errors.

If you have any feedback or suggestions, please do leave them in the comments. I know its far from a perfect script.

Leave a Reply

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