Skip to content

Commit 6563771

Browse files
committed
Add NOSQL backup/restore.
1 parent 5b12a55 commit 6563771

3 files changed

Lines changed: 80 additions & 9 deletions

File tree

changes.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
======= 2.8.0
22

3+
- added: `NOSQL().restore()` restores database
4+
35
- updated: (IMPORTANT) packages compress/decompress function supports streaming data
6+
- updated: (IMPORTANT) `NOSQL().backup()` !!! was changed !!!!
47
- updated: `controller.view(name/url, [model], [headers], [partial])` can render a view from URL address
58
- updated: `F.backup()` argument `path` can contain `String Array` file list
69
- updated: `controller.viewCompile(body, model, [headers], [partial], [cacheKey])` add a cache key

index.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5455,9 +5455,12 @@ F.restore = function(filename, target, callback, filter) {
54555455
var index = 0;
54565456
var parser = {};
54575457
var open = {};
5458-
var count = 0;
54595458
var pending = 0;
54605459
var end = false;
5460+
var output = {};
5461+
5462+
output.count = 0;
5463+
output.path = target;
54615464

54625465
parser.parse_key = function() {
54635466

@@ -5510,7 +5513,7 @@ F.restore = function(filename, target, callback, filter) {
55105513
tmp.zlib.$self = tmp;
55115514
pending++;
55125515

5513-
count++;
5516+
output.count++;
55145517

55155518
tmp.zlib.on('data', function(chunk) {
55165519
this.$self.writer.write(chunk);
@@ -5590,18 +5593,15 @@ F.restore = function(filename, target, callback, filter) {
55905593
break;
55915594
}
55925595

5593-
if (end && !data.length)
5594-
console.log('callback 1');
5595-
5596-
end && !data.length && callback && callback(null, count);
5596+
end && !data.length && callback && callback(null, output);
55975597
};
55985598

55995599
parser.end = function() {
56005600
if (callback) {
56015601
if (pending)
56025602
setTimeout(parser.end, 100);
56035603
else if (end && !data.length)
5604-
callback(null, count);
5604+
callback(null, output);
56055605
}
56065606
};
56075607

@@ -5633,6 +5633,8 @@ F.backup = function(filename, filelist, callback, filter) {
56335633
if (!(filelist instanceof Array))
56345634
filelist = [''];
56355635

5636+
var counter = 0;
5637+
56365638
Fs.unlink(filename, function() {
56375639

56385640
filelist.sort(function(a, b) {
@@ -5696,11 +5698,14 @@ F.backup = function(filename, filelist, callback, filter) {
56965698
}), function() {
56975699
data.length && writer.write(data.toString('base64'));
56985700
writer.write('\n', 'utf8');
5701+
counter++;
56995702
next();
57005703
});
57015704

57025705
});
5703-
}, () => callback(null, filename));
5706+
}, function() {
5707+
callback && Fs.stat(filename, (e, stat) => callback(null, { filename: filename, files: counter, size: stat.size }));
5708+
});
57045709
});
57055710

57065711
return F;

nosql.js

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,70 @@ Database.prototype.modify = function(doc, insert) {
250250
return builder;
251251
};
252252

253-
Database.prototype.backup = function(filename, remove) {
253+
Database.prototype.restore = function(filename, callback) {
254+
var self = this;
255+
256+
U.wait(() => !self.type, function(err) {
257+
258+
if (err)
259+
throw new Error('Database can\'t be restored because it\'s busy.');
260+
261+
self.type = 9;
262+
F.restore(filename, F.path.root(), function(err, response) {
263+
self.type = 0;
264+
!err && self.refresh();
265+
callback && callback(err, response);
266+
});
267+
});
268+
return self;
269+
};
270+
271+
Database.prototype.backup = function(filename, callback) {
272+
273+
var self = this;
274+
var list = [];
275+
var pending = [];
276+
277+
pending.push(function(next) {
278+
F.path.exists(self.filename, function(e) {
279+
e && list.push(Path.join(F.config['directory-databases'], self.name + EXTENSION));
280+
next();
281+
});
282+
});
283+
284+
pending.push(function(next) {
285+
F.path.exists(F.path.databases(self.name + EXTENSION_META), function(e) {
286+
e && list.push(Path.join(F.config['directory-databases'], self.name + EXTENSION_META));
287+
next();
288+
});
289+
});
290+
291+
pending.push(function(next) {
292+
F.path.exists(F.path.databases(self.name + EXTENSION + '-counter'), function(e) {
293+
e && list.push(Path.join(F.config['directory-databases'], self.name + EXTENSION + '-counter'));
294+
next();
295+
});
296+
});
297+
298+
pending.push(function(next) {
299+
F.path.exists(F.path.databases(self.name + '-binary'), function(e, size, file) {
300+
e && !file && list.push(Path.join(F.config['directory-databases'], self.name + '-binary'));
301+
next();
302+
});
303+
});
304+
305+
pending.async(function() {
306+
if (list.length)
307+
F.backup(filename, list, callback);
308+
else
309+
callback(new Error('No files for backuping.'));
310+
});
311+
312+
313+
return self;
314+
};
315+
316+
Database.prototype.backup2 = function(filename, remove) {
254317

255318
if (typeof(filename) === 'boolean') {
256319
remove = filename;

0 commit comments

Comments
 (0)