Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
var async = require('async');
var ProgressBar = require('progress');
var monk = require('monk');
var ObjectId=require('mongodb').ObjectID;
var dest = monk('localhost:27017/storify_localhost');
var backup = monk('localhost:27017/storify_backup');
var userId = ObjectId(YOUR-OBJECT-ID); // monk should have auto casting but we need it for queries
var restore = function(collection, query, callback){
console.info('restoring from ' + collection);
var q = query;
backup.get(collection).count(q, function(e, n) {
console.log('found '+n+' '+collection);
if (e) console.error(e);
var bar = new ProgressBar('[:bar] :current/:total :percent :etas', { total: n-1, width: 40 })
var tick = function(e) {
if (e) {
console.error(e);
bar.tick();
}
else {
bar.tick();
}
if (bar.complete) {
console.log();
console.log('restoring '+collection+' is completed');
callback();
}
};
if (n>0){
console.log('adding '+ n+ ' '+collection);
backup.get(collection).find(q, { stream: true }).each(function(element) {
dest.get(collection).insert(element, tick);
});
} else {
callback();
}
});
}
async.series({
restoreUser: function(callback){ // import user element
backup.get('users').find({_id:userId}, { stream: true, limit: 1 }).each(function(user) {
dest.get('users').insert(user, function(e){
if (e) {
console.log(e);
}
else {
console.log('resored user: '+ user.username);
}
callback();
});
});
},
restoreIdentity: function(callback){
restore('identities',{
userid:userId
}, callback);
},
restoreStories: function(callback){
restore('stories', {authorid:userId}, callback);
}
}, function(e) {
console.log();
console.log('restoring is completed!');
process.exit(1);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment