If you are self hosting and managing your own WordPress, whether on a simple LEMP stack or using tools like WordOps, Webinoly, Easy Engine, or other similar tools, then this simple backup script might be useful for you.
For some reason, I move my WordPress from server to server quite often and change between stack or tools as well, which means backup is quite important to me. Hence I made this little script to help me simplify the process.
https://github.com/josephcy95/wpbackup
Requirements
This script requires having the below tools installed and Duplicacy initialized, and it’s recommended to backup to at least 2 remote storage for redundancy.
- Duplicacy CLI
- zstd
- WP-CLI
- Remote backup storage of your choice (I use IBM Object Storage & Microsoft Sharepoint)
Install Required Tools
First, we’ll install zstd which is used for backup file compression, it is faster than gzip, but if you want to use gzip, just comment out the line in the script and uncomment the one using gzip.
sudo apt install zstd
Next, we’ll install Duplicacy. The reason I use duplicacy is that it’s fast, and I am more familiar with it as I use it on many other projects as well. Plus, it is easy to delete old backups with retention policy.
Download the right distribution for your server. If you are on Oracle ARM instances, get the ARM version, but for regular Intel/AMD servers, just get the AMD version:
https://github.com/gilbertchen/duplicacy/releases/
wget -O duplicacy https://github.com/gilbertchen/duplicacy/releases/download/v2.7.2/duplicacy_linux_x64_2.7.2
sudo chmod +x duplicacy
sudo mv duplicacy /usr/bin/duplicacy
Last we’ll need WP-CLI. If you are using tools like WordOps, it is probably already installed for you, just try to run the command “wp” to confirm. If it is not yet installed, this is how you install it:
wget -O wp https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
sudo chmod +x wp
sudo mv wp /usr/bin/wp
Once you have done installing all the required tools, we can start using the script.
Using The WordPress Backup Script
First, we’ll create a directory to be used as a backup directory then we’ll download the script. The directory will only be used to prepare backups to be uploaded to remote storage, then the backup file will be deleted. This is because we are not using this for local backup.
mkdir wpbackup
git clone https://github.com/josephcy95/wpbackup.git .
Next, use nano or vim to check the script wpbackup.sh and make sure the directory is matching where your WordPress is installed. Then we will initialize the directory for duplicacy backup, in this post, I’ll use Microsoft SharePoint as an example.
duplicacy init wpback odb://wpbackup
Using Microsoft SharePoint would require a token to grant access. You can use duplicacy tools to get the token, which will prompt when you type the above command. I have gotten my odb-token.json, and I will save it in ~/wpbackup/.duplicacy
Then I’ll save the token as a variable, so duplicacy won’t ask me where the token is every time the backup is run. Then we can try running a backup script to see if everything works correctly.
duplicacy set -key odb_token -value .duplicacy/odb-token.json
./wpbackup.sh
# Then run this to check if the files are in remote location
duplicacy list -files
You are done if there’s no error, and you can see your files in remote storage! Now we’ll just have to run it with cron for an automated daily backup.
0 0 * * * /home/ubuntu/wpbackup/wpbackup.sh > /dev/null 2>&1
Finally, you have done setting up your backup. If you have more questions on duplicacy such as adding more remote location or restoring files, I suggest looking at their documentation at: