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 proceedIfDbValid(db, callback) {
|
163 | db.service.headDatabase({ db: db.db }).then(() => callback()).catch(err => {
|
164 | err = error.convertResponseError(err, function(err) {
|
165 | if (err && err.status === 404) {
|
166 |
|
167 | var msg = `Database ${db.url}` +
|
168 | `${db.db} does not exist. ` +
|
169 | 'Check the URL and database name have been specified correctly.';
|
170 | var noDBErr = new Error(msg);
|
171 | noDBErr.name = 'DatabaseNotFound';
|
172 | return noDBErr;
|
173 | } else {
|
174 |
|
175 | return error.convertResponseError(err);
|
176 | }
|
177 | });
|
178 | callback(err);
|
179 | });
|
180 | }
|
181 |
|
182 | module.exports = {
|
183 |
|
184 | |
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 | backup: function(srcUrl, targetStream, opts, callback) {
|
200 | var listenerErrorIndicator = { errored: false };
|
201 | if (typeof callback === 'undefined' && typeof opts === 'function') {
|
202 | callback = opts;
|
203 | opts = {};
|
204 | }
|
205 | if (!validateArgs(srcUrl, opts, callback)) {
|
206 |
|
207 | return;
|
208 | }
|
209 |
|
210 |
|
211 |
|
212 | addEventListener(listenerErrorIndicator, targetStream, 'error', function(err) {
|
213 | debug('Error ' + JSON.stringify(err));
|
214 | if (callback) callback(err);
|
215 | });
|
216 |
|
217 | opts = Object.assign({}, defaults(), opts);
|
218 |
|
219 | const ee = new events.EventEmitter();
|
220 |
|
221 |
|
222 | const backupDB = request.client(srcUrl, opts);
|
223 |
|
224 |
|
225 | proceedIfDbValid(backupDB, function(err) {
|
226 | if (err) {
|
227 | if (err.name === 'DatabaseNotFound') {
|
228 | err.message = `${err.message} Ensure the backup source database exists.`;
|
229 | }
|
230 |
|
231 | callback(err);
|
232 | return;
|
233 | }
|
234 | var backup = null;
|
235 | if (opts.mode === 'shallow') {
|
236 | backup = backupShallow;
|
237 | } else {
|
238 | backup = backupFull;
|
239 | }
|
240 |
|
241 |
|
242 |
|
243 |
|
244 | if (opts.resume) {
|
245 | targetStream.write('\n');
|
246 | }
|
247 |
|
248 |
|
249 |
|
250 | const internalEE = backup(backupDB, opts);
|
251 | addEventListener(listenerErrorIndicator, internalEE, 'changes', function(batch) {
|
252 | ee.emit('changes', batch);
|
253 | });
|
254 | addEventListener(listenerErrorIndicator, internalEE, 'received', function(obj, q, logCompletedBatch) {
|
255 |
|
256 |
|
257 |
|
258 | function writeFlushed() {
|
259 | ee.emit('written', { total: obj.total, time: obj.time, batch: obj.batch });
|
260 | if (logCompletedBatch) {
|
261 | logCompletedBatch(obj.batch);
|
262 | }
|
263 | debug(' backed up batch', obj.batch, ' docs: ', obj.total, 'Time', obj.time);
|
264 | }
|
265 |
|
266 | const continueWriting = targetStream.write(JSON.stringify(obj.data) + '\n',
|
267 | 'utf8',
|
268 | writeFlushed);
|
269 | if (!continueWriting) {
|
270 |
|
271 |
|
272 | if (q && !q.paused) {
|
273 | q.pause();
|
274 | targetStream.once('drain', function() {
|
275 | q.resume();
|
276 | });
|
277 | }
|
278 | }
|
279 | });
|
280 |
|
281 | addEventListener(listenerErrorIndicator, internalEE, 'error', function(err) {
|
282 | debug('Error ' + JSON.stringify(err));
|
283 | callback(err);
|
284 | });
|
285 | addEventListener(listenerErrorIndicator, internalEE, 'finished', function(obj) {
|
286 | function emitFinished() {
|
287 | debug('Backup complete - written ' + JSON.stringify(obj));
|
288 | const summary = { total: obj.total };
|
289 | ee.emit('finished', summary);
|
290 | if (callback) callback(null, summary);
|
291 | }
|
292 | if (targetStream === process.stdout) {
|
293 |
|
294 | targetStream.write('', 'utf8', emitFinished);
|
295 | } else {
|
296 |
|
297 |
|
298 |
|
299 | targetStream.end('', 'utf8', emitFinished);
|
300 | }
|
301 | });
|
302 | });
|
303 | return ee;
|
304 | },
|
305 |
|
306 | |
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 | restore: function(srcStream, targetUrl, opts, callback) {
|
319 | var listenerErrorIndicator = { errored: false };
|
320 | if (typeof callback === 'undefined' && typeof opts === 'function') {
|
321 | callback = opts;
|
322 | opts = {};
|
323 | }
|
324 | validateArgs(targetUrl, opts, callback);
|
325 | opts = Object.assign({}, defaults(), opts);
|
326 |
|
327 | const ee = new events.EventEmitter();
|
328 |
|
329 |
|
330 | const restoreDB = request.client(targetUrl, opts);
|
331 |
|
332 |
|
333 | proceedIfDbValid(restoreDB, function(err) {
|
334 | if (err) {
|
335 | if (err.name === 'DatabaseNotFound') {
|
336 | err.message = `${err.message} Create the target database before restoring.`;
|
337 | }
|
338 |
|
339 | callback(err);
|
340 | return;
|
341 | }
|
342 |
|
343 | restoreInternal(
|
344 | restoreDB,
|
345 | opts,
|
346 | srcStream,
|
347 | ee,
|
348 | function(err, writer) {
|
349 | if (err) {
|
350 | callback(err, null);
|
351 | return;
|
352 | }
|
353 | if (writer != null) {
|
354 | addEventListener(listenerErrorIndicator, writer, 'restored', function(obj) {
|
355 | debug(' restored ', obj.total);
|
356 | ee.emit('restored', { documents: obj.documents, total: obj.total });
|
357 | });
|
358 | addEventListener(listenerErrorIndicator, writer, 'error', function(err) {
|
359 | debug('Error ' + JSON.stringify(err));
|
360 |
|
361 | if (srcStream.destroy && srcStream.destroy instanceof Function) {
|
362 | srcStream.destroy();
|
363 | }
|
364 | callback(err);
|
365 | });
|
366 | addEventListener(listenerErrorIndicator, writer, 'finished', function(obj) {
|
367 | debug('restore complete');
|
368 | ee.emit('finished', { total: obj.total });
|
369 | callback(null, obj);
|
370 | });
|
371 | }
|
372 | }
|
373 | );
|
374 | });
|
375 | return ee;
|
376 | }
|
377 | };
|
378 |
|
379 |
|
380 |
|
381 |
|
382 |
|
383 |
|
384 |
|