-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup-restore
executable file
·83 lines (69 loc) · 1.85 KB
/
backup-restore
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env node
const path = require('path');
const fs = require('fs');
const Restorer = require('../lib/Restorer');
const package = require('../package.json');
const utils = require('../lib/utils');
const LOCK_FILE = path.join(__dirname, '.backup-restore.lock');
function usage() {
console.log(`${package.name} version ${package.version}
Usage: ${__filename} [OPTIONS] REMOTE_DIR_OR_FILE
Options:
--output OUTPUT_DIR_OR_FILE [required]
--test
--ignore-lock
--yes
--verbose
--dry
--help
Examples:
${__filename} --output /foo/ /remote/dir/file1.txt
Will restore s3://BUCKET/remote/dir/file1.txt to /foo/remote/dir/file1.txt
${__filename} --output /foo/ /remote/
Will restore s3://BUCKET/remote/dir/file1.txt to /foo/remote/dir/file1.txt
Will restore s3://BUCKET/remote/other/file2.txt to /foo/remote/other/file2.txt
`);
}
function main() {
if (fs.existsSync(LOCK_FILE) && !utils.hasFlag('ignore-lock')) {
utils.log(
`Another instance of the ${package.name} is already running or has not properly terminated. Remove lock file to continue.`,
);
return;
}
fs.writeFileSync(LOCK_FILE, '');
utils.log(`${package.name} version ${package.version}`);
if (utils.DRY_RUN) {
utils.log('This is a DRY run! No changes/uploads will be made.');
}
const input = process.argv.pop();
const output = path.resolve(utils.getOption('output'));
let finalResult;
const restorer = new Restorer();
restorer
.start(input, output)
.then(
result => {
utils.debug('Restorer result:', result);
finalResult = result;
},
err => {
utils.log('Restorer error:', err);
},
)
.then(() => restorer.finish())
.then(finish)
.then(() => {
if (finalResult.includes('FAIL')) {
process.exit(1);
}
});
}
function finish() {
fs.unlinkSync(LOCK_FILE);
}
if (utils.hasFlag('help') || !utils.getOption('output')) {
usage();
} else {
main();
}