In this post you will learn how to automatically dump your MongoDB database, rsync it across to your staging server and restore it on your staging server.
Step 1) Setup SSH
On your production server, create a new public key and print it to the console.
ssh-keygen -f ~/.ssh/id_rsa -q -P ""
You will see a row of output that begins with ‘ssh-rsa’, copy the whole row to your clipboard.
On the staging server open up the authorized keys file and past the key that you generated in the previous step. Save and close the file.
Test that rsync is working by syncing a test directory. In the command below I am syncing a directory called db_backups where I will later dump the MongoDB database to.
rsync -avz ssh ~/db_backups/ email@example.com:~/db_backups
The output from the command should tell you how many bytes were sent, how many were received ect…
Step 2) Automate the dump with crontab
In this step we will create a shell script and tell crontab to execute the script.
Add the following commands to your shell script
In this command we dump the database into a folder, rsync it across to our staging server and delete the backup folder. I like to name the backup folder by the current date because I also sync the backups to an Amazon S3 bucket.
Finally execute your script with crontab.
crontab -e00 18 * * 1-5 ~/scripts/backup.sh >> /root/temp/backup.txt
I have set the crontab to run every week day at 6pm. You can easily generate crontab schedule expressions with crontab guru.
Step 3) Restore the database to the fresh version
Now that you now have the fresh database on your staging server can can drop your old collections and insert the new ones. We’re going to use the same setup, a shell scrip and crontab.
You want to execute the script when you know you have the latest database in the backups folder so I run the script 15 minutes after the script on the production server.
crontab -e15 18 * * 1-5 ~/scripts/restore.sh >> /root/temp/restore.txt