Backing up wordpress to S3 bucket with cron

So after doing several scripts for backing up wordpress to various places, and primarily S3 buckets, I figured I better put the script here so it is easier for me to find it the next time, and maybe it will be useful for you too 🙂

“Before you act for the greater good, you should know what is great and what is good.” ― Jeffrey Fry

So we start with some general niceties

I use cron to run this script that copies all the files under /var/www/html and dumps the db, then zips it up and pushes it to S3

# Set the date format, filename and the directories where your backup files will be placed and which directory will be archived
NOW=$(date +"%Y-%m-%d-%H%M")
FILE="my_website_backup.$NOW.tar"
BACKUP_DIR="/home/ec2-user/backups"
WWW_DIR="/var/www/html/"

[If you wish to make it faster and smarter – filter the files from the html folder, some plugins store the entire cache in there, urrrgghhh]

Now, some DB credentials – again this is my stuff – you put your own ingenious passwords etc.

Nice feature, btw, of S3 buckets is that you can set expiration on files so they get automatically deleted after a certain amount of time. Also different paths on the bucket can have different expirations. Ponder that for a sec…

Now for the whole thing, but before – since I placed it all on a cron, and the cron needs to have the same PATH and access to the aws cli and config, I have inserted a few lines at the beginning – but next time I do it – I will add this to the cron. Maybe.