forked from OriginTrail/ot-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_db_backup.js
129 lines (103 loc) · 4.11 KB
/
add_db_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
const fs = require('fs');
const rimraf = require('rimraf');
const { exec } = require('child_process');
const d = new Date();
const todaysDate = `${d.getFullYear()}-${d.getMonth() + 1}-${d.getDate()}-${d.getHours()}:${d.getMinutes()}:${d.getSeconds()}`;
if (fs.existsSync('./db_backup')) {
console.log('Database backup directory already exists');
} else {
fs.mkdir('./db_backup', (err) => {
if (err) {
console.log('Error : ', err);
}
});
console.log('Database backup directory is made');
}
if (fs.existsSync('./db_backup/arango_db')) {
console.log('Arango database directory already exists');
} else {
fs.mkdir('./db_backup/arango_db', (err) => {
if (err) {
console.log('Error :', err);
}
});
console.log('Arango database backup directory made');
}
if (fs.existsSync('./db_backup/sqllite_db')) {
console.log('SqlLite database directory already exists');
} else {
fs.mkdir('./db_backup/sqllite_db', (err) => {
if (err) {
console.log('Error :', err);
}
});
console.log('SqlLite database backup directory made');
}
if (fs.existsSync(`./db_backup/sqllite_db/${todaysDate}-sqlbackup.db`)) {
console.log(`SqlLite database backup for ${todaysDate} already exist`);
} else {
console.log('Backup of sqlLite database does not exist');
console.log('Creating one');
fs.copyFile('./modules/Database/system.db', `./db_backup/sqllite_db/${todaysDate}-sqlbackup.db`, (err) => {
if (err) throw err;
console.log(`Backup od sqlLite database for ${todaysDate} was created`);
});
}
if (fs.existsSync(`./db_backup/arango_db/arango-db-${todaysDate}`)) {
console.log(`Arango database backup directory for ${todaysDate} already exist`);
} else {
exec(
'arangoexport --type json --collection ot_edges --collection ot_vertices --server.database origintrail --overwrite true --graph-name origintrail --output-directory \'arango-db\' --server.password \'\'',
(error, stdout, stderr) => {
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
if (error !== null) {
console.log(`exec error: ${error}`);
}
},
);
setTimeout(() => {
console.log('Backup of Arango does not exist');
console.log('Creating one');
fs.rename('./arango-db', `./db_backup/arango_db/arango-db-${todaysDate}`, (err) => {
if (err) throw err;
console.log(`Backup od Arango database for ${todaysDate} was created`);
});
}, 1000);
}
setTimeout(() => {
const files = fs.readdirSync('./db_backup/sqllite_db');
var i;
for (i = 0; i < files.length; i += 1) {
const stats = fs.statSync(`./db_backup/sqllite_db/${files[i]}`);
const ctime = new Date((stats.ctime));
const curTime = new Date();
const curTimestamp = curTime.getTime();
const timeStamp = ctime.getTime();
if ((curTimestamp - ((24 * 60 * 60 * 1000) * 7)) > timeStamp) {
rimraf(`./db_backup/sqllite_db/${files[i]}`, () => {
console.log('Successfully deleted backup of sqlLite database that is more than 7 days old ');
});
} else {
console.log('There is no backup of sqlLite database that is more than 7 days old');
}
}
}, 2000);
setTimeout(() => {
const files = fs.readdirSync('./db_backup/arango_db');
var i;
for (i = 0; i < files.length; i += 1) {
const stats = fs.statSync(`./db_backup/arango_db/${files[i]}`);
const ctime = new Date((stats.ctime));
const curTime = new Date();
const curTimestamp = curTime.getTime();
const timeStamp = ctime.getTime();
if ((curTimestamp - ((24 * 60 * 60 * 1000) * 7)) > timeStamp) {
rimraf(`./db_backup/arango_db/${files[i]}`, () => {
console.log('Successfully deleted backup of arango database that is more than 7 days old ');
});
} else {
console.log('There is no backup of arango database that is more than 7 days old ');
}
}
}, 2000);