-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoperations.js
117 lines (97 loc) · 3.83 KB
/
operations.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
113
114
115
116
117
/**
* This file is where the backup and restore operations are stored.
*
*/
var couchbackup = require('@cloudant/couchbackup');
var environment = require('./environment');
var pkgcloud = require('pkgcloud');
var fs = require('fs');
var AWS = require('ibm-cos-sdk');
var util = require('util');
exports.backupCloudObjectStorage = function() {
console.log("Executing Cloudant Backup operation...");
var opts, config;
if (environment.VCAP_SERVICES["cloudantNoSQLDB"] || environment.VCAP_SERVICES["cloudantNoSQLDB Dedicated"]) {
console.log("Using Bound Cloudant");
var vcapServices = environment.VCAP_SERVICES["cloudantNoSQLDB"] || environment.VCAP_SERVICES["cloudantNoSQLDB Dedicated"];
credentials = vcapServices[0].credentials;
if (credentials.apikey) {
opts = { iamApiKey: credentials.apikey };
console.log("Using IAM authentication for Cloudant");
var sourceUrl = 'https://' + credentials.host + '/' + database_name;
} else { // Using legacy auth with username password in URL
console.log("Using legacy authentication for Cloudant");
opts = {};
var sourceUrl = credentials.url + '/' + database_name;
};
} else {
console.log("Using Remote Cloudant");
if (environment.cloudant_apikey) { // Using IAM auth for Cloudant
opts = { iamApiKey: environment.cloudant_apikey };
console.log("Using IAM authentication for Cloudant");
var sourceUrl = 'https://' + environment.cloudant_host + '/' + database_name;
} else { // Using legacy auth with username password in URL
console.log("Using legacy authentication for Cloudant");
opts = {};
var sourceUrl = environment.cloudant_url + '/' + database_name;
};
}
couchbackup.backup(sourceUrl, fs.createWriteStream("backup.txt"), opts, function(err, obj) {
if (err) {
console.log("Error backing up from Cloudant: ", err);
} else {
var config;
//if bound - pull in values from Bluemix VCAP credentials
if (environment.VCAP_SERVICES["cloud-object-storage"]) {
console.log("Using Bound Object Storage");
var vcapServices = environment.VCAP_SERVICES["cloud-object-storage"];
credentials = vcapServices[0].credentials;
config = {
apiKeyId: credentials.apikey,
serviceInstanceId: credentials.resource_instance_id
};
//if remote - Pull in values from manifest
} else {
console.log("Using Remote Cloud Object Storage");
config = {
apiKeyId: environment.cos_api_key,
serviceInstanceId: environment.cos_resource_instance_id
};
}
config.endpoint = environment.cos_endpoint_url;
config.ibmAuthEndpoint = 'https://iam.ng.bluemix.net/oidc/token';
//Authenticate
var cos = new AWS.S3(config);
//Upload file
var fs = require('fs');
var date = new Date();
var dbName = environment.database_name.replace(/_/g, '-');
var fileName = dbName + "-" + date.toISOString() + ".json";
var bucketName = "cloudant-db-" + dbName + "-" + date.toLocaleString('en-us', { year: 'numeric' }) + "-" + date.toLocaleString('en-us', { month: '2-digit' });
var BucketLocConstraint = environment.cos_region;
cos.createBucket({
Bucket: bucketName,
CreateBucketConfiguration: {
LocationConstraint: BucketLocConstraint
},
}, function(err, bucket) {
if (err && err.statusCode != 409) {
console.error(err);
} else {
var rStream = fs.createReadStream('backup.txt');
cos.putObject({
Bucket: bucketName,
Key: fileName,
Body: rStream
}, function(err, data) {
if (err) {
console.error(err);
} else {
console.log("File uploaded successfully",data);
}
});
}
});
}
});
};