Schedule the DB backup job with nodejs

Schedule the DB backup job with nodejs

Job scheduling has always been an important part of any application where we want to make certain tasks automatically like sending emails to the subscribed user or anything like that.

In this article, we will cover that how we can make a cron job that takes DB backup automatically at a given time interval.

First, we will need to create a node project, Create node app with the following command.

npm init -y

The above command will make package.json file and -y will give all question-answer which asked during making a project.

We will be using express.js and to run the cron job we will be using a package called node-cron

npm i express node-cron

The above command will install express and node-cron package.

Let's create a file called index.js and import the following things.

index.js

const { spawn } = require('child_process');
const path = require('path');
const cron = require('node-cron');

Spawn process will help us to run command in a terminal, It also does many things but we only need it to run our command.

Path will be used to join the given path which will be provided along with the directory name.

The following command will dump a mongo DB backup in the format we will provide.

mongodump --db=<your db name> --archive=./<path>.gzip --gzip

//example

mongodump --db=users --archive=./userDBBackup.gzip --gzip

The above command will have a couple of arguments, first will be the command name which is mongodump, the Second one is DB name, the Third one is the path of the archive where we want to store the DB backup and the last one will be the compression which we want to use.

Now we will create a function that will be responsible to make DB backup.

const DB_NAME = 'users';

const BACKUP_PATH= path.join(__dirname, 'public', `${DB_NAME}.gzip`);

const  backUpDB = () => {
    const child = spawn('mongodump',[
        `--db=${DB_NAME}`,
        `--archive=${BACKUP_PATH}`,
        '--gzip'
    ]);
    child.stdout.on('data',(data)=>{
        console.log('stdoup',data)
    })
    child.stderr.on('data',(data)=>{
        console.log('stdoup',Buffer.from(data).toString())
    })
    child.on('error',(err)=>{
        console.log(err);
    })
    child.on('exit',(code,signal)=>{
        if(code){
            console.log('process exit with code', code)
        }else if(signal){
            console.log('process killed with signal',signal)
        }else{
            console.log("back up success");
        }
    })
}

Let's understand the above code.

So path.join method will join the two-path, __dirname will return the current directory of our location, and also we can provide directory name and file name in the argument.

Spawn will execute the command in a terminal, It takes the first argument as a command name and another argument will be an array of all parameters of a command. Here we just wrote the command of DB backup.

Now we will need to handle the output as well as the error which comes from the terminal or from the file itself.

So stdout will return the output if our command has been executed successfully, It will return a callback function and in the argument, it provides the data.

stderr will return the error if the command is not found or something wrong related to the command or terminal.

Now the third thing is the simple error that can occur in our file. We will also check the code and signal if the process is failed due to some issue.

Let's create a job that can run at a certain time interval and we will call our function there.

If you want to learn more about job scheduling then Click Here, This link is from one of my articles on a cron job.

Also we have used different package in this article if you went through my previous article, But Let me tell you something, It doesn't matter which package you use for a job. Just pick up any decent popular package which has a good rate of weekly downloads.

So following code will run the job at a given time.

// Scheduling the database backup every night
cron.schedule('00 00 00 * * *', () => backUpDB ());

Here schedule method expects arguments which is the time when we want to run the DB backup code.

To learn more about the combination of different times Click here .

So the final code will look like this.

const { spawn } = require('child_process');
const path = require('path');
const cron = require('node-cron');

const DB_NAME = 'users';

const BACKUP_PATH= path.join(__dirname, 'public', `${DB_NAME}.gzip`);

const  backUpDB = () => {
    const child = spawn('mongodump',[
        `--db=${DB_NAME}`,
        `--archive=${BACKUP_PATH}`,
        '--gzip'
    ]);
    child.stdout.on('data',(data)=>{
        console.log('stdoup',data)
    })
    child.stderr.on('data',(data)=>{
        console.log('stdoup',Buffer.from(data).toString())
    })
    child.on('error',(err)=>{
        console.log(err);
    })
    child.on('exit',(code,signal)=>{
        if(code){
            console.log('process exit with code', code)
        }else if(signal){
            console.log('process killed with signal',signal)
        }else{
            console.log("back up success");
        }
    })
}

// Scheduling the database backup every night
cron.schedule('00 00 00 * * *', () => backUpDB ());

So this was a simple article on how you can backup the database.

Note - You can backup any database by their corresponding commands, You just need to pass some arguments in the spawn.

Tip - You also can upload backup on any cloud like AWS S3 or google drive, cloudinary, or anything like that. You can check my another article to know more about uploading files in the cloud. https://blog.yashvant.in/uploaddownloaddelete-any-file-on-aws-s3-bucket-using-nodejs

Thanks :)