-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfresch-db.js
51 lines (43 loc) · 1.78 KB
/
fresch-db.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env node
const program = require("commander");
const dateformat = require("dateformat");
const log = require("./utils/log");
const setOptions = require("./utils/set-options");
const getConfig = require("./utils/get-config");
const checkConfig = require("./utils/check-config");
const rsync = require("./utils/rsync");
const ssh = require("./utils/ssh");
const run = require("./utils/run");
const requiredOpts = [ "db", "remote", "op" ];
const timestamp = dateformat(new Date(), "yyyymd_HM");
const dumpLocation = `/tmp/local_sync_${timestamp}.sql.gz`;
setOptions(program, requiredOpts);
program.parse(process.argv);
const alias = program.args[0];
const config = checkConfig(getConfig(program, alias), requiredOpts);
console.log();
// make dump
log("• Making database dump on remote ...", "info", true);
run(
ssh(
config,
`mysqldump -h ${config.remotedbhost} -u ${config.remotedbuser} --password="${config.remotedbpass}" ${config.remotedb} | gzip > ${dumpLocation}`
),
config.simulate
);
log("• Made database dump on remote", "success", true);
// sync dump
log("• Syncing database dump to local temp directory ...", "info", true);
run(rsync(config, dumpLocation, dumpLocation), config.simulate);
log("• Synced dump to local temp directory", "success", true);
// clean up dump on remote
log("• Cleaning up dump on remote ...", "info", true);
run(ssh(config, `rm ${dumpLocation}`), config.simulate);
log("• Cleaned up dump on remote", "success", true);
// import dump
log("• Importing dump to local database ...", "info", true);
run(
`pv -f ${dumpLocation} | gunzip | ${config.localdbbin} -h ${config.localdbhost} -u ${config.localdbuser} --password="${config.localdbpass}" ${config.localdb}`,
config.simulate
);
log("• Imported dump to local database", "success", true);