-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
96 lines (83 loc) · 3.96 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
'use strict';
var dotenvObj = require('dotenv');
var azure = require('azure-storage');
var backup = require('mongodb-backup');
var fs = require('fs');
// load configuration from environment or file.
dotenvObj.config();
var logger = require('./configurations/logger');
var azureConfig = require('./configurations/azure');
var databaseConfig = require('./configurations/database');
var azureStorageAccount = azureConfig.storageAccount;
var azureAccessKey = azureConfig.accessKey;
var azureContainerName = azureConfig.container;
var retryOperations = new azure.ExponentialRetryPolicyFilter();
var blobService = azure.createBlobService(azureStorageAccount, azureAccessKey).withFilter(retryOperations);
var timeStamp = new Date().toISOString(); // backup file name
var temporaryFileLocation = __dirname + '/backups/';
var temporaryFileName = temporaryFileLocation + timeStamp + '.tar';
/**********************************************************************************************************************
* Clear temporary file stored in 'temporaryFileLocation'.
**********************************************************************************************************************/
function clearTempLocation() {
fs.stat(temporaryFileName, function (err) {
if (err) {
logger.log('error', timeStamp + ' error running backup command.', error);
return;
}
fs.unlink(temporaryFileName, function (err) {
if (err) {
logger.log('error', timeStamp + ' error running backup command.', error);
return;
}
logger.log('info', timeStamp + ' temporary file removed');
logger.log('info', timeStamp + ' COMPLETED SUCCESSFULLY');
});
});
}
/**********************************************************************************************************************
* Once all collections are downloaded to disk as .tar file.
* Upload .tar file to azure account.
**********************************************************************************************************************/
function storeInAzureAccount(err) {
if (err) {
logger.log('error', timeStamp + ' error running backup command.', error);
return;
}
logger.log('info', timeStamp + ' backup command successful. Going to push to azure account.');
blobService.createContainerIfNotExists(
azureContainerName,
function (error,
result,
response) {
if (error) {
logger.log('error', timeStamp + ' azure container:' + azureContainerName + '. Error:', error);
return;
}
logger.log('info', timeStamp + ' azure container:' + azureContainerName + '. Ready');
blobService
.createBlockBlobFromLocalFile(
azureContainerName,
timeStamp,
temporaryFileName,
function (error,
result,
response) {
if (error) {
logger.log('error', timeStamp + ' could not write to azure' + azureContainerName + '. Error:', error);
return;
}
clearTempLocation();
});
});
}
/**********************************************************************************************************************
* Initiate backup.
**********************************************************************************************************************/
logger.log('info', timeStamp + ' INITIATING...');
backup({
uri: `mongodb://${encodeURIComponent(databaseConfig.DB_USER)}:${encodeURIComponent(databaseConfig.DB_PASS)}@${databaseConfig.DB_HOST}:${databaseConfig.DB_PORT}/${databaseConfig.DB_NAME}`,
root: temporaryFileLocation,
tar: timeStamp + '.tar', // save backup into this tar file
callback: storeInAzureAccount
});