-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathbackup.js
More file actions
72 lines (59 loc) · 2.26 KB
/
backup.js
File metadata and controls
72 lines (59 loc) · 2.26 KB
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
var { S3Client } = require('@aws-sdk/client-s3');
var { Upload } = require('@aws-sdk/lib-storage');
var Dyno = require('@mapbox/dyno');
var stream = require('stream');
var zlib = require('zlib');
module.exports = function(config, done) {
var primary = Dyno(config);
var s3Client = new S3Client({ region: config.region });
var log = config.log || console.log;
var scanOpts = Object.prototype.hasOwnProperty.call(config, 'segment') && config.segments ?
{ Segment: config.segment, TotalSegments: config.segments } : undefined;
if (config.backup)
if (!config.backup.bucket || !config.backup.prefix || !config.backup.jobid)
return done(new Error('Must provide a bucket, prefix and jobid for backups'));
var index = !isNaN(parseInt(config.segment)) ? config.segment.toString() : 0;
var key = [config.backup.prefix, config.backup.jobid, index].join('/');
var count = 0;
var size = 0;
var stringify = new stream.Transform({ objectMode: true });
stringify._transform = function(record, enc, callback) {
var line = Dyno.serialize(record);
setImmediate(function() {
stringify.push(line + '\n');
count++;
callback();
});
};
var data = primary.scanStream(scanOpts)
.on('error', next)
.pipe(stringify)
.on('error', next)
.pipe(zlib.createGzip());
log('[segment %s] Starting backup job %s of %s', index, config.backup.jobid, config.region + '/' + config.table);
const upload = new Upload({
client: s3Client,
params: {
Bucket: config.backup.bucket,
Key: key,
Body: data
}
});
upload.on('httpUploadProgress', function(progress) {
log('[segment %s] Uploaded %s bytes', index, progress.loaded);
size = progress.loaded;
});
upload.done()
.then(() => {
log('[segment %s] Uploaded dynamo backup to s3://%s/%s', index, config.backup.bucket, key);
log('[segment %s] Wrote %s items to backup', index, count);
next();
})
.catch(err => {
next(err);
});
function next(err) {
if (err) return done(err);
done(null, { size: size, count: count });
}
};