Examples
Before you begin, connect to your cluster:- .
- .
- .
Full backup
To take a of a cluster:Backup subdirectories
BACKUP... INTO adds a backup to a location. To view the backup paths in a given collection location (your storage bucket), use :
/2023/12/21-142943.73) to the RESTORE statement.
Restore
To restore from the most recent backup () in the collection’s location, use theLATEST syntax:
You cannot restore a backup of a multi-region database into a single-region database.
RESTORE statement. To view the available subdirectories, use . If you are restoring an incremental backup, the URI must point to the storage location that contains the full backup:
Incremental backup
When aBACKUP statement specifies an existing subdirectory in the collection, explicitly or via the LATEST keyword, an incremental backup will be added to the default /incrementals directory at the root of the storage location.
To take an incremental backup using the LATEST keyword:
If you intend to take a full backup, we recommend running
BACKUP INTO {collectionURI} without specifying a subdirectory.Scheduled backup
This example for a cluster backup with revision history that is taken every day at midnight:FULL BACKUP clause is not included, CockroachDB also scheduled a full backup to run @weekly. This is the default cadence for incremental backups RECURRING > 1 hour but <= 1 day.
Encrypted backup
You can take and restore encrypted backups in the following ways:BACKUP statement’s kms option:
Locality-aware backup
CockroachDB Basic and Standard clusters operate with a different architecture compared to CockroachDB self-hosted and CockroachDB Advanced clusters. These architectural differences have implications for how locality-aware backups can run. Basic and Standard clusters will scale resources depending on whether they are actively in use, which means that it is less likely to have a SQL pod available in every locality. As a result, your cluster may not have a SQL pod in the locality where the data resides, which can lead to the cluster uploading that data to a storage bucket in a locality where you do have active SQL pods. You should consider this as you plan a backup strategy that must comply with requirements. For example, to create a where nodes with the localityregion=us-west write backup files to s3://us-west-bucket, and all other nodes write to s3://us-east-bucket by default, run:
BACKUP statement for a locality-aware backup, check the following:
- The locality query string parameters must be URL-encoded.
- If you are creating an with , you must pass them in uppercase otherwise you will receive an
unknown query parameterserror. - A successful locality-aware backup job requires that each node in the cluster has access to each storage location. This is because any node in the cluster can claim the job and become the node.
default URI when the backup was created. If you have moved your backups to a different location since the backup was originally taken, the first URI must be the new location of the files originally written to the default location.
To restore from a specific backup, use
Backup and restore data from userfile storage
To put files on your CockroachDB cluster without external servers, use , a per-user file storage.
For information on userfile commands, visit the following pages:
Only database and table-level backups are possible when using
userfile as storage. Restoring cluster-level backups will not work because userfile data is stored in the defaultdb database, and you cannot restore a cluster with existing table data.userfile storage allows for database and table-level backups.
First, run the following statement to backup a database to a directory in the default userfile space:
When backing up from a cluster and restoring a database or table that is stored in your
userfile space to a different cluster, you can run to download the backup files to a local machine and to upload to the userfile of the alternate cluster.BACKUP... INTO adds a backup to a collection within the backup destination. The path to the backup is created using a date-based naming scheme by default, unless an is passed with the BACKUP statement. To view the backup paths in a given destination, use :
userfile as storage. Restoring cluster-level backups will not work because userfile data is stored in the defaultdb database, and you cannot restore a cluster with existing table data.
In cases when you need to restore a specific backup, add the backup subdirectory to the RESTORE statement:
userfile:///bank-backup as userfile:/// refers to the default path userfile://defaultdb.public.userfiles_$user/.
To restore from the most recent backup, use :
userfile storage:
cockroach userfile delete {file}, it will take as long as the to be removed from disk.
Back up a CockroachDB Cloud cluster and restore into a new cluster
You can move an existing CockroachDB Cloud cluster’s contents to a new cluster by creating a self-managed backup and restoring the backup in the new cluster. This method of migration requires a period of downtime during which application traffic to the cluster must be stopped. If minimal downtime on the cluster is a requirement, contact the . To move to a new cluster, follow these steps:-
Create a of the source cluster if a self-managed backup of the cluster does not already exist. If the backup storage is in AWS S3 or Google Cloud GCS, append
?AUTH=specifiedto the storage URL. - Provision the .
- Stop application traffic to the source cluster.
- Perform a to include any last-minute changes to data on the cluster.
-
:
MacLinuxWhere:Windows
-
<useris the SQL user. By default, this is your CockroachDB Cloud account username. -
<cluster-name-<short-idis the short name of your cluster plus the short ID. For example,funny-skunk-3ab. -
<cluster-idis a unique string used to identify your cluster when downloading the CA certificate. For example,12a3bcde-4fa5-6789-1234-56bc7890d123. -
<regionis the region in which your cluster is running. If you have a multi-region cluster, you can choose any of the regions in which your cluster is running. For example,aws-us-east-1. -
<databaseis the name for your database. For example,defaultdb. You can find these settings in the Connection parameters tab of the Connection info dialog.
-
-
the latest backup to the new cluster:
a. Use
SHOW BACKUPSwith your external location to find the backup’s subdirectory:b. Use the subdirectory to specify the backup to restore: - Update application connection strings to use the new cluster.
- Resume application traffic and monitor performance on the new cluster.
- .
Back up a self-hosted CockroachDB cluster and restore into a CockroachDB Cloud cluster
To back up a self-hosted CockroachDB cluster and restore into a CockroachDB Cloud cluster:-
While , your databases and/or tables to an :
If you are backing up the data to AWS or GCP, use the
specified option for the AUTH parameter, as CockroachDB Cloud will need the specified credentials upon . For more information on authentication parameters to cloud storage providers, see .-
:
MacLinuxWhere:Windows
-
<useris the SQL user. By default, this is your CockroachDB Cloud account username. -
<cluster-name-<short-idis the short name of your cluster plus the short ID. For example,funny-skunk-3ab. -
<cluster-idis a unique string used to identify your cluster when downloading the CA certificate. For example,12a3bcde-4fa5-6789-1234-56bc7890d123. -
<regionis the region in which your cluster is running. If you have a multi-region cluster, you can choose any of the regions in which your cluster is running. For example,aws-us-east-1. -
<databaseis the name for your database. For example,defaultdb. You can find these settings in the Connection parameters tab of the Connection info dialog.
-
-
to your CockroachDB Cloud cluster.
a. Use
SHOW BACKUPSwith your external location to find the backup’s subdirectory:b. Use the subdirectory to specify the backup to restore:

