Last active
June 6, 2020 13:54
-
-
Save LaureRC/edaabaf85b45fc78999e410caec8944d to your computer and use it in GitHub Desktop.
Backup cloud function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { google } = require("googleapis"); | |
const { auth } = require("google-auth-library"); | |
var sqladmin = google.sqladmin("v1beta4"); | |
// Based on https://medium.com/@kennethteh90/how-to-schedule-daily-cloud-sql-export-to-google-cloud-storage-4c1bd360af06 | |
exports.backup = async (_req, res) => { | |
const authRes = await auth.getApplicationDefault(); | |
let authClient = authRes.credential; | |
var request = { | |
project: process.env.PROJECT_ID, | |
instance: process.env.DB_INSTANCE_NAME, | |
resource: { | |
exportContext: { | |
kind: "sql#exportContext", | |
fileType: "SQL", | |
uri: `gs://${process.env.BUCKET_NAME}/${(new Date()).toISOString()}-backup.gz`, | |
databases: [process.env.DB_NAME_TO_EXPORT] | |
} | |
}, | |
auth: authClient | |
}; | |
try { | |
const result = await sqladmin.instances.export(request); | |
console.log(result); | |
res.status(200).send("Command completed", null, result); | |
} catch(err) { | |
console.log(err); | |
res.status(500).send("Error", err); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment