Tuesday, November 26, 2019

takes automatic database backups for mongodb

Nowadays database backup is the most important thing in popular web sites and applications. Database backups are most essential for protection against data loss. but because of takes database backup manually, it makes headaches for most people.

Generally, developers make some script by which it will take backup automatically in a given time to time. like as script here we create a cronjob in linux / ubuntu server for takes database backup in every day.

Here, We will discuss how to takes mongodb database backup automatically every day. but you can also take another database backup like mysql, postgresql,  etc.

linux / ubuntu has built-in cron.service by which we can run any takes or command as per our requirements.

For takes automatic database backups for MongoDB in linux server check following steps

-> goto backup directory
cd /home/user10/backup
Here, you can use any directory for taking backups, right now we take backups in a backup directory

->Create database backup directory/folder
sudo mkdir db_backups
Create db_backups directory/folder to store all database backup here.

->Create script.sh file
sudo touch script.sh
Create a new script.sh file by using touch command from the command prompt or you can make manually this file in db_backups directory. in this script, we will put takes/commands for taking backup in every day.

Now, finally, our directory structure made like as
/home/user10/backup
    |-----script.sh
    |-----db_backups
            |-----11-05-19 (this will be made and store backup automatically)
            |-----11-06-19 (this will be made and store backup automatically)
            |-----11-07-19 (this will be made and store backup automatically)
            ....
            ....
            ....

->Edit script.sh file and put following code
#!/bin/sh
DIR=`date +%m%d%y`
DEST=/db_backups/$DIR
mkdir $DEST
mongodump -h <your_database_host> -d <your_database_name> -u <username> -p <password> -o $DEST

->EX : script.sh
#!/bin/sh
DIR=`date +%m-%d-%y`
DEST=/home/user10/backup/db_backups/$DIR
mkdir $DEST
mongodump -d business -o $DEST
Here, we can run mongodump command with required options

DIR=`date +%m%d%y` line is put for takes current date like as 11-05-19, 11-06-19, 11-07-19, etc, you can use any format as you like
DEST=/db_backups/$DIR line will be the destination path where our database backup will be stored.

->Set Up cron to Run the Script at 2:30am every night
sudo crontab -e
Run above command for add cronjob. if you haven't selected any default edit then you can choose any your favorite edit listed in cmd and then add  30 2 * * * /home/user10/backup/script.sh in it like as following and save it

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

30 2 * * * /home/user10/backup/script.sh
Here we cron will be run at 2:30am every day.

->Now, give exicution permision to script.sh
sudo chmod +x script.sh

->Enable and start cronjob/crontab service
sudo systemctl enable cron.service
sudo systemctl start cron.service

->for stop cronjob/crontab service
sudo systemctl stop cron.service

Now, database backup will be taken automatically every day.

If you want to remove directories that are 30 days old. and If you don’t want to keep more than 30 days of backups, then you can also add commands in script.sh for remove old backups.

No comments:

Post a Comment