Skip to content

Instantly share code, notes, and snippets.

@soldair soldair/db.js
Last active Jun 21, 2018

Embed
What would you like to do?
backup a running node levelup process
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');
})
})
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
You can’t perform that action at this time.