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 restoreInternal = require('./includes/restore.js');
|
23 | const backupShallow = require('./includes/shallowbackup.js');
|
24 | const backupFull = require('./includes/backup.js');
|
25 | const defaults = require('./includes/config.js').apiDefaults();
|
26 | const events = require('events');
|
27 | const debug = require('debug')('couchbackup:app');
|
28 | const error = require('./includes/error.js');
|
29 | const fs = require('fs');
|
30 | const legacyUrl = require('url');
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | function isSafePositiveInteger(x) {
|
38 |
|
39 | const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991;
|
40 |
|
41 | return Object.prototype.toString.call(x) === '[object Number]' &&
|
42 |
|
43 | x % 1 === 0 &&
|
44 |
|
45 | x > 0 &&
|
46 |
|
47 | x <= MAX_SAFE_INTEGER;
|
48 | }
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | function validateArgs(url, opts, cb) {
|
58 | if (typeof url !== 'string') {
|
59 | cb(new error.BackupError('InvalidOption', 'Invalid URL, must be type string'), null);
|
60 | return;
|
61 | }
|
62 | if (opts && typeof opts.bufferSize !== 'undefined' && !isSafePositiveInteger(opts.bufferSize)) {
|
63 | cb(new error.BackupError('InvalidOption', 'Invalid buffer size option, must be a positive integer in the range (0, MAX_SAFE_INTEGER]'), null);
|
64 | return;
|
65 | }
|
66 | if (opts && typeof opts.iamApiKey !== 'undefined' && typeof opts.iamApiKey !== 'string') {
|
67 | cb(new error.BackupError('InvalidOption', 'Invalid iamApiKey option, must be type string'), null);
|
68 | return;
|
69 | }
|
70 | if (opts && typeof opts.log !== 'undefined' && typeof opts.log !== 'string') {
|
71 | cb(new error.BackupError('InvalidOption', 'Invalid log option, must be type string'), null);
|
72 | return;
|
73 | }
|
74 | if (opts && typeof opts.mode !== 'undefined' && ['full', 'shallow'].indexOf(opts.mode) === -1) {
|
75 | cb(new error.BackupError('InvalidOption', 'Invalid mode option, must be either "full" or "shallow"'), null);
|
76 | return;
|
77 | }
|
78 | if (opts && typeof opts.output !== 'undefined' && typeof opts.output !== 'string') {
|
79 | cb(new error.BackupError('InvalidOption', 'Invalid output option, must be type string'), null);
|
80 | return;
|
81 | }
|
82 | if (opts && typeof opts.parallelism !== 'undefined' && !isSafePositiveInteger(opts.parallelism)) {
|
83 | cb(new error.BackupError('InvalidOption', 'Invalid parallelism option, must be a positive integer in the range (0, MAX_SAFE_INTEGER]'), null);
|
84 | return;
|
85 | }
|
86 | if (opts && typeof opts.resume !== 'undefined' && typeof opts.resume !== 'boolean') {
|
87 | cb(new error.BackupError('InvalidOption', 'Invalid resume option, must be type boolean'), null);
|
88 | return;
|
89 | }
|
90 |
|
91 |
|
92 | try {
|
93 | const urlObject = legacyUrl.parse(url);
|
94 |
|
95 | if (urlObject.protocol !== 'https:' && urlObject.protocol !== 'http:') {
|
96 | cb(new error.BackupError('InvalidOption', 'Invalid URL protocol.'));
|
97 | return;
|
98 | }
|
99 | if (!urlObject.host) {
|
100 | cb(new error.BackupError('InvalidOption', 'Invalid URL host.'));
|
101 | return;
|
102 | }
|
103 | if (!urlObject.path) {
|
104 | cb(new error.BackupError('InvalidOption', 'Invalid URL, missing path element (no database).'));
|
105 | return;
|
106 | }
|
107 | if (opts && opts.iamApiKey && urlObject.auth) {
|
108 | cb(new error.BackupError('InvalidOption', 'URL user information must not be supplied when using IAM API key.'));
|
109 | return;
|
110 | }
|
111 | } catch (err) {
|
112 | cb(err);
|
113 | return;
|
114 | }
|
115 |
|
116 | if (opts && opts.resume) {
|
117 | if (!opts.log) {
|
118 |
|
119 |
|
120 | cb(new error.BackupError('NoLogFileName', 'To resume a backup, a log file must be specified'), null);
|
121 | return;
|
122 | } else if (!fs.existsSync(opts.log)) {
|
123 | cb(new error.BackupError('LogDoesNotExist', 'To resume a backup, the log file must exist'), null);
|
124 | return;
|
125 | }
|
126 | }
|
127 | return true;
|
128 | }
|
129 |
|
130 | module.exports = {
|
131 |
|
132 | |
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 | backup: function(srcUrl, targetStream, opts, callback) {
|
147 | if (typeof callback === 'undefined' && typeof opts === 'function') {
|
148 | callback = opts;
|
149 | opts = {};
|
150 | }
|
151 | if (!validateArgs(srcUrl, opts, callback)) {
|
152 |
|
153 | return;
|
154 | }
|
155 | opts = Object.assign({}, defaults, opts);
|
156 |
|
157 | var backup = null;
|
158 | if (opts.mode === 'shallow') {
|
159 | backup = backupShallow;
|
160 | } else {
|
161 | backup = backupFull;
|
162 | }
|
163 |
|
164 | const ee = new events.EventEmitter();
|
165 |
|
166 |
|
167 |
|
168 | targetStream.on('error', function(err) {
|
169 | debug('Error ' + JSON.stringify(err));
|
170 | if (callback) callback(err);
|
171 | });
|
172 |
|
173 |
|
174 |
|
175 |
|
176 | if (opts.resume) {
|
177 | targetStream.write('\n');
|
178 | }
|
179 |
|
180 |
|
181 |
|
182 | const internalEE = backup(srcUrl, opts)
|
183 | .on('changes', function(batch) {
|
184 | ee.emit('changes', batch);
|
185 | }).on('received', function(obj, q, logCompletedBatch) {
|
186 | debug(' backed up batch', obj.batch, ' docs: ', obj.total, 'Time', obj.time);
|
187 |
|
188 | function writeFlushed() {
|
189 | ee.emit('written', {total: obj.total, time: obj.time, batch: obj.batch});
|
190 | if (logCompletedBatch) {
|
191 | logCompletedBatch(obj.batch);
|
192 | }
|
193 | }
|
194 |
|
195 | const continueWriting = targetStream.write(JSON.stringify(obj.data) + '\n',
|
196 | 'utf8',
|
197 | writeFlushed);
|
198 | if (!continueWriting) {
|
199 |
|
200 |
|
201 | if (q && !q.isPaused) {
|
202 | q.pause();
|
203 | targetStream.once('drain', function() {
|
204 | q.resume();
|
205 | });
|
206 | }
|
207 | }
|
208 | })
|
209 |
|
210 | .on('error', function(err) {
|
211 | debug('Error ' + JSON.stringify(err));
|
212 |
|
213 |
|
214 |
|
215 |
|
216 | internalEE.removeAllListeners();
|
217 | callback(err);
|
218 | })
|
219 | .on('finished', function(obj) {
|
220 | function emitFinished() {
|
221 | debug('Backup complete - written ' + JSON.stringify(obj));
|
222 | const summary = {total: obj.total};
|
223 | ee.emit('finished', summary);
|
224 | if (callback) callback(null, summary);
|
225 | }
|
226 | if (targetStream === process.stdout) {
|
227 |
|
228 | targetStream.write('', 'utf8', emitFinished);
|
229 | } else {
|
230 |
|
231 |
|
232 |
|
233 | targetStream.end('', 'utf8', emitFinished);
|
234 | }
|
235 | });
|
236 |
|
237 | return ee;
|
238 | },
|
239 |
|
240 | |
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 | restore: function(srcStream, targetUrl, opts, callback) {
|
252 | if (typeof callback === 'undefined' && typeof opts === 'function') {
|
253 | callback = opts;
|
254 | opts = {};
|
255 | }
|
256 | validateArgs(targetUrl, opts, callback);
|
257 | opts = Object.assign({}, defaults, opts);
|
258 |
|
259 | const ee = new events.EventEmitter();
|
260 |
|
261 | restoreInternal(
|
262 | targetUrl,
|
263 | opts,
|
264 | srcStream,
|
265 | ee,
|
266 | function(err, writer) {
|
267 | if (err) {
|
268 | callback(err, null);
|
269 | return;
|
270 | }
|
271 | if (writer != null) {
|
272 | writer.on('restored', function(obj) {
|
273 | debug(' restored ', obj.total);
|
274 | ee.emit('restored', {documents: obj.documents, total: obj.total});
|
275 | })
|
276 |
|
277 | .on('error', function(err) {
|
278 | debug('Error ' + JSON.stringify(err));
|
279 |
|
280 |
|
281 |
|
282 |
|
283 | writer.removeAllListeners();
|
284 |
|
285 | if (srcStream.destroy && srcStream.destroy instanceof Function) {
|
286 | srcStream.destroy();
|
287 | }
|
288 | callback(err);
|
289 | })
|
290 | .on('finished', function(obj) {
|
291 | debug('restore complete');
|
292 | ee.emit('finished', {total: obj.total});
|
293 | callback(null, obj);
|
294 | });
|
295 | }
|
296 | }
|
297 | );
|
298 |
|
299 | return ee;
|
300 | }
|
301 |
|
302 | };
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|