Last active
April 14, 2022 14:34
-
-
Save demirtasdurmus/1facffe9ab606eb1e85a1d2bf56439cc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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