Best Practice for Mongo backup

Best Practice for Mongo backup

Paxata uses Mongo document DB as metadata DB, this metadata DB is very critical for Paxata application, hence setting up mongo replica setup is important. Also taking daily mongo backup is important. Below are the steps how this can be automated and controlled to keep last 7 backups.

Please ensure to select backup time when there is no paxata activity going on during backup like paxata automation jobs or REST APIs, or paxata app access so that backup has clean data.

sudo mkdir -p /data/paxata/mongobackups

Then backup command should look like this:

sudo mongodump --db paxata --out /data/paxata/mongobackups/`date +"%m-%d-%y"`

Note,we have used  command date +"%m-%d-%y" which gets the current date automatically. This will allow us to have the backups inside the directory.

As a general rule, you should make regular backups, such as on a daily basis, and preferably during a time when the server is least loaded. Thus, you can set the mongodump command as a cron job so that it's run regularly, e.g. every day at 03:03 AM. 

sudo crontab -e

Note that when you run sudo crontab you will be editing the cron jobs for the root user. This is recommended because if you set the crons for your user, they might not be executed properly, especially if your sudo profile requires password verification.

Inside the crontab prompt insert the following mongodump command:

Crontab window

3 3 * * * mongodump --out /data/paxata/mongobackups/`date +"%m-%d-%y"`

In the above command we are omitting the --db argument on purpose because typically you will want to have all of your databases backed up.

To control how many backup copies you want to keep, you can delete the older ones. To delete all the backups older than 7 days you can use the following bash command:

find /data/paxata/backups/mongobackups/ -mtime +7 -exec rm -rf {} \;

Similarly to the previous mongodump command, this one can be also added as a cron job. It should run just before you start the next backup, e.g. at 03:01 AM. For this purpose open again crontab:

sudo crontab -e

After that insert the following line:

Crontab window

3 1 * * * find /data/paxata/backups/mongobackups/ -mtime +7 -exec rm -rf {} \;

Completing all the tasks in this step will ensure a good backup solution for your MongoDB databases.

Below is the script that can executed to achieve all of the above
The steps below creates file /tmp/ this script will stop paxata, create backup and bring it up.
The last line edits cronttab entries

sudo echo "sudo mkdir -p /data/paxata/mongobackups" > /tmp/
sudo echo "sudo service paxata-server stop" >> /tmp/
sudo echo "sudo mongodump --db paxata --out /data/paxata/mongobackups/`date +'%m-%d-%y'`" >> /tmp/
sudo echo "sudo service paxata-server start" >> /tmp/
sudo echo "sudo find /data/paxata/backups/mongobackups/ -mtime +7 -exec rm -rf {} \;" >> /tmp/
(crontab -l 2>/dev/null; echo "3 3 * * * sh /tmp/") | crontab -

To restore the paxata schema from a Mar 23 2017 use the below command

sudo mongorestore --db paxataMar232017 --drop /var/backups/mongobackups/03-23-17/paxata/

once you confirm paxataMar232017 has everything you can drop paxata or older schema.

0 Kudos
0 Replies