-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
57 lines (53 loc) · 1.89 KB
/
app.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
var backup = require('./includes/backup.js'),
restore = require('./includes/restore.js'),
debug = require('debug')('couchbackup'),
defaults = require('./includes/defaults.js').get(),
fs = require('fs');
var mergeDefaults = function(opts, defaults) {
for(i in defaults) {
if (!opts[i]) {
opts[i] = defaults[i];
}
}
return opts;
}
module.exports = {
backupStream: function(writeStream, opts, callback) {
opts = mergeDefaults(opts, defaults);
if (opts.COUCH_MODE === 'shallow') {
backup = require('./includes/shallowbackup.js');
}
return backup(opts.COUCH_URL, opts.COUCH_DATABASE, opts.COUCH_BUFFER_SIZE, opts.COUCH_PARALLELISM, opts.COUCH_LOG, opts.COUCH_RESUME, opts.OUTPUT)
.on('written', function(obj) {
debug(' backed up batch', obj.batch, ' docs: ', obj.total, 'Time', obj.time);
writeStream.write(JSON.stringify(obj.data) + '\n');
})
.on('writeerror', function(obj) {
debug('Error' + JSON.stringify(obj));
})
.on('writecomplete', function(obj) {
debug('Backup complete - written' + JSON.stringify(obj));
callback(null,obj);
});
},
restoreStream: function(readStream, opts, callback) {
opts = mergeDefaults(opts, defaults);
return restore(opts.COUCH_URL, opts.COUCH_DATABASE, opts.COUCH_BUFFER_SIZE, opts.COUCH_PARALLELISM, readStream)
.on('written', function(obj) {
debug(' written ', obj.total);
})
.on('writeerror', function(e) {
debug(' error', e);
})
.on('writecomplete', function(obj) {
debug('restore complete');
callback(null, obj);
});
},
backupFile: function(filename, opts, callback) {
return this.backupStream(fs.createWriteStream(filename), opts, callback);
},
restoreFile: function(filename, opts, callback) {
return this.restoreStream(fs.createReadStream(filename), opts, callback);
}
}