azzaky9 / cron-job-backup-weekly

Schedule Backup Weekly with Node Js and shell Script

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cron Job Backup Database MySql Weely

(You can also using another Sql Database but you need extra configuration)

The purpose of this code is for schedule the backup your data you served with Sql DBMS, this code have low memory usage i'm not use extra library for serving Web Server like (Express or Something Else..) instead of using the library i prefer doing the same thing just only using http:server from Node Js and for executing the shell script from node js i use child_process.

What case fit for this Cron Jobs Worker ?

  • Your app served in VPS
  • Your app using MySql for DBMS
  • Low Library Usage

DEFAULT CONFIG

  • Day = 'Friday'
  • Times = 23:00
  • Schedule = 'Weekly'

Shell Script Config

// make your own .env 
// Location ./src/backup.js

const host = process.env.DB_HOST;
const username = process.env.DB_USERNAME;
const password = process.env.DB_PASSWORD;
const dbName = process.env.DB_NAME;
const backupPath = process.env.BACKUP_PATH;

Default Backup Script

# Location ./src/backup.js

mysqldump -u ${username} -p${password} ${dbName} > ${backupPath}/${date.getFullYear()}-${date.getMonth()}-${date.getDay()}At[${date.getHours()}:${date.getMinutes()}]-backup-${host}.sql

Default Executable Cron Jobs.

// Location main.js

new CronJob(
    "* * 23 * * 5", // Make your own configuration
    function () {
      console.log("============== Cron Fire =============");
      executeBackup();
    },
    "Cron sucessfully execute without problem.",
    true,
   "America/Los_Angeles"
)

Refference Library

Technolgy

  • Node Js v20.12.2
  • Package Manager (PNPM) v9.0.6

About

Schedule Backup Weekly with Node Js and shell Script


Languages

Language:JavaScript 100.0%