UNPKG

2.38 kBJavaScriptView Raw
1#!/usr/bin/env node
2// Copyright © 2017, 2018 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')('couchbackup:backup');
23debug.enabled = true;
24
25var program = parser.parseBackupArgs();
26
27var databaseUrl = cliutils.databaseUrl(program.url, program.db);
28var 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// log configuration to console
40console.error('='.repeat(80));
41console.error('Performing backup on ' + databaseUrl.replace(/\/\/.+@/g, '//****:****@') + ' using configuration:');
42console.error(JSON.stringify(opts, null, 2).replace(/"iamApiKey": "[^"]+"/, '"iamApiKey": "****"'));
43console.error('='.repeat(80));
44
45var ws = process.stdout;
46
47// open output file
48if (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
57debug('Fetching all database changes...');
58
59return 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});