It has been 3 months since I decided to implement my new eCommerce website on the WooCommerce platform which is a part of the Wordpress ecosystem. The site is live now and you go check it out - justspices.in It took me numerous nights and days to learn WordPress and take the site where it is now. Wordpress is kind of a double-edged sword. While it is easy to quickly deploy the site and integrate all the functionalities by choosing among a slew of plugins provided by the Wordpress community but at the same time, it is not so developer-friendly. There is no streamlined developer-workflow defined unlike other programming platforms and you need to bang your head with already abandoned PHP language to get what you want from the website.
Getting the rant out of the way, let’s dive straight into the topic of discussion for today’s post on how I automated Wordpress’s database and files backup. Now, I know what you are thinking. Jiten why would you sweat yourself writing a script when you can easily install a plugin and get it done hassle-free. See, as a programmer, you need control on your software and I already had installed enough plugins on my website which made me lose track of which plugin did what.
So, here I am writing this post mainly for the following reason -
I have hosted my site on amazing AWS’s lightsail which has one-click installation for Wordpress sites. You can check out how to set up it here
You can essentially track all the changes that you do on your Wordpress site by tracking database changes and file changes. All your plugin-installs, media-uploads, and user-generated changes updates database or WordPress files or both. So, if you were to back up your site you would need a recent snapshot of your site’s file and database.
I have written a shell script that runs periodically via cronjob. If you don’t what cronjob is you can refer here and to quickly get the correct format of your desired cadence you can use tools like crontab.guru
The shell-script looked like this -
#!/bin/sh
NOW=$(date +"%m-%d-%Y")
path=~/Documents/backup_testing/db_backups/
sql_extension=".sql"
zip_extension=".tgz"
file_name=$NOW$sql_extension
full_file_path=$path$NOW$sql_extension
db_name=testDB
echo "backup script execution initialisation"
zip_file_name=$NOW$zip_extension
zip_file_path=$path$NOW$zip_extension
aws_bucket_name=justspices-backup
s3_path=s3://$aws_bucket_name
cd $path
mysqldump -uroot --databases $db_name > $file_name
echo "backup mysql file created"
tar -czf $zip_file_name $file_name
echo "backup zip file created"
aws s3 cp $zip_file_name $s3_path
echo "file sent to aws"
rm $file_name
rm $zip_file_name
echo "files deleted and script execution completed"
So, basically what this does every day is -
Create a mysqldump to a file named as current-date by this command -
mysqldump -uroot --databases $db_name > $file_name
Once the file is created push it to AWS S3 bucket using this command -
cp $zip_file_name $s3_path
Once push is complete remove the files.
AWS S3 bucket works fine here as they are super-cheap to use and have amazing API for any language and platform.
Future Improvement - Cleanup the S3 bucket for older files.
For backing up my files I simply chose GitHub as my backup repository. I am not sure why aren’t many people doing this, taking periodical backups in their Github repo by a cronjob. Backing up on a repo is free, easy and your changes are stored as a timed-snapshot which can be restored to any point easily.
Before running any GitHub commands on your instance make sure you have ssh keys of your instance on your GitHub account. You can refer to this document - Here is what I did using a cronjob periodically - Add all the changes Commit with a timestamp Push it Simple right?
#!/bin/sh
cd /home/bitnami/apps/wordpress/htdocs
current=$(date +"%Y-%m-%d %T")
commit_message="script commit ${current}"
git add .
echo "will commit files now"
git commit -m "${commit_message}"
git push
So, here is how I am backing up my WordPress site without using any external plugins. My method is also really cheap and cost-effective which gives you complete control over your backups.
Let me know your thoughts on how I am performing my backup, is there something I could have done better. Let me know in the comments. See you next time.