Last active
February 7, 2022 01:01
-
-
Save soldair/5959183 to your computer and use it in GitHub Desktop.
backup a running node levelup process
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var levelup = require('level') | |
var zlib = require('zlib') | |
var fs = require('fs') | |
var through = require('through') | |
//hot backup | |
db = levelup(...) | |
var backingup = false | |
var backupfile = "./backupjson.log.gz" | |
process.on('SIGHUP', function() { | |
if(backingup) return console.log('im already backing up') | |
console.log('backup started!') | |
var t = Date.now() | |
// an example of backing up to a file. | |
db.createReadStream() | |
.pipe(through(function(obj) { | |
// depending on if you store binary data your serialization method could be msgpack. | |
this.queue(JSON.stringify(obj)+"\n"); | |
})) | |
.pipe(zlib.createGzip()) | |
.pipe(fs.createWriteStream(backupfile)) | |
.on("close",function() { | |
backingup = false; | |
console.log('backup complete. took ', Date.now()-t, 'ms'); | |
}) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var split = require("split") | |
var through = require("through") | |
var fs = require("fs") | |
var zlib = require("zlib") | |
var levelup = require("level") | |
var db = levelup("./db2") | |
fs.createReadStream("./backupjson.log.gz") | |
.pipe(zlib.createGunzip()) | |
.pipe(split()) | |
.pipe(through(function(str){ | |
this.queue(JSON.parse(str)) | |
})) | |
.pipe(db.createWriteStream()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment