DEV Community

Discussion on: Schedule the DB backup cron job with nodejs

Collapse
 
cuu508 profile image
Pēteris Caune

Hello, interesting! A couple of questions:

A. How to run this? Who/what will run index.js? Would there also be a systemd service to launch it or something like that?

B. express.js is a web application framework. Isn't it a bad idea to run a web server on a database host, just for the sake of making backups?

C. For this task, I would normally:

  • crontab -e
  • add the mongodump command, save and exit the editor
  • done

What's the added benefit of using the combination of JS script, express.js and node-cron for this?

Collapse
 
yashvant profile image
yashvant vala • Edited

Hey,
A. you can run index.js with node index.js command or you can specify the command in package.json file.

B. I think it is not bad idea to use express.js, I have seen many web apps which does it using express js. It would automate things like uploading to s3 server and anywhere you want it to save. The idea behind this is to how this process works.

C. Yeah you can but you are doing it manually If I am not wrong, By this process it will get job done for us without doing it repetitively.

There are some use cases where we need our back-end to do job for us. It doesn't matter what back-end you are using or what package to schedule job you are using.

Thanks for the comment, Hope it clears you doubts. If you still have then we can discuss more about this.

Collapse
 
cuu508 profile image
Pēteris Caune

A. OK, then who/what runs "node index.js"? Would there also be a systemd service to launch it or something like that?

B. I didn't mean express.js itself is bad. But I do think it is a questionable practice to run a web server on a database host. Unless it is a developer machine where you run all services on the same machine for convenience.

C. You can script cron job creation. The "crontab -e" lets you edit cron job definitions interactively. From a script, you can run "crontab /path/to/my/cronjob.definitions" and it will replace user's current scripts with whatever is in cronjob.definitions.

I see the value of node-cron – it could run some business logic on schedule, for example. I'm not sure I'd use it for backing up databases though!

Thread Thread
 
yashvant profile image
yashvant vala • Edited

Hi
A. Index.js can be launched by manually if you just want to learn something to want to run in local or want to do any task only once, In ideal scenario it will be run by system when web server starts and it will be constantly listening.

B. Yes you are right but some applications may have web server and db host on single instance and i haven't covered any production ready or best practices to get things done in this article, but surely it is not to hard to search i guess.

C. I haven't tried crontab but the goal is to run it in certein time after setting it one time in code and avoid manual task by user.

Hope my answer make sense :)