-
Notifications
You must be signed in to change notification settings - Fork 3
/
backup.js
77 lines (74 loc) · 2.59 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
var fs = require('fs'),
path = require('path'),
knox = require('knox'),
request = require('request'),
JSZip = require('jszip'),
zip = new JSZip(),
async = require('async'),
moment = require('moment');
function backup(config) {
var urlStem = 'http://tiddlyspace.com/bags/',
options = {
headers: {
'X-ControlView': false
},
json: true,
auth: {
user: config.username,
pass: config.password
}
};
// step 1 - go get the tiddlydata
async.each(config.bags, function(bag, callback) {
options.url = urlStem + bag + '/tiddlers.json?fat=1';
request.get(options, function(err, response, json) {
if(err) {
throw err;
}
zip.file(bag + '.json', JSON.stringify(json));
callback();
});
}, function(err) {
if(err) {
throw err;
}
// step 2 - zip it up
var buffer = zip.generate({type: 'nodebuffer'}),
timestamp = moment().format('YYYY-MM-DD-HH-mm-ss'),
fileExtension = '.zip',
fileName = 'backup-'+timestamp+fileExtension,
filePath = path.join(__dirname, fileName);
fs.writeFile(filePath, buffer, function(err) {
if (err) {
throw err;
}
console.log('written zip file '+fileName);
console.log('zip size', buffer.length);
// step 4 - if s3 creds exist, go put the zip to s3
if(config.s3_api_key) {
var client = knox.createClient({
key: config.s3_api_key,
secret: config.s3_api_secret,
bucket: config.s3_bucket,
region: config.s3_region
}),
s = client.putFile(fileName, '/'+fileName, function(err, res){
// Always either do something with `res` or at least call `res.resume()`.
if(err) {
throw err;
}
res.resume();
if(res.statusCode!==200) {
throw new Error('S3 returned bad statusCode: '+res.statusCode+', for url: '+res.req.url);
}
});
s.on('progress', function() {
console.log('s3 upload progress', arguments);
});
}
});
});
}
module.exports = {
backup: backup
};