-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
50 lines (45 loc) · 1.36 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
// REQUIRES
var cfg = require('./app/config.js')
, bck = require('./app/backup.js')
, ftp = require('./app/ftp.js')
, CronJob = require('cron').CronJob;
// POLYFILLS
require('./extras/date.format.js');
// GLOBAL VARIABLES
var configFilePath = '/usr/cfg/config.json';
// PROCESS
var doBackup = function(config, shutdown) {
console.log('start backup at: ' + (new Date()).format('yyyy-mm-dd HH:MM:ss'));
cfg.setDefaults(config)
.then(bck.check)
.then(bck.compress)
.then(bck.crypt)
.then(ftp.upload)
.then(ftp.clean)
.then(bck.clean)
.then(function(result) {
console.log('backup done! (' + (new Date()).format('yyyy-mm-dd HH:MM:ss') + ')');
})
.catch(function(err) {
console.log('backup rejected! (' + (new Date()).format('yyyy-mm-dd HH:MM:ss') + ')');
console.log(err);
})
.finally(function () {
if (shutdown === true)
process.exit(0);
});
};
cfg.toJSON(configFilePath)
.then(cfg.validate)
.then(function(config) {
if (config.cron.active) {
console.log('backup cron time: ' + config.cron.time);
var job = new CronJob({
cronTime: config.cron.time,
onTick: function() { doBackup(config, false); }
});
job.start();
} else {
doBackup(config, true);
}
});