@cloudant/couchbackup 2.9.17 → 2.10.0-206
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -0
- package/app.js +253 -268
- package/bin/couchbackup.bin.js +1 -3
- package/bin/couchrestore.bin.js +2 -4
- package/includes/allDocsGenerator.js +53 -0
- package/includes/backup.js +103 -247
- package/includes/backupMappings.js +260 -0
- package/includes/config.js +10 -9
- package/includes/error.js +42 -44
- package/includes/liner.js +134 -23
- package/includes/logfilegetbatches.js +25 -60
- package/includes/logfilesummary.js +41 -71
- package/includes/parser.js +3 -3
- package/includes/request.js +95 -106
- package/includes/restore.js +45 -14
- package/includes/restoreMappings.js +141 -0
- package/includes/spoolchanges.js +57 -79
- package/includes/transforms.js +378 -0
- package/package.json +5 -8
- package/includes/change.js +0 -41
- package/includes/shallowbackup.js +0 -80
- package/includes/writer.js +0 -164
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloudant/couchbackup",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.10.0-206",
|
|
4
4
|
"description": "CouchBackup - command-line backup utility for Cloudant/CouchDB",
|
|
5
5
|
"homepage": "https://github.com/IBM/couchbackup",
|
|
6
6
|
"repository": {
|
|
@@ -24,10 +24,8 @@
|
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
26
|
"@ibm-cloud/cloudant": "0.8.3",
|
|
27
|
-
"async": "3.2.5",
|
|
28
27
|
"commander": "12.0.0",
|
|
29
|
-
"debug": "4.3.4"
|
|
30
|
-
"tmp": "0.2.1"
|
|
28
|
+
"debug": "4.3.4"
|
|
31
29
|
},
|
|
32
30
|
"peerDependencies": {
|
|
33
31
|
"ibm-cloud-sdk-core": "^4.1.4",
|
|
@@ -40,7 +38,7 @@
|
|
|
40
38
|
"couchrestore": "bin/couchrestore.bin.js"
|
|
41
39
|
},
|
|
42
40
|
"devDependencies": {
|
|
43
|
-
"eslint": "8.
|
|
41
|
+
"eslint": "8.57.0",
|
|
44
42
|
"eslint-config-semistandard": "17.0.0",
|
|
45
43
|
"eslint-config-standard": "17.1.0",
|
|
46
44
|
"eslint-plugin-header": "3.1.1",
|
|
@@ -50,10 +48,9 @@
|
|
|
50
48
|
"eslint-plugin-promise": "6.1.1",
|
|
51
49
|
"http-proxy": "1.18.1",
|
|
52
50
|
"mocha": "10.3.0",
|
|
53
|
-
"nock": "13.5.
|
|
51
|
+
"nock": "13.5.4",
|
|
54
52
|
"tail": "2.2.6",
|
|
55
|
-
"uuid": "9.0.1"
|
|
56
|
-
"lodash": "4.17.21"
|
|
53
|
+
"uuid": "9.0.1"
|
|
57
54
|
},
|
|
58
55
|
"scripts": {
|
|
59
56
|
"lint": "eslint --ignore-path .gitignore .",
|
package/includes/change.js
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
// Copyright © 2017 IBM Corp. All rights reserved.
|
|
2
|
-
//
|
|
3
|
-
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
// you may not use this file except in compliance with the License.
|
|
5
|
-
// You may obtain a copy of the License at
|
|
6
|
-
//
|
|
7
|
-
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
//
|
|
9
|
-
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
// See the License for the specific language governing permissions and
|
|
13
|
-
// limitations under the License.
|
|
14
|
-
'use strict';
|
|
15
|
-
|
|
16
|
-
// stolen from http://strongloop.com/strongblog/practical-examples-of-the-new-node-js-streams-api/
|
|
17
|
-
const stream = require('stream');
|
|
18
|
-
|
|
19
|
-
module.exports = function(onChange) {
|
|
20
|
-
const change = new stream.Transform({ objectMode: true });
|
|
21
|
-
|
|
22
|
-
change._transform = function(line, encoding, done) {
|
|
23
|
-
let obj = null;
|
|
24
|
-
|
|
25
|
-
// one change per line - remove the trailing comma
|
|
26
|
-
line = line.trim().replace(/,$/, '');
|
|
27
|
-
|
|
28
|
-
// extract thee last_seq at the end of the changes feed
|
|
29
|
-
if (line.match(/^"last_seq":/)) {
|
|
30
|
-
line = '{' + line;
|
|
31
|
-
}
|
|
32
|
-
try {
|
|
33
|
-
obj = JSON.parse(line);
|
|
34
|
-
} catch (e) {
|
|
35
|
-
}
|
|
36
|
-
onChange(obj);
|
|
37
|
-
done();
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
return change;
|
|
41
|
-
};
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
// Copyright © 2017, 2022 IBM Corp. All rights reserved.
|
|
2
|
-
//
|
|
3
|
-
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
// you may not use this file except in compliance with the License.
|
|
5
|
-
// You may obtain a copy of the License at
|
|
6
|
-
//
|
|
7
|
-
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
//
|
|
9
|
-
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
// See the License for the specific language governing permissions and
|
|
13
|
-
// limitations under the License.
|
|
14
|
-
'use strict';
|
|
15
|
-
|
|
16
|
-
const async = require('async');
|
|
17
|
-
const error = require('./error.js');
|
|
18
|
-
const events = require('events');
|
|
19
|
-
|
|
20
|
-
module.exports = function(db, options) {
|
|
21
|
-
const ee = new events.EventEmitter();
|
|
22
|
-
const start = new Date().getTime();
|
|
23
|
-
let batch = 0;
|
|
24
|
-
let hasErrored = false;
|
|
25
|
-
let startKey = null;
|
|
26
|
-
let total = 0;
|
|
27
|
-
|
|
28
|
-
async.doUntil(
|
|
29
|
-
function(callback) {
|
|
30
|
-
// Note, include_docs: true is set automatically when using the
|
|
31
|
-
// fetch function.
|
|
32
|
-
const opts = { db: db.db, limit: options.bufferSize, includeDocs: true };
|
|
33
|
-
|
|
34
|
-
// To avoid double fetching a document solely for the purposes of getting
|
|
35
|
-
// the next ID to use as a startKey for the next page we instead use the
|
|
36
|
-
// last ID of the current page and append the lowest unicode sort
|
|
37
|
-
// character.
|
|
38
|
-
if (startKey) opts.startKey = `${startKey}\0`;
|
|
39
|
-
db.service.postAllDocs(opts).then(response => {
|
|
40
|
-
const body = response.result;
|
|
41
|
-
if (!body.rows) {
|
|
42
|
-
ee.emit('error', new error.BackupError(
|
|
43
|
-
'AllDocsError', 'ERROR: Invalid all docs response'));
|
|
44
|
-
callback();
|
|
45
|
-
} else {
|
|
46
|
-
if (body.rows.length < opts.limit) {
|
|
47
|
-
startKey = null; // last batch
|
|
48
|
-
} else {
|
|
49
|
-
startKey = body.rows[opts.limit - 1].id;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const docs = [];
|
|
53
|
-
body.rows.forEach(function(doc) {
|
|
54
|
-
docs.push(doc.doc);
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
if (docs.length > 0) {
|
|
58
|
-
ee.emit('received', {
|
|
59
|
-
batch: batch++,
|
|
60
|
-
data: docs,
|
|
61
|
-
length: docs.length,
|
|
62
|
-
time: (new Date().getTime() - start) / 1000,
|
|
63
|
-
total: total += docs.length
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
callback();
|
|
67
|
-
}
|
|
68
|
-
}).catch(err => {
|
|
69
|
-
err = error.convertResponseError(err);
|
|
70
|
-
ee.emit('error', err);
|
|
71
|
-
hasErrored = true;
|
|
72
|
-
callback();
|
|
73
|
-
});
|
|
74
|
-
},
|
|
75
|
-
function(callback) { callback(null, hasErrored || startKey == null); },
|
|
76
|
-
function() { ee.emit('finished', { total: total }); }
|
|
77
|
-
);
|
|
78
|
-
|
|
79
|
-
return ee;
|
|
80
|
-
};
|
package/includes/writer.js
DELETED
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
// Copyright © 2017, 2021 IBM Corp. All rights reserved.
|
|
2
|
-
//
|
|
3
|
-
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
// you may not use this file except in compliance with the License.
|
|
5
|
-
// You may obtain a copy of the License at
|
|
6
|
-
//
|
|
7
|
-
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
//
|
|
9
|
-
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
// See the License for the specific language governing permissions and
|
|
13
|
-
// limitations under the License.
|
|
14
|
-
'use strict';
|
|
15
|
-
|
|
16
|
-
const async = require('async');
|
|
17
|
-
const stream = require('stream');
|
|
18
|
-
const error = require('./error.js');
|
|
19
|
-
const debug = require('debug')('couchbackup:writer');
|
|
20
|
-
|
|
21
|
-
module.exports = function(db, bufferSize, parallelism, ee) {
|
|
22
|
-
const writer = new stream.Transform({ objectMode: true });
|
|
23
|
-
let buffer = [];
|
|
24
|
-
let written = 0;
|
|
25
|
-
let linenumber = 0;
|
|
26
|
-
|
|
27
|
-
// this is the queue of chunks that are written to the database
|
|
28
|
-
// the queue's payload will be an array of documents to be written,
|
|
29
|
-
// the size of the array will be bufferSize. The variable parallelism
|
|
30
|
-
// determines how many HTTP requests will occur at any one time.
|
|
31
|
-
const q = async.queue(function(payload, cb) {
|
|
32
|
-
// if we are restoring known revisions, we need to supply new_edits=false
|
|
33
|
-
if (payload.docs && payload.docs[0] && payload.docs[0]._rev) {
|
|
34
|
-
payload.new_edits = false;
|
|
35
|
-
debug('Using new_edits false mode.');
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
if (!didError) {
|
|
39
|
-
db.service.postBulkDocs({
|
|
40
|
-
db: db.db,
|
|
41
|
-
bulkDocs: payload
|
|
42
|
-
}).then(response => {
|
|
43
|
-
if (!response.result || (payload.new_edits === false && response.result.length > 0)) {
|
|
44
|
-
throw new Error(`Error writing batch with new_edits:${payload.new_edits !== false}` +
|
|
45
|
-
` and ${response.result ? response.result.length : 'unavailable'} items`);
|
|
46
|
-
}
|
|
47
|
-
written += payload.docs.length;
|
|
48
|
-
writer.emit('restored', { documents: payload.docs.length, total: written });
|
|
49
|
-
cb();
|
|
50
|
-
}).catch(err => {
|
|
51
|
-
err = error.convertResponseError(err);
|
|
52
|
-
debug(`Error writing docs ${err.name} ${err.message}`);
|
|
53
|
-
cb(err, payload);
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
}, parallelism);
|
|
57
|
-
|
|
58
|
-
let didError = false;
|
|
59
|
-
|
|
60
|
-
// write the contents of the buffer to CouchDB in blocks of bufferSize
|
|
61
|
-
function processBuffer(flush, callback) {
|
|
62
|
-
function taskCallback(err, payload) {
|
|
63
|
-
if (err && !didError) {
|
|
64
|
-
debug(`Queue task failed with error ${err.name}`);
|
|
65
|
-
didError = true;
|
|
66
|
-
q.kill();
|
|
67
|
-
writer.emit('error', err);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
if (flush || buffer.length >= bufferSize) {
|
|
72
|
-
// work through the buffer to break off bufferSize chunks
|
|
73
|
-
// and feed the chunks to the queue
|
|
74
|
-
do {
|
|
75
|
-
// split the buffer into bufferSize chunks
|
|
76
|
-
const toSend = buffer.splice(0, bufferSize);
|
|
77
|
-
|
|
78
|
-
// and add the chunk to the queue
|
|
79
|
-
debug(`Adding ${toSend.length} to the write queue.`);
|
|
80
|
-
q.push({ docs: toSend }, taskCallback);
|
|
81
|
-
} while (buffer.length >= bufferSize);
|
|
82
|
-
|
|
83
|
-
// send any leftover documents to the queue
|
|
84
|
-
if (flush && buffer.length > 0) {
|
|
85
|
-
debug(`Adding remaining ${buffer.length} to the write queue.`);
|
|
86
|
-
q.push({ docs: buffer }, taskCallback);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// wait until the queue size falls to a reasonable level
|
|
90
|
-
async.until(
|
|
91
|
-
// wait until the queue length drops to twice the paralellism
|
|
92
|
-
// or until empty on the last write
|
|
93
|
-
function(callback) {
|
|
94
|
-
// if we encountered an error, stop this until loop
|
|
95
|
-
if (didError) {
|
|
96
|
-
return callback(null, true);
|
|
97
|
-
}
|
|
98
|
-
if (flush) {
|
|
99
|
-
callback(null, q.idle() && q.length() === 0);
|
|
100
|
-
} else {
|
|
101
|
-
callback(null, q.length() <= parallelism * 2);
|
|
102
|
-
}
|
|
103
|
-
},
|
|
104
|
-
function(cb) {
|
|
105
|
-
setTimeout(cb, 20);
|
|
106
|
-
},
|
|
107
|
-
|
|
108
|
-
function() {
|
|
109
|
-
if (flush && !didError) {
|
|
110
|
-
writer.emit('finished', { total: written });
|
|
111
|
-
}
|
|
112
|
-
// callback when we're happy with the queue size
|
|
113
|
-
callback();
|
|
114
|
-
});
|
|
115
|
-
} else {
|
|
116
|
-
callback();
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
// take an object
|
|
121
|
-
writer._transform = function(obj, encoding, done) {
|
|
122
|
-
// each obj that arrives here is a line from the backup file
|
|
123
|
-
// it should contain an array of objects. The length of the array
|
|
124
|
-
// depends on the bufferSize at backup time.
|
|
125
|
-
linenumber++;
|
|
126
|
-
if (!didError && obj !== '') {
|
|
127
|
-
// see if it parses as JSON
|
|
128
|
-
try {
|
|
129
|
-
const arr = JSON.parse(obj);
|
|
130
|
-
|
|
131
|
-
// if it's an array with a length
|
|
132
|
-
if (typeof arr === 'object' && arr.length > 0) {
|
|
133
|
-
// push each document into a buffer
|
|
134
|
-
buffer = buffer.concat(arr);
|
|
135
|
-
|
|
136
|
-
// pause the stream
|
|
137
|
-
// it's likely that the speed with which data can be read from disk
|
|
138
|
-
// may exceed the rate it can be written to CouchDB. To prevent
|
|
139
|
-
// the whole file being buffered in memory, we pause the stream here.
|
|
140
|
-
// it is resumed, when processBuffer calls back and we call done()
|
|
141
|
-
this.pause();
|
|
142
|
-
|
|
143
|
-
// break the buffer in to bufferSize chunks to be written to the database
|
|
144
|
-
processBuffer(false, done);
|
|
145
|
-
} else {
|
|
146
|
-
ee.emit('error', new error.BackupError('BackupFileJsonError', `Error on line ${linenumber} of backup file - not an array`));
|
|
147
|
-
done();
|
|
148
|
-
}
|
|
149
|
-
} catch (e) {
|
|
150
|
-
ee.emit('error', new error.BackupError('BackupFileJsonError', `Error on line ${linenumber} of backup file - cannot parse as JSON`));
|
|
151
|
-
// Could be an incomplete write that was subsequently resumed
|
|
152
|
-
done();
|
|
153
|
-
}
|
|
154
|
-
} else {
|
|
155
|
-
done();
|
|
156
|
-
}
|
|
157
|
-
};
|
|
158
|
-
|
|
159
|
-
// called when we need to flush everything
|
|
160
|
-
writer._flush = function(done) {
|
|
161
|
-
processBuffer(true, done);
|
|
162
|
-
};
|
|
163
|
-
return writer;
|
|
164
|
-
};
|