Synchronizing WordPress multi-site database and media files from production to staging environment

We have a multi-site WordPress setup that we want to stage development to, so therefore we need to have an automated way to synchronize the data to our staging environment.

We will setup a sync account on both servers, setup ssh access via keys and get a dump of the production database, replace the staging database and update it to work on our staging environment. We will then copy all uploaded files.

If you want to just get something working fast, you can do the preparations step and then download this script.

Preparations

Create user accounts

Run the following command on both computers to create a sync user, we call him wpsync:

useradd wpsync
mkdir /home/wpsync
chown wpsync:wpsync /home/wpsync

Setup certificates

On staging server, run the following command and hit enter (default) on all questions:

#staging
sudo -u wpsync ssh-keygen -t rsa

Copy the contents of /home/wpsync/id_rsa.pub from the staging server to /home/wpsync/.ssh/authorized_keys on the production server