Skip to content

Instantly share code, notes, and snippets.

@osmanyz
Created March 19, 2020 12:06
Show Gist options
  • Select an option

  • Save osmanyz/ac179e56ab590793feeb88584eb0b38a to your computer and use it in GitHub Desktop.

Select an option

Save osmanyz/ac179e56ab590793feeb88584eb0b38a to your computer and use it in GitHub Desktop.
This only works with mysql via nodejs.
const fs = require('fs');
const path = require('path');
const spawn = require('child_process').spawn;
/*
const date = (new Date()).toLocaleDateString("tr-TR", {
year: 'numeric',
month: 'numeric',
day: 'numeric'
});
const wstream = fs.createWriteStream(path.join(__dirname, `/backup-files/${date}.sql`));
*/
const wstream = fs.createWriteStream('backup-file.sql');
// const config = require('./config');
const config = {
db: {
user: 'username',
password: '123',
database: 'database',
}
};
const dump = spawn('mysqldump', [
'-u',
config.db.user,
`-p${config.db.password}`,
config.db.database,
]);
dump
.stdout
.pipe(wstream)
.on('finish', function () {
console.log('Backup completed.');
})
.on('error', function (err) {
console.log(err);
});
@osmanyz
Copy link
Author

osmanyz commented Mar 19, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment