For backing up of WordPress sites, I have been using a myriad of backup strategies which rely on a couple of SaaS services. One of the services is Dropbox.
I have been enjoying the perks of a rather outsized Dropbox space limits for a free account. Due to tie-ups with HTC in the past, I had received a fair bit of space from them. This additional storage space is time-limited and had since expired. Additionally, I had participated in the giveaways that Dropbox had for students (while I was still in university) and had maxed out the storage space given through referrals.
Recently, my Dropbox keeps showing me the following notification on my devices:
I have been using Dropbox for many purposes. One of them is to back up some of the sites that I have been maintaining. However, since I have built a NAS (of sorts), I thought I should utilise it more. This NAS has a RAID6 setup, spreading across 6 3TB hard disks.
I had been relying on Updraft plugin to backup to Dropbox. It has been a great plugin that I have been utilising to automate the backups. To change to using my NAS, I have to switch to using either FTP or SFTP. My preference is SFTP. However, SFTP is a paid extension for Updraft. The miser I decide to code out a bash script to run the backup on a scheduled basis instead.
The benefits of doing so are:
- Learn bash (again).
- Offloading the processing to generate backups from WordPress to the server itself.
The script that I have done is available as a gist:
Before deploying this script for use, there are a couple of conditions to be fufilled:
- The target server must have WP-CLI installed.
- The target server must have a backup folder.
- The target server has the NAS server’s ssh public key.
This script is written to be reused across different server. This script takes in a set of configured values for the specified domain name. You should add additional sets in the case statement as required.
The script then does a mysqldump of the database and compresses the wordpress installation. You can specify which folder or file to ignore in a comma-delimited manner. The script stores these files in the specified backup folder.
The script then transfers the backup files from the target server to the NAS via rsync. I added a line here to transfer from one mount point to another because the first mount point is actually an SSD and the second is the actual RAID array. I am using it to speed up the transfer speed from the target server to the NAS server. The script deletes the backups in the target server.
The script then deletes older backups in the NAS server.
This script is still a work in progress. There are checks and features that I had thought up, but wasn’t necessary to kickstart the process to change the backup destination, starting with this site. These however should be added as I update the backup options for the other sites. Some of the extra stuff that I want in are:
- Install WP-CLI on target server.
- Failing which, extract the database information somehow from wp-config.php.
- Add checks and exception handling at every possible line where things may break.
- Adhere to the linter’s suggestions.