-
Notifications
You must be signed in to change notification settings - Fork 6
/
backup.js
81 lines (71 loc) · 2.23 KB
/
backup.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
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
const child_process = require('child_process')
const { promisify } = require('util')
const { username, password } = require('./.mongorc.json')
const spawn = child_process.spawn
const exec = promisify(child_process.exec)
const main = async () => {
const mongoDumpPath = '/root/backup'
await mongoDump(mongoDumpPath)
const date = new Date().toISOString().split(".")[0]
const backupFile = `dump_${date}.tar.gz`
const backupFilePath = '/root/backups'
await tarCompress(backupFile, backupFilePath, mongoDumpPath)
await megaPut(backupFile, backupFilePath)
await exec(`rm ${backupFilePath}/${backupFile}`)
}
const mongoDump = (mongoDumpPath) =>
new Promise((resolve, reject) => {
const spawnMongoDump = spawn(
'docker',
[ 'run', '--rm', '--network=webproxy', '--link', 'mongodb:mongodb', '-v', `${mongoDumpPath}:/backup`, 'mongo', 'bash', '-c', 'mongodump --out /backup --host mongodb' ]
)
spawnMongoDump.stdout.on('data', data => {
console.info(String(data))
})
spawnMongoDump.stderr.on('data', data => {
console.info(String(data))
})
spawnMongoDump.on('close', code => {
if (code === 0) resolve()
reject(code)
})
})
const tarCompress = (backupFile, backupFilePath, mongoDumpPath) =>
new Promise((resolve, reject) => {
const spawnTarCompress = spawn(
'tar',
[ '-zcvf', `${backupFilePath}/${backupFile}`, mongoDumpPath ]
)
spawnTarCompress.stdout.on('data', data => {
console.info(String(data))
})
spawnTarCompress.stderr.on('data', data => {
console.info(String(data))
})
spawnTarCompress.on('close', code => {
if (code === 0) resolve()
reject(code)
})
})
const megaPut = (backupFile, backupFilePath) =>
new Promise((resolve, reject) => {
const spawnMegaPut = spawn(
'megaput',
[ '--path', '/Root/smmdb', '-u', username, '-p', password, `${backupFilePath}/${backupFile}` ]
)
spawnMegaPut.stdout.on('data', data => {
console.info(String(data))
})
spawnMegaPut.stderr.on('data', data => {
console.info(String(data))
})
spawnMegaPut.on('close', code => {
if (code === 0) resolve()
reject(code)
})
})
try {
main()
} catch (err) {
console.error(err)
}