-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
112 lines (109 loc) · 2.69 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
var AWS = require('aws-sdk');
var request = require('request');
var https = require('https');
exports.handler = function () {
var s3 = new AWS.S3({
region: 'us-west-2'
});
var params = {
Bucket: 'backupcollibra'
, Key: 'BackupCollibra.json'
}
s3.getObject(params, function (err, data) {
if (err) console.log(err, err.stack); // an error occurred
else {
var config = JSON.parse(data.Body.toString());
backupData(config);
}
});
}
var backupData = function (config) {
console.log("Starting Backup");
var date = new Date();
var day = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
var authentication = new Buffer(config.un + ':' + config.pw).toString('base64');
var fileName = 'Auto Backup-' + day;
var passObj = {
day: day
, authentication: authentication
, fileName: fileName
, config: config
, user: config.un
, pass: config.pw
}
var options = {
method: 'POST'
, url: 'https://console-byu.collibra.com/rest/backup/3Aafe5bbe3-b000-4755-8704-db6ef366cd33'
, headers: {
'cache-control': 'no-cache'
, 'content-type': 'application/json'
, accept: 'application/json'
, authorization: 'Basic ' + authentication
}
, body: {
name: fileName
, description: 'Automatic Backup-' + day
, database: 'dgc'
, dgcBackupOptionSet: ['CUSTOMIZATIONS']
, repoBackupOptionSet: ['DATA', 'HISTORY', 'CONFIGURATION']
}
, json: true
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
getFile(body, passObj);
});
}
function getFile(body, passObj) {
setTimeout(function () {
putFileInS3(body, passObj);
}, 16000);
}
function putFileInS3(body, passObj) {
console.log('https://console-byu.collibra.com/rest/backup/' + body.id);
const options = {
hostname: 'console-byu.collibra.com'
, port: 443
, path: '/rest/backup/' + body.id
, method: 'GET'
, headers: {
'Authorization': 'Basic ' + passObj.authentication
}
};
const req = https.request(options, (res) => {
console.log('statusCode:', res.statusCode);
console.log('headers:', res.headers);
var bufs = [];
res.on('data', function (d) {
bufs.push(d);
});
res.on('end', function () {
var buf = Buffer.concat(bufs);
s3Put(buf, passObj);
});
});
req.on('error', (e) => {
console.error(e);
});
req.end();
}
function s3Put(buffer, passObj) {
var s3put = new AWS.S3({
region: 'us-west-2'
});
console.log("putting file into s3");
s3put.putObject({
Body: buffer
, Key: passObj.fileName + ".zip"
, Bucket: 'backupcollibra/Backups'
}, function (error, data) {
if (error) {
console.log("error pushing to s3");
}
else {
console.log("success uploading to s3");
}
});
setTimeout(function () {
}, 5000);
}