-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
251 lines (163 loc) · 5.73 KB
/
index.js
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/**
Author: Seun Matt ([email protected]);
Project Name: restore-backup-mongodb
Project Desc:
This module will restore backup of mongodb created by backup-mongodb
in .zip format.
@param(databaseUri) the uri to the mongodatabase e.g. mongodb://127.0.0.1:27017/test
@param (pathToZipFile) path/to/backupfile.zip
Usage:
var databaseUri = "mongodb://127.0.0.1:27017/test";
var filePath = "backup/dev_19_9_16.21.40.28.zip";
var Restore = require("restore-backup-mongodb");
new Restore(databaseUri, filePath).restore();
*
**/
var fs = require("fs-extra");
var path = require("path");
var unzip = require("unzip");
var mongodb = require("mongodb");
var mongoClient = require("mongodb").MongoClient;
var databaseUri;
var fileNames = [];
var jsonData = []; //this file will contain the loaded data
var db; //global db object
var zipPath; // path/to/zipfile.zip
var tempPath = __dirname + "/temp";
var d; //global var for done callback for mocha test
var winston = require("winston");
var ObjectID = require("mongodb").ObjectID;
//this boolean value will determine if the database utilizes the ObjectID class of mongodb
var isObjectID = true;
function Restore (dbaseUri, pathToZipFile, useObjectID) {
if(!dbaseUri || !pathToZipFile) {
winston.error("incomplete params \ndbaseUri = " + dbaseUri + "\npathToZipFile = " + pathToZipFile);
throw new Error("incomplete params \ndbaseUri = " + dbaseUri + "\npathToZipFile = " + pathToZipFile);
if(d) d();
}
isObjectID = useObjectID;
winston.error("isObjectID = " + isObjectID + " useObjectID = " + useObjectID);
databaseUri = dbaseUri;
zipPath = pathToZipFile;
}
Restore.prototype.restore = function(done) {
d = done;
mongoClient.connect(databaseUri, function(error, dbObj) {
if(error) {
winston.error("ERROR CONNECTING TO MONGODB " + error);
if(d) d();
return;
}
else {
winston.info("Restore Script Connected to MongoDb successfully");
db = dbObj;
// first extract the zip file to tempPath
extractZip();
}
});
}
function extractZip() {
// this is the first thing to be done. It extracts the zip file
var unzipExtractor = unzip.Extract({ path: tempPath});
unzipExtractor.on("close", function() {
winston.info("Extraction Complete . . .");
// now invoke getAllCollections to read the dir for the .json files
getAllCollections();
});
fs.createReadStream(zipPath).pipe(unzipExtractor);
}
function getAllCollections() {
// the zip has been extracted to tempPath
// this will walk through the dir and read all the files in the tempPath
// it will then save the names of each file in the fileNames[]
// The files are collections from the database in .json format
fs.readdir(tempPath, function(error, results) {
if(error) {
winston.error("error reading dir from restore " + error);
db.close();
if(d) d();
return;
}
else {
winston.info("dir read and contains " + results.length + " files");
for(var x in results) {
if(results[x].indexOf(".zip") < 0) { // remove the .zip archive
fileNames.push(path.win32.basename(results[x], ".json"));
}
if(x == results.length - 1) {
winston.info("fileNames = " + fileNames);
loadJsonData(0);
}
}
}
});
}
function loadJsonData(z) {
//this will load the data in the json files i.e the collections
//it will load the data for a single file per time and save the data to the db
// after completing a file, it will progress to another file
if(z > fileNames.length - 1) {
winston.info("Restoration procedure complete...");
db.close();
fs.remove(tempPath, function(error){
if(error) {
winston.error("error removing temporary path " + error);
if(d) d();
}
else {
winston.verbose("tempPath removed");
if(d) d();
}
});
}
else {
winston.debug("\nload json data invoked " + z);
var collectionName = fileNames[z];
winston.info("collection under processing = " + collectionName + "\n");
fs.readJson(tempPath + "/" + collectionName + ".json", function(error, fileData) {
if(error) {
winston.error("error reading file in Restore " + fileNames[z] + ": " + error);
db.close();
if(d) d();
return; }
else {
// function callback () { loadJsonData(z + 1); }
saveToDb( fileData, 0, collectionName, function() { loadJsonData(z + 1) });
}
}); //end fs
}
}
function saveToDb(fileData, x, collectionName, callback) {
//this method will accept fileData which are the actual records in the collection file
//it will save each record contained in the data to the database
//if the record exits it will update it else it will just create it
//once it's done it will call loadJsonData to load another file for processing
if(x > fileData.length - 1) { winston.info("Done Processing " + collectionName + "\n"); callback(); }
else {
winston.verbose("fileData length = " + fileData.length);
var collection = fileData[x];
// add this data to the database
//change the ID to ObjectID
//if the isObjectID variable is true
if(isObjectID) {
collection._id = new ObjectID.createFromHexString(collection._id);
}
if(collection._created_at) {
collection._created_at = new Date(collection._created_at);
}
if(collection._updated_at) {
collection._updated_at = new Date(collection._updated_at);
}
// winston.info("collection object = " + collection);
db.collection(collectionName).update({"_id":collection._id}, collection, {upsert: true}, function(error, result){
if(error) {
winston.error("error updating document " + collectionName + " : " + error);
if(d) d();
} else {
winston.verbose("update successful " + result);
saveToDb(fileData, (x + 1), collectionName, callback);
}
});
}
}
module.exports = Restore;