forked from burtontanner/cron-mysql-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
234 lines (205 loc) · 8.83 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
const mysqldump = require('mysqldump');
const cron = require('node-cron');
const nodemailer = require('nodemailer');
const path = require('path');
const fs = require('fs');
const diskfree = require('diskfree');
let fsPromises = fs.promises;
let setUpCronJobs = (options) => {
validateOptions(options);
// For sanity do a single attempt on the first schedule
attemptBackup(options, 0).catch(console.error);
for (let i in options.schedules) {
cron.schedule(options.schedules[i].cronSchedule, () => {
attemptBackup(options, i).catch(console.error);
});
}
if (options.sendDailyReportEmail) {
// Daily success email
cron.schedule("*/2 * * * *", () => {
//cron.schedule("0 0 * * *", () => {
sendDailyReportEmail(options);
})
}
};
let attemptBackup = async (options, index) => {
try {
await validateFreeDiskSpace(options);
let backupStartTime = Date.now();
let backupFileName = await backupDatabase(options.connection, options.schedules[index].directory);
let backupDuation = Date.now() - backupStartTime;
await removeOldestBackups(options.schedules[index].maxBackups, options.schedules[index].directory);
console.log(`Backup Complete (${backupFileName}) - Duration: ${(backupDuation / 1000).toFixed(3)} seconds`);
options.schedules[index].successHistory.push({ backupFileName, backupDuation, time: new Date() });
} catch(e) {
console.error(e);
return sendFailureEmail(options.sendTo, options.sendFrom, options.sendFromPassword, e, options.connection.database);
}
};
let backupList = async (directory)=>{
await ensureDirectoryExists(directory);
let files = await fsPromises.readdir(directory);
return files;
};
let removeOldestBackups = async (maxBackups, directory) => {
await ensureDirectoryExists(directory);
let files = await fsPromises.readdir(directory);
files = files.sort(sortFiles);
let count = 0;
let now = Date.now();
for(let i in files){
if(++count > maxBackups){
await fsPromises.unlink(path.join(directory, files[i]));
console.log("Removing Backup", path.join(directory, files[i]));
}
}
};
let backupDatabase = async (connection, directory) => {
await ensureDirectoryExists(directory);
let fileName = `${Date.now()}.sql.gz`;
let result = await mysqldump({
connection,
dumpToFile: path.join(directory, fileName),
compressFile: true
});
return fileName;
};
let validateFreeDiskSpace = async (options) => {
let largestBackupBytes = 0;
for (let i in options.schedules) {
let backupSizeBytes = await largestBackupSizeBytes(options.schedules[i].directory);
if (backupSizeBytes > largestBackupBytes)
largestBackupBytes = backupSizeBytes;
}
let free = await freeSpaceBytes();
let total = await totalSpaceBytes();
console.log(`Free space ${(free / (1024*1024*1024)).toFixed(3)} GiB`);
console.log(`Total space ${(total / (1024*1024*1024)).toFixed(3)} GiB`);
console.log(`Largest backup ${(largestBackupBytes / (1024*1024)).toFixed(3)} MiB`, );
if((free / total) < 0.1) { // keep 10% free
throw new Error("Out of disk space.")
}
};
let freeSpaceBytes = ()=>{
return new Promise((resolve, reject)=>{
diskfree.check('/', (err,stats)=>{
if(err) reject(err);
resolve(stats.available);
});
});
};
let totalSpaceBytes = ()=>{
return new Promise((resolve, reject)=>{
diskfree.check('/', (err,stats)=>{
if(err) reject(err);
resolve(stats.total);
});
});
};
let largestBackupSizeBytes = async (directory) => {
await ensureDirectoryExists(directory);
let files = await fsPromises.readdir(directory);
let largest = 0;
for (let i in files){
const stats = await fsPromises.stat(path.join(directory, files[i]));
if(stats.size < largest) continue;
largest = stats.size;
}
if(!largest) return 0;
return largest;
};
let sendDailyReportEmail = async (options) => {
let averageBackupTime = 0;
let backupList = [];
for (let i in options.schedules) {
backupList = backupList.concat(options.schedules[i].successHistory);
}
let backupMessageHtml = `<!DOCTYPE html><html><body><style>body { color: #444; }</style><h2>The following is a report of your backup history</h2>`;
let backupListHtml = `<table style="border-collapse:collapse; border: 1px solid #a3a3a3;"><tr style="border: 1px solid #a3a3a3;">
<th style="border: 1px solid #a3a3a3; padding: 5px;">Date of Backup</th><th style="border: 1px solid #a3a3a3; padding: 5px;">Duration</th>
<th style="border: 1px solid #a3a3a3; padding: 5px;">Backup Filename</th></tr>`;
for (let i in backupList) {
averageBackupTime += backupList[i].backupDuation;
backupListHtml += `<tr style="border: 1px solid #a3a3a3; padding: 5px;">
<td style="border: 1px solid #a3a3a3; padding: 5px;">${backupList[i].time}</td>
<td style="border: 1px solid #a3a3a3; padding: 5px;">${(backupList[i].backupDuation/1000).toFixed(3)} seconds</td>
<td style="border: 1px solid #a3a3a3; padding: 5px;">${backupList[i].backupFileName}</td></tr>`;
}
backupListHtml += `</table>`;
backupMessageHtml += `<p>Average Backup Time: ${(averageBackupTime/(backupList.length*1000)).toFixed(3)} seconds</p><p>Total Backup Count: ${backupList.length}</p>`;
backupMessageHtml += backupListHtml;
backupMessageHtml += `</body></html>`;
await sendEmail(options.sendTo, options.sendFrom, options.sendFromPassword, `${options.connection.database} - Backup Complete`, backupMessageHtml);
};
let sendSuccessEmail = async (sendTo, sendFrom, sendFromPassword, backupFileName, directory, database) => {
let backups = await backupList(directory);
await sendEmail(sendTo, sendFrom, sendFromPassword, `${database} - Backup Complete`, `Your last database backup was on ${new Date()} with filename: ${backupFileName}.<br> You have ${backups.length} backups.<br> ${backups.join('<br>')}`);
};
let sendFailureEmail = async (sendTo, sendFrom, sendFromPassword, e, database) => {
await sendEmail(sendTo, sendFrom, sendFromPassword, database + " Backup Failed", "Here is the error message <br> " +e + JSON.stringify(e));
};
let sendEmail = async (sendTo, sendFrom, sendFromPassword, subject, body)=>{
let mailConfig = {
service: 'gmail',
auth: {
user: sendFrom,
pass: sendFromPassword
}
};
let transporter = nodemailer.createTransport(mailConfig);
let mailOptions = {
from: '"cron-mysql-backup" <'+sendFrom+'>', // sender address
to: sendTo, // list of receivers
subject, // Subject line
html: body
};
let info = await transporter.sendMail(mailOptions);
console.log('Email Sent', info.response);
};
let validateOptions = (options) => {
let requiredFields = ['schedules', 'sendTo', 'connection', 'sendFrom', 'sendFromPassword', 'sendDailyReportEmail'];
let requiredConnectionFields = ['host', 'user', 'password', 'database'];
let missing = validateObj(options, requiredFields);
let missingConnection = validateObj(options.connection, requiredConnectionFields);
let error = '';
if(missing.length > 0){
error = 'You need to include '+ missing.join(', ')+ ' in the options object.\n';
}
if(missingConnection.length > 0){
error += 'You need to include '+ missingConnection.join(', ')+ ' in the options.connection object.\n';
}
if(options.schedules.length == 0) {
error += 'You did not specify any cron schedules.\n';
} else {
let requiredScheduleFields = ['cronSchedule', 'directory', 'maxBackups'];
let missingScheduleFields = [];
for (let i in options.schedules) {
options.schedules[i].successHistory = [];
missingScheduleFields = missingScheduleFields.concat(validateObj(options.schedules[i], requiredScheduleFields));
}
if(missingScheduleFields.length > 0){
error += 'You need to include '+ missingScheduleFields.join(', ')+ ' in the options.schedules object.\n';
}
}
if(error)throw new Error(error);
};
let validateObj = (object, required) => {
let missing = [];
for(let i in required){
let opt = required[i];
if(object[opt]) continue;
missing.push(opt);
}
return missing;
};
let sortFiles = (a, b)=>{
let aTime = parseInt(a.replace('.sql', ''));
let bTime = parseInt(b.replace('.sql', ''));
return bTime - aTime;
};
let ensureDirectoryExists = async (directory) => {
if (!fs.existsSync(directory)) {
await fsPromises.mkdir(directory);
}
};
module.exports = setUpCronJobs;