Don’t let MongoDB get amnesia, quiesce it!
If you are a MongoDB user that also has Zerto Virtual Replication you can now get quiesced Zerto journal checkpoints with a script I published this week over on the Zerto Tech Alliances Github page. (BTW if you are a Zerto customer you may want to bookmark that user account because that is where @shannonsnowden and I publish all the stuff we are working on)
Right now this script works for Linux based MongoDB servers and requires a couple of scripts to be added to your MongoDB server as well as one to your ZVM server. Once set up, you can schedule the script to run as often as you would like, and the result is a Zerto User checkpoint where MongoDB has flushed all remaining data to disk.
Do you really need this to recover MongoDB?
Probably not! In my testing, I was able to recover MongoDB with normal Zerto checkpoints just as easily as I could with the user checkpoints generated by this script. However, sometimes you just like that warm and fuzzy feeling, or maybe you have some other use case where it makes sense to quiesce.
Keep in mind though that I am by no means pushing my MongoDB server in terms of transactions per second. So this script may be even more useful on a server at scale than it is in my lab.
How to set it up
To get started we need to install a few things on our ZVM server (or any Windows server really). Right now the easiest way to talk to Zerto is through our PowerShell API, that is why we have a script that needs a Windows server, once some API’s are updated this year I may be able to remove Windows portion and run this all from the MongoDB server.
First, install the Zerto Powershell SnapIn, you can get it from MyZerto just below the ZVM installers. You can test that everything installed correctly by opening a PowerShell window and typing the following.
After that, you will be asked some information about your ZVM. Make sure if you have not already changed the Zerto PowerShell cmdlet login credentials that you use administrator / password for those fields.
If you see checkpoints listed then all is working well.
Next up you will want to install the PoSH-SSH module as well. This module can be downloaded and installed right from the official PowerShell library.
The library is here https://www.powershellgallery.com/packages/Posh-SSH/2.0.1
and to install it just run this command
Install-Module -Name Posh-SSH -RequiredVersion 2.0.1
Lastly, save the PowerShell script to the server in a convenient location.
(We will copy a private key file over to Windows from out Linux box, but I’ll show you how to do that in the Linux requirements section.)
These requirements will all need to be installed on the Linux machine that is running MongoDB. There are two scripts, one for freeze and one for unfreeze, these will need to be made executable and placed in the /usr/local/bin/ directory. (or somewhere in PATH of the user your SSHing in as)
The easiest way to get the script on your linux machine is git clone if you have git installed.
git clone https://github.com/Zerto-TA-Public/MongoDB-Journal-Checkpoints.git
sudo cp mongo_* /usr/local/bin/
Next up you will want to use your preferred text editor to change the MongoDB admin password in both mongo_freeze.sh and mongo_unfreeze.sh (make sure to change it in the /usr/local/bin folder)
You can also test these scripts locally too by running them, just make sure that you run unfreeze quickly after you run freeze as all writes stop to your database while it’s frozen.
The last thing we need to do on the MongoDB server is to generate a public/private RSA key so that we can login to ssh without a password. From the useraccount that you plan to ssh into MongoDB with run the following commands to generate the key pair as well as add the public key to the authorized_keys file.
ssh-keygen -t rsa
cat .ssh/id_rsa.pub >> .ssh/authorized_keys
Lastly we need to copy the private key that we just generated over to our Windows box. The file that we need is /home/username/.ssh/id_rsa the script will look for this file on the c:\ drive of your Windows machine by default. (I used WinSCP to copy this file to my Windows machine.)
Customizing the PowerShell script
Now that you have the ssh private key on the Windows server we can open up the PowerShell script and customize the variables that are being used by the script.
The things you will want to change are:
- VPGName (name of the VPG that MongoDB is in)
- ZVMUser (if you added or changed the default username)
- ZVMPassword (if you changed the sha1 hash)
- MongoServer (ip or hostname of the MongoDB server)
- MongoUser (this is the SSH username for MongoDB)
Test Running the Script
To test out the script open a PowerShell window and run the script manually.
If there are no errors you are now ready to schedule the script to run automatically.
Scheduling the script to run
On your Windows machine, you can navigate to the Task Scheduler inside of Computer Management and tell this PowerShell script to run as often as you would like. Keep in mind however that all writes to the database are paused while this is executing, so don’t schedule it to run every 5 seconds or something silly like that!
To get started click Create Task in the right actions menu to bring up the Create Task wizard. Give your task a name then click on the triggers tab.
On the Triggers tab click New and schedule the task to run Daily (recur every 1 day) and to repeat task every X minutes based on what you want, for a duration of 1 day.
Next, on the Actions tab, we need click New, then tell the task to run Powershell.exe, and also specify some parameters.
For me the parameters box includes the following:
-ExecutionPolicy Bypass c:\Zerto-Mongo-Checkpoint.ps1
Adjust yours to where you placed the PowerShell script.
Click OK to get back to the name Task Scheduler and then navigate into the Library View. You should now see your task. Wait a bit until the “Last Run TIme” changes to a relevant time.
Now we can login to ZVM and check to see if there is a User Checkpoint at the same time (note in my screenshots my time is off between the servers a little bit)
(Click the image to make it bigger)
And there you go, now we are replicating MongoDB quiesced checkpoints every 5 minutes with Zerto!
Thanks for reading!
I hope this helps, let me know if you have any questions!