本页面介绍了如何通过将 Cloud SQL 备份复制到 AlloyDB for PostgreSQL 免费试用集群来迁移 Cloud SQL for PostgreSQL 实例。通过将 Cloud SQL 备份复制到 AlloyDB 集群,您可以将数据快速加载到 AlloyDB for PostgreSQL 中,以便评估或迁移到 AlloyDB。
本页面假定您熟悉 Cloud SQL。如果您是刚开始接触 AlloyDB,请参阅 AlloyDB 概览。
如需了解如何使用持续数据复制将数据从 Cloud SQL 迁移到 AlloyDB,请参阅 Database Migration Service for PostgreSQL 到 AlloyDB。
不支持:
- 跨项目和跨区域恢复
- 使用客户管理的加密密钥 (CMEK) 的实例
- 使用 Identity and Access Management (IAM) 群组身份验证的实例
准备工作
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the AlloyDB, Compute Engine, and Service Networking APIs.
- 确保您拥有以下各项:
- 必要的 Identity and Access Management (IAM) 权限
- 大小小于 1TB 的 Cloud SQL 备份
- AlloyDB 支持的 PostgreSQL 版本
所需的角色
如需获得将 Cloud SQL for PostgreSQL 备份复制到 AlloyDB 集群所需的权限,请向自己授予项目的以下 IAM 角色:
- Viewer (
roles/viewer
) - Cloud AlloyDB Admin (
roles/alloydb.admin
)
将 Cloud SQL 备份复制到 AlloyDB 集群
将 Cloud SQL 备份复制到 AlloyDB免费试用集群会在 AlloyDB 上将备份恢复为同一 PostgreSQL 版本,例如,PostgreSQL 14 Cloud SQL 备份会恢复为 PostgreSQL 14 免费试用集群。请注意,扩展程序版本和 PostgreSQL 次要版本可能会不同。
从 Cloud SQL 复制备份仅支持 AlloyDB免费试用 集群中支持的那些项的配置。
如需将 Cloud SQL 备份复制到 AlloyDB免费试用集群,请按以下步骤操作:
控制台
- 在 Google Cloud 控制台中,前往集群页面。
- 如果您尚未预配集群,请点击开始免费试用。如果您有现有集群,请点击迁移数据。
- 点击从 Cloud SQL 备份复制。
- 在从 Cloud SQL 备份复制页面中,启用所需的 API。如果您已启用相应 API,则无需重新启用这些 API。
- 在选择集群类型页面中,点击免费试用集群。如果您不符合免费试用集群的条件,此字段会处于灰显状态。
- 点击选择集群类型。
- 选择要从中进行复制的 Cloud SQL 实例,然后点击选择实例。您可以过滤 Cloud SQL 实例。
系统仅显示兼容的数据库版本。副本没有备份,不会显示在可用实例列表中。 - 选择要从中导入的备份,然后点击选择备份。此页面会显示最近 1,000 个备份。
- 在 Create your free cluster(创建免费集群)页面中,输入集群 ID 和网络信息。
- 点击开始免费试用。
操作完成后,系统会自动创建主实例。您会看到一个新的 AlloyDB 集群,其中包含从您选择的 Cloud SQL for PostgreSQL 备份复制的数据。
将 Cloud SQL for PostgreSQL 备份复制到 AlloyDB 集群时,数据库标志和资源级别权限不会自动迁移。复制完成后,您必须手动设置这些标志和权限。
您可以在 Create your free cluster(创建免费集群)页面上查看 AlloyDB 支持的 Cloud SQL for PostgreSQL 数据库标志。
REST API
- 如需获取要从中恢复备份的实例的备份列表,请调用
backupsRuns.list
方法:
GET "https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_ID/backupRuns"
替换以下内容:
PROJECT_ID
:项目 ID。INSTANCE_ID
:实例 ID。
请求 JSON 正文:
{ "kind": string, "items": [ { object (BackupRun) } ], "nextPageToken": string }
如需发送您的请求,请使用以下方式之一:
curl(Linux、macOS 或 Cloud Shell)
以下命令假定您已使用您的用户账号通过运行
gcloud init
或gcloud auth login
登录Google Cloud CLI
,或者使用了Cloud Shell
,这会使您自动登录gcloud CLI
。您可以运行
gcloud auth list
来检查活跃的账号。将请求正文保存在名为
request.json
的文件中,然后执行以下命令:curl -X GET \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ "https://sqladmin.googleapis.com//sql/v1beta4/projects/PROJECT_ID/instances/INSTANCE_ID/backupRuns"
PowerShell (Windows)
以下命令假定您已使用您的用户账号通过运行
gcloud init
或gcloud auth login
登录gcloud CLI
,或者使用了Cloud Shell
,这会使您自动登录gcloud CLI
。您可以运行
gcloud auth list
来检查活跃的账号。将请求正文保存在名为
request.json
的文件中,然后执行以下命令:$cred = gcloud auth print-access-token $headers = @{ "Authorization" = "Bearer $cred" } Invoke-WebRequest ` -Method GET ` -Headers $headers ` -ContentType: "application/json; charset=utf-8" ` -Uri "https://sqladmin.googleapis.com//sql/v1beta4/projects/PROJECT_ID/instances/INSTANCE_ID/backupRuns"|Select-Object -Expand Content
您会收到类似以下内容的 JSON 响应:
响应
如果成功,响应正文会包含一个
API 响应会以数组列表的形式返回实例的备份列表,其中包含BackupRun
列表。"id": string,
。 - 调用 restoreFromCloudSQL 方法:
POST https://alloydb.googleapis.com/v1beta/projects/PROJECT_ID/locations/REGION/clusters:restoreFromCloudSQL
替换以下内容:
PROJECT_ID
:项目 ID。REGION
:部署 AlloyDB 集群的区域。
请求 JSON 正文:
{ "clusterId": string, "cluster": { "databaseVersion": "POSTGRES_14/POSTGRES_15/POSTGRES_16", "subscriptionType": "TRIAL" }, // Union field source can be only one of the following: "cloudsqlBackupRunSource": { object (CloudSQLBackupRunSource) } // End of list of possible types for union field source. }
如需发送您的请求,请使用以下方式之一:
curl(Linux、macOS 或 Cloud Shell)
以下命令假定您已使用您的用户账号通过运行
gcloud init
或gcloud auth login
登录gcloud CLI
,或者使用了Cloud Shell
,这会使您自动登录gcloud CLI
。您可以运行
gcloud auth list
来检查活跃的账号。将请求正文保存在名为
request.json
的文件中,然后执行以下命令:curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ -d @request.json \ "https://alloydb.googleapis.com/v1beta/projects/PROJECT_ID/locations/REGION/clusters:restoreFromCloudSQL"
PowerShell (Windows)
以下命令假定您已使用您的用户账号通过运行
gcloud init
或gcloudauth login
登录gcloud CLI
,或者使用了Cloud Shell
,这会使您自动登录gcloud CLI
。您可以运行
gcloud auth list
来检查活跃的账号。将请求正文保存在名为
request.json
的文件中,然后执行以下命令:$cred = gcloud auth print-access-token $headers = @{ "Authorization" = "Bearer $cred" } Invoke-WebRequest ` -Method POST ` -Headers $headers ` -ContentType: "application/json; charset=utf-8" ` -InFile request.json ` -Uri "https://alloydb.googleapis.com/v1beta/projects/PROJECT_ID/locations/REGION/clusters:restoreFromCloudSQL"|Select-Object -Expand Content
您会收到类似以下内容的 JSON 响应:
响应
如果成功,则响应正文包含一个
Operation
实例。系统会在指定的项目和位置创建一个新集群,并使用从 CloudSQLBackupRunSource 消息中指向的备份恢复的卷。
- 当集群处于
READY
状态时,通过调用projects.locationsinstances.create
方法创建主实例:
POST https://alloydb.googleapis.com/v1beta/{parent=projects/PROJECT_ID/locations/REGION/clusters/CLUSTER_ID}/instances
替换以下内容:
PROJECT_ID
:项目 ID。REGION
:部署 AlloyDB 集群的区域。CLUSTER_ID
:集群 ID。
请求 JSON 正文:
{ "instanceId": "string", "instance": { "object": "Instance" } }
请求正文包含一个
Instance
实例。如需发送您的请求,请使用以下方式之一:
curl(Linux、macOS 或 Cloud Shell)
以下命令假定您已使用您的用户账号通过运行
gcloud init
或gcloud auth login
登录gcloud CLI
,或者使用了Cloud Shell
,这会使您自动登录gcloud CLI
。您可以运行
gcloud auth list
来检查活跃的账号。将请求正文保存在名为
request.json
的文件中,然后执行以下命令:curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ -d @request.json \ "https://alloydb.googleapis.com/v1beta/projects/PROJECT_ID/locations/REGION/clusters/CLUSTER_ID/instances"
PowerShell (Windows)
以下命令假定您已使用您的用户账号通过运行
gcloud init
或gcloud auth login
登录gcloud CLI
,或者使用了Cloud Shell
,这会使您自动登录gcloud CLI
。您可以运行
gcloud auth list
来检查活跃的账号。将请求正文保存在名为
request.json
的文件中,然后执行以下命令:$cred = gcloud auth print-access-token $headers = @{ "Authorization" = "Bearer $cred" } Invoke-WebRequest ` -Method POST ` -Headers $headers ` -ContentType: "application/json; charset=utf-8" ` -InFile request.json ` -Uri "https://alloydb.googleapis.com/v1beta/projects/PROJECT_ID/locations/REGION/clusters/CLUSTER_ID/instances"|Select-Object -Expand Content
您会收到类似以下内容的 JSON 响应:
响应
如果成功,响应正文将包含一个新创建的
Operation
实例。 - 将实例更新到
READY
状态后,连接到该实例并访问从 Cloud SQL 快照恢复的数据。