Created
March 19, 2020 12:06
-
-
Save osmanyz/ac179e56ab590793feeb88584eb0b38a to your computer and use it in GitHub Desktop.
This only works with mysql via nodejs.
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 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); | |
| }); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Your console code:
node mysql.backup.jsIf you want you can check it.
https://nodejs.org/api/child_process.html#child_process_spawning_bat_and_cmd_files_on_windows
https://stackoverflow.com/a/48035109