Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
151 lines (138 loc) · 4.07 KB
/
Copy pathindex.js
File metadata and controls
151 lines (138 loc) · 4.07 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const fs = require("fs");
const { JWT } = require('google-auth-library');
/**
* This is a helper for restoring backups accross projects according to
* https://stackoverflow.com/questions/48620009/share-and-restore-cloud-sql-backup-files-across-account-project
* and
* https://cloud.google.com/sql/docs/mysql/backup-recovery/restoring#projectid
*
* Requires a service key JSON that is authorised
*/
class SqlRestore {
/**
* Must be called first to prepare authorisation
* @param {string} path Path to json service key
*/
authorizeJwt(path) {
const serviceAccount = JSON.parse(fs.readFileSync(path, 'utf8'));
const googleJWTClient = new JWT(
serviceAccount.client_email,
null,
serviceAccount.private_key,
['https://www.googleapis.com/auth/cloud-platform'],
null
);
this.googleClient = googleJWTClient;
}
/**
* List all backups for an instance
* @param {string} opts.projectId
* @param {string} opts.instanceId
* @returns {object[]} List of backup runs for the instance
*/
async listBackups({ projectId, instanceId }) {
const url = `https://www.googleapis.com/sql/v1beta4/projects/${projectId}/instances/${instanceId}/backupRuns`;
try {
const response = await this.googleClient.request({
url,
method: 'GET',
});
return response.data.items;
} catch (error) {
console.error(error);
throw error;
}
}
/**
* Restore a backup from one instance to another instance
* @param {string} opts.sourceProjectId
* @param {string} opts.targetProjectId
* @param {string} opts.sourceInstanceId
* @param {string} opts.targetInstanceId
* @param {string} opts.backupRunId
*/
async restoreBackup(opts) {
const {
sourceProjectId,
targetProjectId,
sourceInstanceId,
targetInstanceId,
backupRunId,
} = opts;
const url = `https://www.googleapis.com/sql/v1beta4/projects/${targetProjectId}/instances/${targetInstanceId}/restoreBackup`;
const data = {
restoreBackupContext: {
backupRunId,
project: sourceProjectId,
instanceId: sourceInstanceId,
},
};
const result = await this.googleClient.request({
url,
data,
method: 'POST',
});
return result.data;
}
/**
* Shortcut to find the latest successful backup from the source and restore it to target
* @param {string} opts.sourceProjectId
* @param {string} opts.targetProjectId
* @param {string} opts.sourceInstanceId
* @param {string} opts.targetInstanceId
* @returns {object} An operation object
*/
async restoreLatestBackup(opts) {
const { sourceProjectId, sourceInstanceId } = opts;
const backups = await this.listBackups({
projectId: sourceProjectId,
instanceId: sourceInstanceId,
});
if (!backups.length) {
throw new Error(
'No backups were found on the source, cannot restore'
);
}
const sortedBackups = backups
.filter((b) => b.status === 'SUCCESSFUL')
.sort((a, b) => (a.startTime > b.startTime ? -1 : 1));
return this.restoreBackup({
...opts,
backupRunId: sortedBackups[0].id,
});
}
/**
* Get operations for a project (and optionally an instance)
* @param {string} opts.projectId
* @param {string} opts.instanceId optional
* @returns {object[]} Operations
*/
async listOperations({ projectId, instanceId, maxResults }) {
if (!maxResults) maxResults = 10;
let url = `https://www.googleapis.com/sql/v1beta4/projects/${projectId}/operations?maxResults=${maxResults}`;
if (instanceId) url += `&instance=${instanceId}`;
const operations = await this.googleClient.request({
url,
method: 'GET',
});
return operations.data.items;
}
/**
* Check the status of a restore operation
* @param {object} operation Returned by restore
* @returns {object} Updated operation
* @throws {Error} if the operation contains an error
*/
async checkOperationStatus(operation) {
const url = operation.selfLink;
const update = await this.googleClient.request({ url, method: 'GET' });
const data = update.data;
if (data.error) {
const error = new Error(data.error[0].errors[0].message);
error.original = data.error;
throw error;
}
return data;
}
}
module.exports = SqlRestore;