-
Notifications
You must be signed in to change notification settings - Fork 0
/
controllers.js
172 lines (145 loc) · 6.24 KB
/
controllers.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const extract = require('extract-zip')
const resolve = require('path').resolve
const child_process = require('child_process')
const AdmZip = require("adm-zip");
const mongotools = require('./mongotools')
const firebaseStorage = require('./storage');
const {InternalServerError500, BadRequestError400, NotFoundError404} = require("./response/errorTypes");
const {CreatedResponse201, OKResponse200} = require("./response/successTypes");
const backupController = async (callback) => {
console.log('backup command initiated')
let folderName = new Date().getTime()
console.log('fetching database...')
console.log(mongotools.mongodumpPath+" "+"--out=backup/"+folderName+" "+process.env.MONGODB_URI_PROD)
var child = child_process.spawnSync(mongotools.mongodumpPath, ['--out=backup/' + folderName,
process.env.MONGODB_URI_PROD], {encoding: 'utf8'})
console.log('Process finished.')
if (child.error) {
console.log('ERROR: ', child.error)
console.log("RESPOND CALLED on child process")
return new InternalServerError500('Error in spawning child process',{error:child.error})
}
console.log('stdout: ', child.stdout)
console.log('stderr: ', child.stderr)
console.log('exit code: ', child.status)
console.log('fetching database completed.')
console.log(`creating zip...`);
const zip = new AdmZip();
zip.addLocalFolder('./backup/' + folderName);
zip.writeZip('backup/' + folderName + '.zip');
console.log(`creating zip successful`);
console.log('uploading to cloud...')
await firebaseStorage.uploadFile(`backup/${folderName}.zip`, `backup/${folderName}.zip`)
console.log('uploading to cloud completed')
return new CreatedResponse201('Successfully created backup',{
output: child.stdout,
error: child.stderr,
childStatus: child.status,
time: folderName
})
}
const deleteController = async (argv) => {
console.log('delete command initiated')
console.log('time: ', argv.time)
console.log('fetching backups from cloud...')
let backupList = await firebaseStorage.getBackupList()
console.log('fetching backups from cloud completed')
if (!backupList.includes(argv.time)) {
console.log('backup with specified timestamp not found')
return new NotFoundError404('backup with specified timestamp not found')
}
console.log('backup found with specified timestamp')
console.log('deleting backup...')
await firebaseStorage.deleteFile(`backup/${argv.time}.zip`)
console.log('successfully deleted backup')
return new OKResponse200('successfully deleted backup')
}
const listController = async () => {
console.log('list command initiated')
console.log('fetching backups from cloud...')
let backupList = await firebaseStorage.getBackupList()
backupList.sort().reverse()
console.log('fetching backups from cloud completed')
console.log('found backups:')
backupList.forEach((timeStamp) => {
console.log(new Date(timeStamp).toLocaleString(), ' (timestamp: ', timeStamp, ')')
})
return new OKResponse200('Successfully fetched list of backups',{backups: backupList})
}
const restoreController = async (argv) => {
console.log('restore command initiated')
console.log('time: ', argv.time)
console.log('restore to production: ', argv.production)
let mongoURI = process.env.MONGODB_URI_TEST
if (argv.production === true) {
mongoURI = process.env.MONGODB_URI_PROD
}
console.log('fetching backups from cloud...')
let backupList = await firebaseStorage.getBackupList()
console.log('fetching backups from cloud completed')
if (!backupList.includes(argv.time)) {
console.log('backup with specified timestamp not found')
return new NotFoundError404('backup with specified timestamp not found')
}
console.log('backup found with specified timestamp')
console.log('downloading backup...')
await firebaseStorage.downloadFile(`backup/${argv.time}.zip`, `./backup/${argv.time}.zip`)
console.log('backup downloaded')
console.log('extracting backup...')
const targetPath = `./backup/${argv.time}.zip`
const unpackPath = `./backup/${argv.time}`
const resolvedUnpackPath = resolve(unpackPath)
console.log('extracting ' + targetPath + ' to ' + resolvedUnpackPath)
await extract(targetPath, {dir: resolvedUnpackPath})
console.log('extracting backup completed')
console.log('restoring backup...')
const child = child_process.spawnSync(mongotools.mongorestorePath, ['--drop', `--dir=backup/${argv.time}/Badhan`, mongoURI], {encoding: 'utf8'})
console.log('Process finished.')
if (child.error) {
console.log('ERROR: ', child.error)
return new InternalServerError500('Child process spawnsync failed')
}
console.log('stdout: ', child.stdout)
console.log('stderr: ', child.stderr)
console.log('exit code: ', child.status)
return new OKResponse200('Backup successfully restored',{
childSpawn: {
output: child.stdout,
error: child.stderr,
childStatus: child.status,
},
argv
})
}
const pruneController = async () => {
console.log("prune command initiated")
console.log('fetching backup list from cloud...')
let backupList = await firebaseStorage.getBackupList()
console.log('fetching backup list from cloud completed')
backupList = backupList.reverse().slice(3)
for (let i = 0; i < backupList.length; i++) {
console.log(`deleting backup ${backupList[i]}`)
await firebaseStorage.deleteFile(`backup/${backupList[i]}.zip`)
}
return new OKResponse200('Deleted all older databases')
}
const restoreLatestController = async (argv) => {
console.log("restoreLatest command initiated")
console.log('fetching backup list from cloud...')
let backupList = await firebaseStorage.getBackupList()
console.log('fetching backup list from cloud completed')
if (backupList.length === 0) {
console.log("No backups found")
return
}
argv.time = backupList[backupList.length - 1]
await restoreController(argv)
}
module.exports = {
backupController,
deleteController,
listController,
restoreController,
pruneController,
restoreLatestController
}