How To Move Spanner Backups From Production to Development
Move Google Cloud Spanner backups between projects for seamless synchronization. Secure your data effortlessly!
Spanner is Google’s powerful cloud database solution. It is highly scalable and globally distributed as a NoSQL database, yet provides relational semantics like your well-known SQL database. Spanner even provides a PostgreSQL-compatible interface.
In this article, we will create a backup and move it to another gcloud project. This comes in handy if you want to synchronize your development database with your live database, for example, to reproduce a bug which occurs in production.
How to create Spanner Backups
There are several ways you can use to create backups for your Spanner database.
These include the web interface, gcloud CLI, several client libraries, as well as a REST API. We will take a look at the first two options.
The Google Cloud console is a straightforward way to create backups. I often use it to create a backup right before a bigger change. However, for your daily backups, I recommend using another, automatic way.
To create a backup in the console, navigate to your Spanner instanceand open the one you want to back up. In the menu, select Backup/Restore, and click on Create Backup.
As you can see, Spanner backups will expire. Select a suitable time period for your use case. If you are just creating a backup to transfer it to another project, choose a short time frame. You are billed for the disk space a backup uses.
You can see the progress of the backup creation in the Activity tab:
Keep the backup name in mind, as we will need it later to transfer the backup to another project.
You can use the gcloud CLI to create a backup as well:
gcloud spanner backups create <backup name> --instance=<Spanner instance name> \
--database=<source database name> --retention-period=1d
Copy the backup to another project
Moving a backup to another project cannot be done in the web interface. You must use gcloud CLI.
First, activate the project with the backup you want to move:
gcloud config set project <your project name>
If you do not know the full project name, use the following to list all your projects:
gcloud projects list
To copy your backup, run:
gcloud spanner backups copy \
--source-backup=projects/<source_project>/instances/<source_instance>/backups/<backup name from above> \
--destination-backup=projects/<target project>/instances/<target instance>/backups/copied-backup --expiration-date=2023-10-30T10:49:41Z
Restore from a backup
Once the copy process is completed, you can create a new Spanner database from the project. The “new” is important — you cannot use an existing database for this. If you need to use the name of an existing database, you have to delete the database before restoring the backup. So please pay extra attention if you do this in any production environment!
To start, open the target project and navigate to your Spanner instances which have the backup.
Click on Backup/Restore. The copied backup should appear in the list of backups. Open the menu by clicking on the three dots on the right and select Restore:
Then select the target instance and the new database name. Restoring the backup takes some time, and you can follow the progress again in the Activity tab.
You can use the gcloud CLI as well to restore a backup:
gcloud spanner databases restore \
--destination-instance=your-instance --destination-database=new-db-name \
--source-instance=your-instance --source-backup=<backup name from above>
Conclusion
Moving a backup of your Google Cloud Spanner database to another project can be a game-changer, allowing you to synchronize your development and production environments effortlessly.
I hope you found this article helpful. If you have any questions or suggestions, please share them in the comments below. Happy coding and data managing!
Links and resources:
gcloud CLI for Spanner Backup
Client Libraries for Spanner Backup
REST API for Spanner Backup
Google Cloud Spanner Documentation