How to Backup WordPress Sites If You Have Multiple Servers
People have multiple virtual private servers for various reasons. You may want to try different Linux distributions on differrent servers. I myself have 4 VPS located across the world. In this tutorial I will show you how to backup your WordPress websites between your servers.
Make a Backup of Your Database
The first thing you want to do is backup your WordPress database. It contains your posts, pages, user information, site settings and so on. You can backup all your databases with the following command,
mysqldump -u root --password=root-password --all-databases --master-data | gzip > all-databases.sql.gz
root-password is the database root user password, not your Linux server root password. The above command will backup all your databases in MariaDB or MySQL database server and save them as a file named all-databases.sql.gz. You can name this file to your liking.
Backup WordPress Site Files
This can be done using the below command:
tar -cpzf site-files.tar.gz /path/to/your/web/root
Common web root are /usr/share/nginx/html/, /var/www/html/. This command will backup all your WordPress site files, e.g. the WordPress program, theme file, your uploaded pictures, plugins and so on and save it as a file named site-files.tar.gz. Again, you can name it to your liking.
Send Backup Files to Remote Server
You can use the scp (secure copy) command line utility to do this.
scp all-databases.sql.gz site-files.tar.gz username@remote-server-ip:/home/username
scp uses the ssh protocol to connect to your remote server and send the previously backed up files to it. You need to enter the remote user’s password. The above command will save the backups to the remote user’s home directory.
How to Automate Your Backup process
First create a cron job with the below command
This will open up your cron job file. Put the following text into it.
0 4 * * * mysqldump -u root --password=root-password --all-databases --master-data | gzip > all-databases.sql.gz && tar -cpzf site-files.tar.gz /path/to/your/web/root
Save the file. The first part 0 4 * * * means on 4 AM (0 4) every day (the first asterisk) every week (the second asterid) every month (the third asterisk) the above command will be executed automatically. The double ampersand (&&) means execute the next command only after the previous command has successfully be executed.
You can also set the command to be executed every other hour, just change the first part 0 4 * * * to 0 2/* * * *.
The above cron job only backup your WordPress to the local server, we have to send our backups to remote server. Using scp command as we discussed before require you to enter the remote server user’s password. It’s not automated. What we can do is we can set up a passwordless ssh login between your servers so that you don’t have to enter password when sending backups.
It’s a good idea to set the MAILTO variable in cron file like this:
If the cron job didn’t completed successfully, it will send a report to the email address you assigned to MAILTO variable.
Now you have multiple copies of your WordPress site across your servers. If one server goes down or hard drive crashes, you can quickly bring your site up and running by restoring databases and site files on another server and point your DNS records to that server.