Skip to content

Instantly share code, notes, and snippets.

@dmdboi
Created September 11, 2020 22:52
Show Gist options
  • Save dmdboi/2b294d64adfaa022a993acd79c5086f5 to your computer and use it in GitHub Desktop.
Save dmdboi/2b294d64adfaa022a993acd79c5086f5 to your computer and use it in GitHub Desktop.
A node.js script to backup a MySQL database using MySQLDump. Can be added to a node-cron timer to automate backups.
var fs = require('fs');
var spawn = require('child_process').spawn;
var wstream = fs.createWriteStream('dumpfilename.sql'); //Name of SQL dump file
var mysqldump = spawn('mysqldump', [
'-u',
'DB_USER',
'-p DB_PASSWORD',
'DB_NAME'
]);
mysqldump
.stdout
.pipe(wstream)
.on('finish', function () {
console.log('Completed')
})
.on('error', function (err) {
console.log(err)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment