Skip to content

Instantly share code, notes, and snippets.

@demirtasdurmus
Last active April 14, 2022 14:34
Show Gist options
  • Select an option

  • Save demirtasdurmus/1facffe9ab606eb1e85a1d2bf56439cc to your computer and use it in GitHub Desktop.

Select an option

Save demirtasdurmus/1facffe9ab606eb1e85a1d2bf56439cc to your computer and use it in GitHub Desktop.
const backupDatabase = () => {
// extract credentials from .env
const dbName = process.env.DB_NAME;
const dbPass = process.env.DB_PASS;
const dbHost = process.env.DB_HOST;
const dbUser = process.env.DB_USER;
const dbPort = process.env.DB_PORT;
const format = "backup"; // sql/backup/dump
// create a custom backup file name with date info
const date = new Date();
const currentDate = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}-${date.getHours()}-${date.getMinutes()}`;
const backupFilePath = `/Users/<username>/<path_to_dir>/${dbName}-${currentDate}.${format}`;
// execute node child process(exec)
exec(`sh ./backup.sh ${dbPass} ${dbHost} ${dbUser} ${dbPort} ${dbName} ${backupFilePath}`,
(error, stdout, stderr) => {
if (error) {
return console.error(`exec error: ${error}`);
};
if (stderr) {
return console.error(`stderr: ${stderr}`);
};
console.log(`Created a backup of ${dbName} at ${date.toLocaleString()} successfully: ${stdout}`);
})
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment