-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.js
68 lines (59 loc) · 1.81 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
var Toolkit = require('popit-toolkit');
var fs = require('fs');
var Q = require('q');
function downloadCollection(toolkit, instanceName, collectionName) {
return function(){
return Q.promise(function(resolve, reject, notify) {
console.log("LOADING", collectionName)
toolkit.loadAllItems(collectionName).then(
function(items) {
try {
items.sort(function(a, b) {
return a.id > b.id ? 1 : -1
})
if("persons" == collectionName || "organizations" == collectionName){
items.forEach(function(item){
if(item.memberships) delete item.memberships;
})
}
fs.writeFileSync('data/' + instanceName + '-' + collectionName + '.json', JSON.stringify(items, null, 2));
console.log('wrote ' + collectionName + '', items.length)
resolve();
} catch (ex) {
console.log('error', ex);
reject();
}
},
function(err) {
console.log('error', err);
reject();
},
function(progress) {
notify(progress);
}
);
});
}
}
function runBackup(instanceName) {
var toolkit = Toolkit({
host: instanceName + '.popit.mysociety.org'
});
Q.fcall(function() {})
.then(downloadCollection(toolkit, instanceName, 'persons'))
.then(downloadCollection(toolkit, instanceName, 'organizations'))
.then(downloadCollection(toolkit, instanceName, 'memberships'))
.done(function(){
console.log("LISTO")
});
}
function runProgram(argv) {
if (argv.length != 3) {
console.log("Usage: node backup.js <instanceName>")
} else {
var instanceName = argv[2];
console.log("Instance: " + instanceName);
runBackup(instanceName)
}
}
runProgram(process.argv);