Approx 5 minutes read

After having a bitter experience of losing data due to execution of wrong command, I decided to backup my database regularly. Hence I wrote a simple shell script which backs up the database and produces gzipped version of backup files. Below is the shell script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/bin/sh

#=====================================================================
# Set the following variables as per your requirement
#=====================================================================
# Database Name to backup
MONGO_DATABASE="mydb"
# Database host name
MONGO_HOST="127.0.0.1"
# Database port
MONGO_PORT="27017"
# Backup directory
BACKUPS_DIR="/var/backups/$MONGO_DATABASE"
# Database user name
DBUSERNAME="username"
# Database password
DBPASSWORD="passw0rd"
# Authentication database name
DBAUTHDB="admin"
# Days to keep the backup
DAYSTORETAINBACKUP="14"
#=====================================================================

TIMESTAMP=`date +%F-%H%M`
BACKUP_NAME="$MONGO_DATABASE-$TIMESTAMP"

echo "Performing backup of $MONGO_DATABASE"
echo "--------------------------------------------"
# Create backup directory
if ! mkdir -p $BACKUPS_DIR; then
  echo "Can't create backup directory in $BACKUPS_DIR. Go and fix it!" 1>&2
  exit 1;
fi;
# Create dump
mongodump -d $MONGO_DATABASE --username $DBUSERNAME --password $DBPASSWORD --authenticationDatabase $DBAUTHDB
# Rename dump directory to backup name
mv dump $BACKUP_NAME
# Compress backup
tar -zcvf $BACKUPS_DIR/$BACKUP_NAME.tgz $BACKUP_NAME
# Delete uncompressed backup
rm -rf $BACKUP_NAME
# Delete backups older than retention period
find $BACKUPS_DIR -type f -mtime +$DAYSTORETAINBACKUP -exec rm {} +
echo "--------------------------------------------"
echo "Database backup complete!"

You need to update the parameters in above shell script as per your environment. Specify the name of database to be backed up, host name, port in MONGO_DATABASE, MONGO_HOST and MONGO_PORT parameters. To specify the backup directory, update the BACKUPS_DIR parameter. You need to update the database credentials in DBUSERNAME, DBPASSWORD and DBAUTHDB parameters if you are using one. Finally you need to specify the number of days you want to retain the backup file in DAYSTORETAINBACKUP parameter. Once you update all the parameters as per your environment, save the file as mongobackup.sh. After that you need to make the file executable by issuing following command:

1
chmod +x mongobackup.sh

Now you need to create a cron job to execute this script automatically. Issue following command to create a cron job:

1
crontab -e

Enter following:

1
2
# MongoDB Backup
00 00 * * * /path/to/script/mongobackup.sh

Save the file by pressing Escand then wq + Enter (similar to vi editor). This cron job will execute the backup script at 12 o’clock every night. You can head over to this site to generate a cron entry of your liking.

That’s it, sit back and relax. The script will execute every midnight, create backup files and will also delete the backup files older than specified in DAYSTORETAINBACKUP parameter.