1 | #!/usr/bin/env node
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | 'use strict';
|
16 |
|
17 | const error = require('../includes/error.js');
|
18 | const fs = require('fs');
|
19 | const cliutils = require('../includes/cliutils.js');
|
20 | const couchbackup = require('../app.js');
|
21 | const parser = require('../includes/parser.js');
|
22 | const debug = require('debug')('couchbackup:backup');
|
23 | debug.enabled = true;
|
24 |
|
25 | var program = parser.parseBackupArgs();
|
26 |
|
27 | var databaseUrl = cliutils.databaseUrl(program.url, program.db);
|
28 | var opts = {
|
29 | bufferSize: program.bufferSize,
|
30 | log: program.log,
|
31 | mode: program.mode,
|
32 | parallelism: program.parallelism,
|
33 | requestTimeout: program.requestTimeout,
|
34 | resume: program.resume,
|
35 | iamApiKey: program.iamApiKey,
|
36 | iamTokenUrl: program.iamTokenUrl
|
37 | };
|
38 |
|
39 |
|
40 | console.error('='.repeat(80));
|
41 | console.error('Performing backup on ' + databaseUrl.replace(/\/\/.+@/g, '//****:****@') + ' using configuration:');
|
42 | console.error(JSON.stringify(opts, null, 2).replace(/"iamApiKey": "[^"]+"/, '"iamApiKey": "****"'));
|
43 | console.error('='.repeat(80));
|
44 |
|
45 | var ws = process.stdout;
|
46 |
|
47 |
|
48 | if (program.output) {
|
49 | var flags = 'w';
|
50 | if (program.log && program.resume) {
|
51 | flags = 'a';
|
52 | }
|
53 | const fd = fs.openSync(program.output, flags);
|
54 | ws = fs.createWriteStream(null, { fd: fd });
|
55 | }
|
56 |
|
57 | debug('Fetching all database changes...');
|
58 |
|
59 | return couchbackup.backup(
|
60 | databaseUrl,
|
61 | ws,
|
62 | opts,
|
63 | error.terminationCallback
|
64 | ).on('changes', function(batch) {
|
65 | debug('Total batches received:', batch + 1);
|
66 | }).on('written', function(obj) {
|
67 | debug('Written batch ID:', obj.batch, 'Total document revisions written:', obj.total, 'Time:', obj.time);
|
68 | }).on('error', function(e) {
|
69 | debug('ERROR', e);
|
70 | }).on('finished', function(obj) {
|
71 | debug('Finished - Total document revisions written:', obj.total);
|
72 | });
|