-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongodb_backup.js
141 lines (112 loc) · 3.95 KB
/
mongodb_backup.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
var jsonfile = require('jsonfile');
var spawn = require('child_process').spawnSync;
var cronJob = require('cron').CronJob
var fs = require('fs');
var b_backup = false;
//Function to create a new repertory
var newDir = function(){
let currentDate = new Date(); // Current date
let newBackupDir = currentDate.getFullYear() + '-' + (currentDate.getMonth() + 1) + '-' + currentDate.getDate();
path = './backup/' + newBackupDir;
if (!fs.existsSync(path)) {
fs.mkdirSync(path); //to create a repertory named with date
}
let newBackupFile = './backup' + '/' + newBackupDir + '/' + newBackupDir ;
return newBackupFile
}
//function to extract the database
var extractdb = function () {
let newBackupFile = newDir() + '_database.json';
spawn('mongoexport',['--db','det','--collection', 'det', '--out', newBackupFile, '--jsonArray']);
}
//Function to convert the database in 'mongo' format and save document
var Json_database_mongo = function(path) {
let dict = jsonfile.readFileSync(path,'utf8');
let write = {"data":dict};
let newFile = newDir() + '_mongo.json';
jsonfile.writeFileSync(newFile, write, {spaces:2}, function(err) { //to write in a file
if(err) {
return console.log(err);
}
});
}
//Function to convert the 'mongo' format in 'detbelt' format
/* Input : data contains
* - 'path' if it is a file
* - 'items' if it is a variable
*/
var Json_mongo_detBelt_format = function(data) {
if(data.hasOwnProperty('items')) {
var dict = {"data": data.items};
}
else if(data.hasOwnProperty('path')) {
var dict = jsonfile.readFileSync(data.path,'utf8'); //if data is a file (.json)
}
else{
console.log('Input is not an item or a file');
}
var write = {};
var category = [];
var counter = 0;
var tempo = '';
var values = Object.keys(dict.data).map(function(key) {
return dict.data[key];
});
for(i=0; i<values.length; i++){ //for each detergent
if(category.includes(dict.data[i].category) == false){ //if it's a new detergent class (1)
category.push(dict.data[i].category);
delete dict.data[i].category
write[category[counter]] = [dict.data[i]];
counter += 1;
}
else{
for(j=0; j<Object.keys(write).length; j++){
if(dict.data[i].category == Object.keys(write)[j]){ //else (2)
tempo = dict.data[i].category;
delete dict.data[i].category;
write[tempo].push(dict.data[i]);
}
}
}
}
dict = {"data":write};
return dict;
}
//Function to save 'detBelt' format in a file
var Json_mongo_detBelt = function(path) {
dict = Json_mongo_detBelt_format({'path' : path});
var newFile = newDir() + '_detBelt.json';
jsonfile.writeFileSync(newFile, dict, {spaces:2}, function(err) { //to write in a file
if(err) {
return console.log(err);
}
});
}
//Function to execute a function automatically at a certain time
var backup = function(time){
backup_time = time.minutes + ' ' + time.hours + ' * * *';
new cronJob(backup_time, function() { //Every day at 19h00
if(b_backup){
let dir_database = newDir() + '_database.json';
let dir_mongo = newDir() + '_mongo.json';
extractdb();
Json_database_mongo(dir_database);
Json_mongo_detBelt(dir_mongo);
console.log('The extraction of the database is finished !')
b_backup = false;
}
}, null, true, 'Europe/Paris');
}
var control_backup = function(value){
if(typeof(value) == 'boolean'){
b_backup = value;
}
else{
console.log('Warning : error in backup variable')
}
}
module.exports = {
Json_mongo_detBelt_format: Json_mongo_detBelt_format,
backup: backup,
control_backup: control_backup
}