-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
76 lines (71 loc) · 2.99 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
const config = require('./config.json');
const schedule = require('node-schedule');
const aws = require('aws-sdk');
const fs = require('fs');
const execSync = require('child_process').execSync;
if (config.enabledHourlyUpload) {
schedule.scheduleJob('1 * * * *', function () {
if (config.limitHoursTo && config.limitHoursTo.length !== 0) {
if (config.limitHoursTo.includes(new Date().getHours().toString())) backup();
} else backup();
});
}
const s3bucket = new aws.S3({
accessKeyId: config.accessKeyId,
secretAccessKey: config.secretAccessKey,
s3BucketEndpoint: config.s3BucketEndpoint,
endpoint: config.endpoint,
region: config.bucketRegion
});
async function backup() {
return new Promise((async resolve => {
for (const command of config.runCommandsBeforeExecution || []) {
console.log(`Running ${command}`);
try {
await execSync(command);
} catch (e) {
console.warn(`Error executing ${command}`);
}
console.log(`Finished running ${command}`);
}
console.log('Backing up...');
const date = new Date();
const filename = `backup-${config.prefix}-${date.getDate()}.${date.getMonth() + 1}.${date.getFullYear()}-${date.getHours()}:${date.getMinutes()}.zip`;
for (const folder of config.folders) {
console.log(`Adding ${folder} to zip...`);
try {
await execSync(`cd ${config.rootDir || '.'} && zip -q -P \"${config.key}\" -r ${__dirname}/${filename} ${folder}`);
console.log(`Added ${folder}`);
} catch (e) {
console.error(`Error adding `, e);
}
}
const readStream = fs.createReadStream(`./${filename}`);
s3bucket.upload({
Bucket: config.bucketID,
Key: config.path + '/' + `${filename}`,
Body: readStream,
ServerSideEncryption: 'AES256',
StorageClass: 'STANDARD_IA'
}, function (err) {
fs.unlinkSync(`${__dirname}/${filename}`);
if (err) return console.error(`Error uploading:`, err);
console.log('Uploaded successfully');
});
if (config.deleteItems) {
s3bucket.listObjects({Bucket: config.bucketID, Prefix: `${config.path}`}, (err, res) => {
if (err) return;
for (const object of res.Contents) {
if (object.LastModified.getTime() + (config.daysBeforeDeletion * 86400000) < new Date().getTime()) {
console.log(`Deleting ${object.Key}...`);
s3bucket.deleteObject({Bucket: config.bucketID, Key: object.Key}, (err, data) => {
if (err) return console.error(`Error deleting: ${err}`);
console.log(`Successfully deleted`);
});
}
}
});
}
}));
}
if (config.backupOnStart) backup();