1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | 'use strict';
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | const backupFull = require('./includes/backup.js');
|
23 | const defaults = require('./includes/config.js').apiDefaults;
|
24 | const error = require('./includes/error.js');
|
25 | const request = require('./includes/request.js');
|
26 | const restoreInternal = require('./includes/restore.js');
|
27 | const backupShallow = require('./includes/shallowbackup.js');
|
28 | const debug = require('debug')('couchbackup:app');
|
29 | const events = require('events');
|
30 | const fs = require('fs');
|
31 | const URL = require('url').URL;
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | function isSafePositiveInteger(x) {
|
39 |
|
40 | const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991;
|
41 |
|
42 | return Object.prototype.toString.call(x) === '[object Number]' &&
|
43 |
|
44 | x % 1 === 0 &&
|
45 |
|
46 | x > 0 &&
|
47 |
|
48 | x <= MAX_SAFE_INTEGER;
|
49 | }
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | function validateArgs(url, opts, cb) {
|
59 | if (typeof url !== 'string') {
|
60 | cb(new error.BackupError('InvalidOption', 'Invalid URL, must be type string'), null);
|
61 | return;
|
62 | }
|
63 | if (opts && typeof opts.bufferSize !== 'undefined' && !isSafePositiveInteger(opts.bufferSize)) {
|
64 | cb(new error.BackupError('InvalidOption', 'Invalid buffer size option, must be a positive integer in the range (0, MAX_SAFE_INTEGER]'), null);
|
65 | return;
|
66 | }
|
67 | if (opts && typeof opts.iamApiKey !== 'undefined' && typeof opts.iamApiKey !== 'string') {
|
68 | cb(new error.BackupError('InvalidOption', 'Invalid iamApiKey option, must be type string'), null);
|
69 | return;
|
70 | }
|
71 | if (opts && typeof opts.log !== 'undefined' && typeof opts.log !== 'string') {
|
72 | cb(new error.BackupError('InvalidOption', 'Invalid log option, must be type string'), null);
|
73 | return;
|
74 | }
|
75 | if (opts && typeof opts.mode !== 'undefined' && ['full', 'shallow'].indexOf(opts.mode) === -1) {
|
76 | cb(new error.BackupError('InvalidOption', 'Invalid mode option, must be either "full" or "shallow"'), null);
|
77 | return;
|
78 | }
|
79 | if (opts && typeof opts.output !== 'undefined' && typeof opts.output !== 'string') {
|
80 | cb(new error.BackupError('InvalidOption', 'Invalid output option, must be type string'), null);
|
81 | return;
|
82 | }
|
83 | if (opts && typeof opts.parallelism !== 'undefined' && !isSafePositiveInteger(opts.parallelism)) {
|
84 | cb(new error.BackupError('InvalidOption', 'Invalid parallelism option, must be a positive integer in the range (0, MAX_SAFE_INTEGER]'), null);
|
85 | return;
|
86 | }
|
87 | if (opts && typeof opts.requestTimeout !== 'undefined' && !isSafePositiveInteger(opts.requestTimeout)) {
|
88 | cb(new error.BackupError('InvalidOption', 'Invalid request timeout option, must be a positive integer in the range (0, MAX_SAFE_INTEGER]'), null);
|
89 | return;
|
90 | }
|
91 | if (opts && typeof opts.resume !== 'undefined' && typeof opts.resume !== 'boolean') {
|
92 | cb(new error.BackupError('InvalidOption', 'Invalid resume option, must be type boolean'), null);
|
93 | return;
|
94 | }
|
95 |
|
96 |
|
97 | try {
|
98 | const urlObject = new URL(url);
|
99 |
|
100 | if (urlObject.protocol !== 'https:' && urlObject.protocol !== 'http:') {
|
101 | cb(new error.BackupError('InvalidOption', 'Invalid URL protocol.'));
|
102 | return;
|
103 | }
|
104 | if (!urlObject.host) {
|
105 | cb(new error.BackupError('InvalidOption', 'Invalid URL host.'));
|
106 | return;
|
107 | }
|
108 | if (!urlObject.pathname || urlObject.pathname === '/') {
|
109 | cb(new error.BackupError('InvalidOption', 'Invalid URL, missing path element (no database).'));
|
110 | return;
|
111 | }
|
112 | if (opts && opts.iamApiKey && (urlObject.username || urlObject.password)) {
|
113 | cb(new error.BackupError('InvalidOption', 'URL user information must not be supplied when using IAM API key.'));
|
114 | return;
|
115 | }
|
116 | } catch (err) {
|
117 | cb(err);
|
118 | return;
|
119 | }
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | if (opts && opts.mode === 'shallow') {
|
125 | if (opts.log || opts.resume) {
|
126 | console.warn('WARNING: the options "log" and "resume" are invalid when using shallow mode.');
|
127 | }
|
128 | if (opts.parallelism) {
|
129 | console.warn('WARNING: the option "parallelism" has no effect when using shallow mode.');
|
130 | }
|
131 | }
|
132 |
|
133 | if (opts && opts.resume) {
|
134 | if (!opts.log) {
|
135 |
|
136 |
|
137 | cb(new error.BackupError('NoLogFileName', 'To resume a backup, a log file must be specified'), null);
|
138 | return;
|
139 | } else if (!fs.existsSync(opts.log)) {
|
140 | cb(new error.BackupError('LogDoesNotExist', 'To resume a backup, the log file must exist'), null);
|
141 | return;
|
142 | }
|
143 | }
|
144 | return true;
|
145 | }
|
146 |
|
147 | function addEventListener(indicator, emitter, event, f) {
|
148 | emitter.on(event, function(...args) {
|
149 | if (!indicator.errored) {
|
150 | if (event === 'error') indicator.errored = true;
|
151 | f(...args);
|
152 | }
|
153 | });
|
154 | }
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 | function proceedIfBackupDbValid(db, callback) {
|
163 | db.service.headDatabase({ db: db.db }).then(() => callback()).catch(err => {
|
164 | err = error.convertResponseError(err, err => parseIfDbValidResponseError(db, err));
|
165 | callback(err);
|
166 | });
|
167 | }
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 | function proceedIfRestoreDbValid(db, callback) {
|
176 | db.service.getDatabaseInformation({ db: db.db }).then(response => {
|
177 | const { doc_count: docCount, doc_del_count: deletedDocCount } = response.result;
|
178 |
|
179 |
|
180 |
|
181 | if (!db.db.startsWith('_') && (docCount !== 0 || deletedDocCount !== 0)) {
|
182 | const notEmptyDBErr = new Error(`Target database ${db.url}${db.db} is not empty.`);
|
183 | notEmptyDBErr.name = 'DatabaseNotEmpty';
|
184 | callback(notEmptyDBErr);
|
185 | } else {
|
186 | callback();
|
187 | }
|
188 | }).catch(err => {
|
189 | err = error.convertResponseError(err, err => parseIfDbValidResponseError(db, err));
|
190 | callback(err);
|
191 | });
|
192 | }
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 | function parseIfDbValidResponseError(db, err) {
|
201 | if (err && err.status === 404) {
|
202 |
|
203 | const msg = `Database ${db.url}` +
|
204 | `${db.db} does not exist. ` +
|
205 | 'Check the URL and database name have been specified correctly.';
|
206 | const noDBErr = new Error(msg);
|
207 | noDBErr.name = 'DatabaseNotFound';
|
208 | return noDBErr;
|
209 | }
|
210 |
|
211 | return error.convertResponseError(err);
|
212 | }
|
213 |
|
214 | module.exports = {
|
215 |
|
216 | |
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 | backup: function(srcUrl, targetStream, opts, callback) {
|
232 | const listenerErrorIndicator = { errored: false };
|
233 | if (typeof callback === 'undefined' && typeof opts === 'function') {
|
234 | callback = opts;
|
235 | opts = {};
|
236 | }
|
237 | if (!validateArgs(srcUrl, opts, callback)) {
|
238 |
|
239 | return;
|
240 | }
|
241 |
|
242 |
|
243 |
|
244 | addEventListener(listenerErrorIndicator, targetStream, 'error', function(err) {
|
245 | debug('Error ' + JSON.stringify(err));
|
246 | if (callback) callback(err);
|
247 | });
|
248 |
|
249 | opts = Object.assign({}, defaults(), opts);
|
250 |
|
251 | const ee = new events.EventEmitter();
|
252 |
|
253 |
|
254 | const backupDB = request.client(srcUrl, opts);
|
255 |
|
256 |
|
257 | proceedIfBackupDbValid(backupDB, function(err) {
|
258 | if (err) {
|
259 | if (err.name === 'DatabaseNotFound') {
|
260 | err.message = `${err.message} Ensure the backup source database exists.`;
|
261 | }
|
262 |
|
263 | callback(err);
|
264 | return;
|
265 | }
|
266 | let backup = null;
|
267 | if (opts.mode === 'shallow') {
|
268 | backup = backupShallow;
|
269 | } else {
|
270 | backup = backupFull;
|
271 | }
|
272 |
|
273 |
|
274 |
|
275 |
|
276 | if (opts.resume) {
|
277 | targetStream.write('\n');
|
278 | }
|
279 |
|
280 |
|
281 |
|
282 | const internalEE = backup(backupDB, opts);
|
283 | addEventListener(listenerErrorIndicator, internalEE, 'changes', function(batch) {
|
284 | ee.emit('changes', batch);
|
285 | });
|
286 | addEventListener(listenerErrorIndicator, internalEE, 'received', function(obj, q, logCompletedBatch) {
|
287 |
|
288 |
|
289 |
|
290 | function writeFlushed() {
|
291 | ee.emit('written', { total: obj.total, time: obj.time, batch: obj.batch });
|
292 | if (logCompletedBatch) {
|
293 | logCompletedBatch(obj.batch);
|
294 | }
|
295 | debug(' backed up batch', obj.batch, ' docs: ', obj.total, 'Time', obj.time);
|
296 | }
|
297 |
|
298 | const continueWriting = targetStream.write(JSON.stringify(obj.data) + '\n',
|
299 | 'utf8',
|
300 | writeFlushed);
|
301 | if (!continueWriting) {
|
302 |
|
303 |
|
304 | if (q && !q.paused) {
|
305 | q.pause();
|
306 | targetStream.once('drain', function() {
|
307 | q.resume();
|
308 | });
|
309 | }
|
310 | }
|
311 | });
|
312 |
|
313 | addEventListener(listenerErrorIndicator, internalEE, 'error', function(err) {
|
314 | debug('Error ' + JSON.stringify(err));
|
315 | callback(err);
|
316 | });
|
317 | addEventListener(listenerErrorIndicator, internalEE, 'finished', function(obj) {
|
318 | function emitFinished() {
|
319 | debug('Backup complete - written ' + JSON.stringify(obj));
|
320 | const summary = { total: obj.total };
|
321 | ee.emit('finished', summary);
|
322 | if (callback) callback(null, summary);
|
323 | }
|
324 | if (targetStream === process.stdout) {
|
325 |
|
326 | targetStream.write('', 'utf8', emitFinished);
|
327 | } else {
|
328 |
|
329 |
|
330 |
|
331 | targetStream.end('', 'utf8', emitFinished);
|
332 | }
|
333 | });
|
334 | });
|
335 | return ee;
|
336 | },
|
337 |
|
338 | |
339 |
|
340 |
|
341 |
|
342 |
|
343 |
|
344 |
|
345 |
|
346 |
|
347 |
|
348 |
|
349 |
|
350 | restore: function(srcStream, targetUrl, opts, callback) {
|
351 | const listenerErrorIndicator = { errored: false };
|
352 | if (typeof callback === 'undefined' && typeof opts === 'function') {
|
353 | callback = opts;
|
354 | opts = {};
|
355 | }
|
356 | validateArgs(targetUrl, opts, callback);
|
357 | opts = Object.assign({}, defaults(), opts);
|
358 |
|
359 | const ee = new events.EventEmitter();
|
360 |
|
361 |
|
362 | const restoreDB = request.client(targetUrl, opts);
|
363 |
|
364 |
|
365 | proceedIfRestoreDbValid(restoreDB, function(err) {
|
366 | if (err) {
|
367 | if (err.name === 'DatabaseNotFound') {
|
368 | err.message = `${err.message} Create the target database before restoring.`;
|
369 | } else if (err.name === 'DatabaseNotEmpty') {
|
370 | err.message = `${err.message} A target database must be a new and empty database.`;
|
371 | }
|
372 |
|
373 | callback(err);
|
374 | return;
|
375 | }
|
376 |
|
377 | restoreInternal(
|
378 | restoreDB,
|
379 | opts,
|
380 | srcStream,
|
381 | ee,
|
382 | function(err, writer) {
|
383 | if (err) {
|
384 | callback(err, null);
|
385 | return;
|
386 | }
|
387 | if (writer != null) {
|
388 | addEventListener(listenerErrorIndicator, writer, 'restored', function(obj) {
|
389 | debug(' restored ', obj.total);
|
390 | ee.emit('restored', { documents: obj.documents, total: obj.total });
|
391 | });
|
392 | addEventListener(listenerErrorIndicator, writer, 'error', function(err) {
|
393 | debug('Error ' + JSON.stringify(err));
|
394 |
|
395 | if (srcStream.destroy && srcStream.destroy instanceof Function) {
|
396 | srcStream.destroy();
|
397 | }
|
398 | callback(err);
|
399 | });
|
400 | addEventListener(listenerErrorIndicator, writer, 'finished', function(obj) {
|
401 | debug('restore complete');
|
402 | ee.emit('finished', { total: obj.total });
|
403 | callback(null, obj);
|
404 | });
|
405 | }
|
406 | }
|
407 | );
|
408 | });
|
409 | return ee;
|
410 | }
|
411 | };
|
412 |
|
413 |
|
414 |
|
415 |
|
416 |
|
417 |
|
418 |
|