UNPKG

2.56 kBJavaScriptView Raw
1#!/usr/bin/env node
2// Copyright © 2017, 2021 IBM Corp. All rights reserved.
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15'use strict';
16
17const error = require('../includes/error.js');
18const fs = require('fs');
19const cliutils = require('../includes/cliutils.js');
20const couchbackup = require('../app.js');
21const parser = require('../includes/parser.js');
22const debug = require('debug');
23const backupDebug = debug('couchbackup:backup');
24const backupBatchDebug = debug('couchbackup:backup:batch');
25
26backupDebug.enabled = true;
27
28const program = parser.parseBackupArgs();
29
30const databaseUrl = cliutils.databaseUrl(program.url, program.db);
31const 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// log configuration to console
43console.error('='.repeat(80));
44console.error('Performing backup on ' + databaseUrl.replace(/\/\/.+@/g, '//****:****@') + ' using configuration:');
45console.error(JSON.stringify(opts, null, 2).replace(/"iamApiKey": "[^"]+"/, '"iamApiKey": "****"'));
46console.error('='.repeat(80));
47
48backupBatchDebug.enabled = !program.quiet;
49
50let ws = process.stdout;
51
52// open output file
53if (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
62backupDebug('Fetching all database changes...');
63
64return 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});