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');
|
23 | const backupDebug = debug('couchbackup:backup');
|
24 | const backupBatchDebug = debug('couchbackup:backup:batch');
|
25 |
|
26 | backupDebug.enabled = true;
|
27 |
|
28 | const program = parser.parseBackupArgs();
|
29 |
|
30 | const databaseUrl = cliutils.databaseUrl(program.url, program.db);
|
31 | const opts = {
|
32 | bufferSize: program.bufferSize,
|
33 | log: program.log,
|
34 | mode: program.mode,
|
35 | parallelism: program.parallelism,
|
36 | requestTimeout: program.requestTimeout,
|
37 | resume: program.resume,
|
38 | iamApiKey: program.iamApiKey,
|
39 | iamTokenUrl: program.iamTokenUrl
|
40 | };
|
41 |
|
42 |
|
43 | console.error('='.repeat(80));
|
44 | console.error('Performing backup on ' + databaseUrl.replace(/\/\/.+@/g, '//****:****@') + ' using configuration:');
|
45 | console.error(JSON.stringify(opts, null, 2).replace(/"iamApiKey": "[^"]+"/, '"iamApiKey": "****"'));
|
46 | console.error('='.repeat(80));
|
47 |
|
48 | backupBatchDebug.enabled = !program.quiet;
|
49 |
|
50 | let ws = process.stdout;
|
51 |
|
52 |
|
53 | if (program.output) {
|
54 | let flags = 'w';
|
55 | if (program.log && program.resume) {
|
56 | flags = 'a';
|
57 | }
|
58 | const fd = fs.openSync(program.output, flags);
|
59 | ws = fs.createWriteStream(null, { fd: fd });
|
60 | }
|
61 |
|
62 | backupDebug('Fetching all database changes...');
|
63 |
|
64 | return couchbackup.backup(
|
65 | databaseUrl,
|
66 | ws,
|
67 | opts,
|
68 | error.terminationCallback
|
69 | ).on('changes', function(batch) {
|
70 | backupBatchDebug('Total batches received:', batch + 1);
|
71 | }).on('written', function(obj) {
|
72 | backupBatchDebug('Written batch ID:', obj.batch, 'Total document revisions written:', obj.total, 'Time:', obj.time);
|
73 | }).on('error', function(e) {
|
74 | backupDebug('ERROR', e);
|
75 | }).on('finished', function(obj) {
|
76 | backupDebug('Finished - Total document revisions written:', obj.total);
|
77 | });
|