forked from brightcove/dog-watcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·78 lines (67 loc) · 1.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
77
78
#!/usr/bin/env node
'use strict';
/**
* This file will kick off a single backup when config.backupInterval is not defined. When it is
* defined this file will set up the backup process to run at the specified interval.
*/
var later = require('later'),
log4js = require('log4js'),
yargs = require('yargs'),
logger = log4js.getLogger('dog-watcher');
const args = yargs
.option('config', {
alias: 'c',
description: 'config file',
default: 'config.json',
type: 'string'
})
.argv;
const config = require(`./${args.config}`);
const backup = require('./lib/backup.js')(config);
const
apiKey = config.dataDogApiKey,
appKey = config.dataDogAppKey,
repo = config.gitRepoForBackups,
backupInterval = config.backupInterval;
let backupSchedule;
/**
* Make sure the config file has DataDog keys and a destination repo.
*/
const validateConfig = function() {
if (!apiKey || !appKey) {
logger.error('You must provide DataDog keys in the config.json file.');
process.exit(1);
}
if (!repo) {
logger.error('You must provide a git repo in the config.json file.');
process.exit(1);
}
},
backupCallback = function(error) {
if (error) {
logger.error('Backup failed.', error);
} else {
logger.debug('Backup complete.')
}
};
logger.level = process.env.LOG_LEVEL || 'INFO';
validateConfig();
if (backupInterval) {
backupSchedule = later.parse.cron(backupInterval);
if (backupSchedule.error && backupSchedule !== -1) {
logger.error('There was a problem parsing your interval [' + backupInterval + ']. Please ' +
'make sure that this is a valid cron interval.', backupSchedule.error);
process.exit(1);
}
logger.info('Scheduling backups to for ' + backupInterval);
logger.debug(JSON.stringify(backupSchedule.schedules));
later.setInterval(
function() {
backup(backupCallback);
},
backupSchedule
)
} else {
logger.info('Running a one-time backup.');
backup(backupCallback);
}