This page offers information about how you can create backups of Spanner databases.
For more information about backups, see Backups overview. You can back up databases using the following ways:
- Using the Google Cloud console.
- Using the Google Cloud CLI.
- Using the client libraries.
- Using the REST or RPC APIs.
Before you begin
-
To get the permissions that you need to create backups, ask your administrator to grant you the following IAM roles on the instance:
-
Create, view, update, and delete backups:
Cloud Spanner Backup Admin (
roles/spanner.backupAdmin
) -
Create and view backups:
Cloud Spanner Backup Writer (
roles/spanner.backupWriter
)
-
Create, view, update, and delete backups:
Cloud Spanner Backup Admin (
The gcloud CLI examples on this page make the following assumptions:
- You have already set up gcloud CLI for use with Spanner. If you are new to using gcloud CLI with Spanner, see Create and query a database using the gcloud CLI.
You have configured gcloud CLI with your project. For example:
gcloud config set core/project PROJECT_ID
Create a backup
When you create a backup, Spanner creates a backup resource and a long-running backup operation to track the progress of the backup. The newly created backup resides in the same instance, region, and project as its source database.
You must specify the following information when creating a backup:
- A source database ID.
- A name for the backup resource.
- An expiration date (up to 1 year from backup creation time).
Optionally, you can specify a
versionTime
for the source database,
which lets you back up your database to an earlier point in time.
The versionTime
field is typically used either to synchronize the backups of
multiple databases or to recover data using point-in-time recovery (PITR).
If versionTime
is not specified, then it is set to the createTime
of the backup.
You can also create backups on a specified frequency by using a backup schedule. For more information, see Create and manage backup schedules.
Console
In the Google Cloud console, go to the Spanner Instances page.
Click the instance that contains the database that you want to back up.
Click the database.
In the navigation pane, click Backup/Restore.
In the Backups tab, click Create backup.
Fill out the form, and click Create.
To check the progress of a backup operation, see Check the operation progress.
gcloud
Before using any of the command data below, make the following replacements:
- INSTANCE_ID: the Spanner instance ID.
- DATABASE_ID: the Spanner database ID.
- BACKUP_NAME: the Spanner backup name.
-
RETENTION_PERIOD: the retention period of
the backup created. For example, if you want the retention duration to be one
day, you can use
86400s
. -
ENCRYPTION_TYPE:
the encryption type of backup created.
Valid values are
USE_CONFIG_DEFAULT_OR_BACKUP_ENCRYPTION
,GOOGLE_DEFAULT_ENCRYPTION
, orCUSTOMER_MANAGED_ENCRYPTION
. If you useCUSTOMER_MANAGED_ENCRYPTION
, you must specify akmsKeyName
.
Execute the following command:
Linux, macOS, or Cloud Shell
gcloud spanner backups create BACKUP_NAME\ --instance=INSTANCE_ID\ --database=DATABASE_ID\ --retention-period=RETENTION_PERIOD\ --encryption-type=ENCRYPTION_TYPE\ --async
Windows (PowerShell)
gcloud spanner backups create BACKUP_NAME` --instance=INSTANCE_ID` --database=DATABASE_ID` --retention-period=RETENTION_PERIOD` --encryption-type=ENCRYPTION_TYPE` --async
Windows (cmd.exe)
gcloud spanner backups create BACKUP_NAME^ --instance=INSTANCE_ID^ --database=DATABASE_ID^ --retention-period=RETENTION_PERIOD^ --encryption-type=ENCRYPTION_TYPE^ --async
You should receive a response similar to the following:
Create request issued for: BACKUP_NAME Check operation [projects/PROJECT_ID/instances/INSTANCE_ID/backups/BACKUP_NAME/operations/_auto_op_234567] for status.
Here are some usage notes:
- To set the backup's expiration date, specify either the
--retention-period
or--expiration-date
flag. For information on date syntax, seegcloud topic datetimes
. - The name of the backup must be unique in the instance.
- The command returns immediately because of the
--async
flag. Without the flag, the command will wait for the backup operation to complete. - Specify the
--help
flag to get help for any command.
To check the progress of a backup operation, see Check the operation progress.
Client libraries
The following code sample creates a backup at a specific version_time
and then waits for it to complete. Once complete, it verifies that the backup is
ready and retrieves some information about it, such as its name, size, and
create time.
C++
C#
Go
Java
Node.js
PHP
Python
Ruby
What's next
To learn more about backups, see Backups overview.
To manage backups, see Manage backups.