How to Rsync a WordPress website to a staging site

How to Rsync a WordPress website from production to a staging server or vice versa (from staging to production).

I made a few Bash scripts to copy WordPress websites between or on the same server(s). Normally I use the Rsync push command from the source to the destination server. This time I made a script that uses the Rsync pull command, this way it will run on the staging server or website.

Rsync is a powerful and robust command line tool and this script will be able to synchronize large websites. I have encountered websites between 150 GB and 600 GB in size; you can migrate them using rsync. It can take a while to copy all the files, depending on the network bandwidth and speed.

Requisites

This script is made for Cloudpanel servers and uses the Cloudpanel CLI to export and import the database. However, it is easy to use the mysqldump command and I also included that command as a comment so you can easily adapt the script for your situation.

SSH keys

You need to generate an SSH key on the staging server and copy it to the production server or vice versa. You can skip this step if you will use the script on the same server, you will need to modify the script so it will work locally.

Here is a useful article: How To Set Up SSH Keys

Generate an SSH key (you leave the password blank when asked)

# ssh-keygen

Copy the SSH key to the destination server (replace 0.0.0.0 for the IP address)

# ssh-copy-id root@0.0.0.0

my.cnf file for database credentials

We will use a separate file to store the database credentials because using passwords in a script is not recommended. We will use a .cnf file which you can also rename to .my.cnf to make it hidden.

[client]
user = "database-user"
password = "database-password"
host = "localhost"

The script to copy the website from production to a staging server

Modify the variables to match your situation.

Crontab

You can run the script manually or add a crontab or cronjob to automate synchronization.

Some WooCommerce users will use staging to test and not sync staging to production because an E-Commerce website is too dynamic and will continuously change. Think about user accounts and orders and statuses.

You can also use the staging site as a backup and run the script every night, this way you always have a recent copy of the website.

To edit the crontab you use this command

# crontab -e

An example of a crontab to run every week on Monday at 02:00 A.M.

0 2 * * MON /home/{PATH}/rsync-pull-production-to-staging.sh 2>&1

The above example will write results to a log file, which is handy when troubleshooting and checking if the script was successful.

An example of a crontab to run every night at 02:00 A.M.

0 2 * * * /home/{PATH}/rsync-pull-production-to-staging.sh 2>&1

The above example will run the script every night and not log into a log file.

Conclusion

It is recommended to use a staging website for testing or development. I have also used a staging website to retrieve files or previous versions of plugins because something went wrong on the production server.

You can also use the script to migrate WordPress websites between servers.

You can use this script to match your use case.

Set up a staging server on DigitalOcean, and get a $200 credit to try

I am using different providers to host virtual servers and so far the General Purpose (Premium Intel) Droplets have the best performance.

Sign up and get $200 in credit for your first 60 days with DigitalOcean.



Leave a Comment