简体   繁体   English

如何导出 Google Storage 中 Cloud SQL 数据库的备份?

[英]How to export backup of Cloud SQL database in Google Storage?

I have a GAE project and for that to store the data i have used Cloud SQL database.我有一个 GAE 项目,为了存储我使用 Cloud SQL 数据库的数据。 I need to take on demand backup of my Cloud SQL database and the backup should be stored in google storage.我需要对我的云 SQL 数据库进行按需备份,备份应该存储在谷歌存储中。

Can anyone help me to how can i do this programatically using JAVA?谁能帮助我如何使用 JAVA 以编程方式执行此操作?

You cannot export a backup (on-demand or automated), as documented here . 您不能导出备份(按需备份或自动备份),如此处所述 What you can do however is to export your data to a SQL dump file or a CSV file, depending on your use case, and store it on Cloud Storage. 但是,您可以根据自己的使用情况将数据导出到SQL转储文件或CSV文件中,并将其存储在Cloud Storage中。

There is no Java utility to perform an export but Google exposes an API endpoint for you to trigger an export. 没有Java实用程序可以执行导出,但是Google公开了一个API端点供您触发导出。 Here's an example request using standard curl tool: 这是使用标准curl工具的示例请求:

curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \
     --header 'Content-Type: application/json' \
     --data '{"exportContext":
                {"fileType": "SQL",
                 "uri": "gs://<BUCKET_NAME>/<PATH_TO_DUMP_FILE>",
                 "databases": ["<DATABASE_NAME1>", "<DATABASE_NAME2>"] }}' \
   -X POST \
   https://www.googleapis.com/sql/v1beta4/projects/[PROJECT-ID]/instances/[INSTANCE_NAME]/export

You'll find further details about exporting data in the documentation here . 您可以在此处的文档中找到有关导出数据的更多详细信息。

While LundinCast's answer is still correct und provides helpful information it is outdated to some extend.虽然 LundinCast 的回答仍然正确并提供了有用的信息,但它在某种程度上已经过时了。

New Java Utilities introduced新 Java 实用程序介绍

Google now privides a Java Utilities for it which can be be found here: https://cloud.google.com/sql/docs/mysql/admin-api/rest/v1beta4/instances/export谷歌现在为它提供了一个 Java 实用程序,可以在这里找到: https://cloud.google.com/sql/docs/mysql/admin-api/rest/v1beta4/instances/export

In my opinion the example code given there lacks the minimum code for how to set the request body.在我看来,那里给出的示例代码缺少如何设置请求正文的最少代码。 So I provide a simple snippet here:所以我在这里提供一个简单的片段:

var exportContext = new ExportContext();
// gs:// stands for the gsutil tool, the filename should have "gz" as file extension
exportContext.setUri("gs://<BUCKET_NAME>/<BACKUP_FILENAME.gz>");     exportContext.setDatabases(List.of("<DATABASE_NAME>"));
exportContext.setFileType("SQL");

var requestBody = new InstancesExportRequest();
requestBody.setExportContext(exportContext);

Neccessary permissions and automatisation必要的权限和自动化

The Java Utility might not work out of the box because you'll need to set some permissions/roles correctly. Java 实用程序可能无法开箱即用,因为您需要正确设置一些权限/角色。 A complete recipe (which also covers automatisation eg every day) can be found here: https://cloud.google.com/architecture/scheduling-cloud-sql-database-exports-using-cloud-scheduler .可以在此处找到完整的方法(也包括每天的自动化): https://cloud.google.com/architecture/scheduling-cloud-sql-database-exports-using-cloud-scheduler Unfortunately (as of 2022-04-21) the Python script is outdated (and also examples for other languages are missing).不幸的是(截至 2022 年 4 月 21 日)Python 脚本已过时(并且缺少其他语言的示例)。 Use the first link given in my answer instead to see newer implementation.使用我的回答中给出的第一个链接来查看更新的实现。

Testing upfront via gcloud通过 gcloud 预先测试

To test the export step it might even help to test it via gcloud first:要测试导出步骤,它甚至可能有助于先通过 gcloud 进行测试:

gcloud sql export sql <CLOUD_SQL_INSTANCE> gs://<BUCKET_NAME>/<BACKUP_FILENAME>.gz --database=<DATABASE_NAME>

(you might have to set you project first via gcloud config set project <PROJECT_NAME> ) (您可能必须先通过gcloud config set project <PROJECT_NAME>设置您的项目)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM