Backup your database using cron

The first question that may arise in our mind is why do we need to back up our database?

Lizen Shakya
2 min readFeb 13, 2022

The database is the most important part of our program. It contains all the necessary information required.

Database backup is the process of backing up the operational state, architecture, and stored data of database software. It enables the creation of a duplicate instance or copy of a database in case the primary database crashes, is corrupted, or is lost.

For this example, we will be using MongoDB.

We can take mongo backup by running the command

mongodump --host ${MONGO_HOST} --port ${MONGO_PORT} --db ${DB_NAME} ${AUTH_PARAM} --out ${DB_BACKUP_PATH}

And for exporting some collection of the database, we can use

mongoexport --host ${MONGO_HOST} --port ${MONGO_PORT} --db ${DB_NAME} ${AUTH_PARAM} --collection ${Collection_Name} --out ${DB_BACKUP_PATH}/${Collection_Name}.json

Why automatic backup?

In a production environment backing up data manually, day to day can be a tedious task. We have the ability to time-based job scheduling in the form of Cron.

What is CRON?

Cron is a software utility that offers time-based job scheduling. It supports Unix computer operating systems. To set up software environments, the developer uses Cron. He/she schedules commands or shell scripts so that they run at chosen times. It could be daily, once a week, or any interval as desired.

To automate database backup using cron. We will do the following:

Create a shell script that will dump the MongoDB database

cd ~
mkdir scripts
cd scripts
nano mongo_backup.sh

Paste the following command which will create the database backup.

#!/bin/bash#naming dir name as dd-mm-yyyy
DIR=`date +%d-%m-%y
#Destination to store your mongo backup
DEST=~/db_backups/$DIR
#making dest directory
mkdir $DEST
# If mongodb is protected with username password.
# Set AUTH_ENABLED to 1
# and add MONGO_USER and MONGO_PASSWD values correctly
AUTH_ENABLED=0
MONGO_HOST='localhost'
MONGO_PORT='27017'
MONGO_USER=''
MONGO_PASSWD=''
if [ ${AUTH_ENABLED} -eq 1 ]; then
AUTH_PARAM=" --username ${MONGO_USER} --password ${MONGO_PASSWD} "
fi
mongodump --host ${MONGO_HOST} --port ${MONGO_PORT} ${AUTH_PARAM} -d my_db_name -o $DEST

Now chmod the script to allow it to for execution

What is Chmod?

The chmod a short command of ‘change mode’ enables the admin to set rules for file handling. In other words, with the help of a “chmod” system call. An administrator can change the access permissions of file system objects.

chmod +x ~/scripts/db_backup.sh

Now open crontab on your Linux operating system.

What is cronTab?

Crontab stands for “cron table”. It allows using a job scheduler, which is known as a cron to execute tasks.

crontab -e

Add the time you want to run your cron along with the path of the script. And save it.

The example below will take backup everyday at 7am.

0 7 * * * ~/scripts/db_backup.sh # take a backup at 7am

So this is the simple example on how you can backup your db using cron.

You can calculate cron from this website also.

--

--