-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackup
More file actions
136 lines (124 loc) · 3.46 KB
/
backup
File metadata and controls
136 lines (124 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
var async = require('async');
var fs = require('fs');
const {
exec
} = require('child_process');
var argv = process.argv;
var fromDate = new Date();
var databaseName = "accio_publishers"
const fromurl = 'mongodb://localhost:27017';
var execCommand = function(cmd, cb) {
console.log(cmd);
exec(cmd, function(err, stdout, stderr) {
if (err) {
return cb(err);
} else {
return cb(null, stderr)
}
});
}
var getDumpFromCollections = function(db, collection, query, cb) {
var dbname = db["databaseName"];
var command = "mongodump -o dump/full --host localhost --port 27017 -d '" + dbname + "' -c '" + collection + "'";
var command2 = "mongodump -o dump/delta --host localhost --port 27017 -d '" + dbname + "' -c '" + collection + "'";
if (query) {
command2 += " -q '" + JSON.stringify(query) + "'"
}
return execCommand(command2, cb)
}
var zipAndMail = function(cb) {
var command = "zip -r mongodump.zip dump/"
return execCommand(command, cb)
}
var collectToServer = function(url, dbName, cb) {
console.log("connecting to mongo server ...");
MongoClient.connect(url, function(err, client) {
if (err) {
console.log("connection failed");
cb(err)
} else {
console.log("connected to mongo server");
cb(null, client, client.db(dbName))
}
});
}
var getListofDatabase = function(client, db, cb) {
var adminDb = db.admin();
adminDb.listDatabases(function(err, dbs) {
cb(err, client, dbs)
});
}
var getAllCollectionsFromDatabase = function(db, cb) {
db.listCollections().toArray().then(function(collection) {
var newColl = collection.map(function(c) {
return c.name
});
cb(null, db, newColl);
}).catch(function(err) {
cb(err);
});
}
var interateCollection = function(db, collections, cb) {
async.map(collections, function(collection, mapcb) {
var dbcollection = db.collection(collection);
/*
count;
delta:
deleted
backup
*/
getDumpFromCollections(db, collection, "", function(err, data) {
dbcollection.count({}, function(err, count) {
var d = {}
d[collection] = count;
mapcb(err, d)
})
})
}, function(err, results) {
cb(null, results)
})
}
var interateDatabases = function(client, dbs, cb) {
// console.log(dbs); // save this file
async.map(dbs.databases, function(database, mapcb) {
console.log(database.name);
var db = client.db(database.name)
async.waterfall([
function(waterfallcb) {
getAllCollectionsFromDatabase(db, function(err, dbObj, collections) {
console.log(collections);
waterfallcb(err, dbObj, collections)
})
},
interateCollection
], function(err, results) {
mapcb(err, results)
})
}, function(err, results) {
console.log();
cb(err, results)
})
}
var takeBackup = function() {
async.waterfall([
function(callback) {
collectToServer(fromurl, databaseName, function(err, client, db) {
callback(err, client, db)
})
},
getListofDatabase,
interateDatabases,
], function(err, results) {
if (err) {
console.log(err);
}
console.log(results);
zipAndMail(function(err, info) {
process.exit();
})
})
}
takeBackup();
// mongorestore --host localhost --port 27017 -d 'dbname' -c 'collections' /Users/Ritesh/Documents/workspace/dbname/mongodump/dbname/collections.bson