How to Automate WordPress Backups to Amazon S3 with a Bash Script
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.
- I first created an IAM user on AWS with full permissions to S3 — only. Nothing else, only S3.
- I created a new user on Ubuntu called “wpbak” which is designated only for this task of baking up WordPress.
- I enabled sudo access for this user and ensured that sudo could be run without a password.
- 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.
There are some optional tasks in the script, such as deleting local files, that you can toggle at your pleasure.
If you have any feedback or suggestions, please do leave them in the comments. I know its far from a perfect script.