Because CockroachDB is designed with high fault tolerance, backups are primarily needed for disaster recovery (i.e., if your cluster loses a majority of its nodes). Isolated issues (such as small-scale node outages) do not require any intervention. However, as an operational best practice, we recommend taking regular backups of your data.

Based on your license type, CockroachDB offers two methods to backup and restore your cluster's data: Enterprise and Core.

Watch the demo

Perform Enterprise backup and restore

If you have an Enterprise license, you can use the BACKUP statement to efficiently back up your cluster's schemas and data to popular cloud services such as AWS S3, Google Cloud Storage, or NFS, and the RESTORE statement to efficiently restore schema and data as necessary.

Tip:

We recommend automating daily backups of your cluster. To automate backups, you must have a client send the BACKUP statement to the cluster. Once the backup is complete, your client will receive a BACKUP response.

Full backups

In most cases, it's recommended to take full nightly backups of your cluster. A cluster backup allows you to do the following:

  • Restore table(s) from the cluster
  • Restore database(s) from the cluster
  • Restore a full cluster

To do a cluster backup, use the BACKUP statement:

copy
icon/buttons/copy
> BACKUP TO '<backup_location>';

If it's ever necessary, you can use the RESTORE statement to restore a table:

copy
icon/buttons/copy
> RESTORE TABLE bank.customers FROM '<backup_location>';

Or to restore a database:

copy
icon/buttons/copy
> RESTORE DATABASE bank FROM '<backup_location>';

Or to restore your full cluster:

copy
icon/buttons/copy
> RESTORE FROM '<backup_location>';
Note:

A full cluster restore can only be run on a target cluster that has never had user-created databases or tables.

Full and incremental backups

If your cluster grows too large for nightly full backups, you can take less frequent full backups (e.g., weekly) with nightly incremental backups. Incremental backups are storage efficient and faster than full backups for larger clusters.

Periodically run the BACKUP command to take a full backup of your cluster:

copy
icon/buttons/copy
> BACKUP TO '<backup_location>';

Then, create nightly incremental backups based off of the full backups you've already created. If you backup to a destination already containing a full backup, an incremental backup will be appended to the full backup in a subdirectory:

copy
icon/buttons/copy
> BACKUP TO '<backup_location>';
Note:

For an example on how to specify the destination of an incremental backup, see Backup and Restore - Advanced Options

If it's ever necessary, you can then use the RESTORE command to restore your cluster, database(s), and/or table(s). Restoring from incremental backups requires previous full and incremental backups. To restore from a destination containing the full backup, as well as the automatically appended incremental backups (that are stored as subdirectories, like in the example above):

copy
icon/buttons/copy
> RESTORE FROM '<backup_location>';

Examples

Automated full and incremental backups

You can automate your backups using scripts and your preferred method of automation, such as cron jobs.

For your reference, we have created this sample backup script that you can customize to automate your backups.

In the sample script, configure the day of the week for which you want to create full backups. Running the script daily will create a full backup on the configured day, and on other days, it'll create incremental backups. The script tracks the recently created backups in a separate file titled backup.txt and uses this file as a base for the subsequent incremental backups.

  1. Download the sample backup script:

    copy
    icon/buttons/copy
    $ wget -qO- https://raw.githubusercontent.com/cockroachdb/docs/master/_includes/v20.1/prod-deployment/backup.sh
    

    Alternatively, you can create the file yourself and copy the script into it:

    copy
    icon/buttons/copy
    #!/bin/bash
    
    set -euo pipefail
    
    # This script creates full backups when run on the configured
    # day of the week and incremental backups when run on other days, and tracks
    # recently created backups in a file to pass as the base for incremental backups.
    
    full_day="<day_of_the_week>"                      # Must match (including case) the output of `LC_ALL=C date +%A`.
    what=""                                           # Leave empty fo cluster backup, or add "DATABASE database_name" to backup a database.
    base="<storage_URL>/backups"                      # The URL where you want to store the backup.
    extra="<storage_parameters>"                      # Any additional parameters that need to be appended to the BACKUP URI (e.g., AWS key params).
    recent=recent_backups.txt                         # File in which recent backups are tracked.
    backup_parameters=<additional backup parameters>  # e.g., "WITH revision_history"
    
    # Customize the `cockroach sql` command with `--host`, `--certs-dir` or `--insecure`, and additional flags as needed to connect to the SQL client.
    runsql() { cockroach sql --insecure -e "$1"; }
    
    destination="${base}/$(date +"%Y%m%d-%H%M")${extra}"
    
    prev=
    while read -r line; do
        [[ "$prev" ]] && prev+=", "
        prev+="'$line'"
    done < "$recent"
    
    if [[ "$(LC_ALL=C date +%A)" = "$full_day" || ! "$prev" ]]; then
        runsql "BACKUP $what TO '$destination' AS OF SYSTEM TIME '-1m' $backup_parameters"
        echo "$destination" > "$recent"
    else
        destination="${base}/$(date +"%Y%m%d-%H%M")-inc${extra}"
        runsql "BACKUP $what TO '$destination' AS OF SYSTEM TIME '-1m' INCREMENTAL FROM $prev $backup_parameters"
        echo "$destination" >> "$recent"
    fi
    
    echo "backed up to ${destination}"
    
  2. In the sample backup script, customize the values for the following variables:

    Variable Description
    full_day The day of the week on which you want to take a full backup.
    what Leave empty for a cluster backup. Otherwise, add DATABASE <db_name> to back up a database (i.e., create backups of all tables and views in the database).
    base The URL where you want to store the backup.

    URL format: [scheme]://[host]/[path]

    For information about the components of the URL, see Backup File URLs.
    extra The parameters required for the storage.

    Parameters format: ?[parameters]

    For information about the storage parameters, see Backup File URLs.
    backup_parameters Additional backup parameters you might want to specify.

    Also customize the cockroach sql command with --host, --certs-dir or --insecure, and additional flags as required.

  3. Change the file permissions to make the script executable:

    copy
    icon/buttons/copy
    $ chmod +x backup.sh
    
  4. Run the backup script:

    copy
    icon/buttons/copy
    $ ./backup.sh
    
Note:

If you miss an incremental backup, delete the recent_backups.txt file and run the script. It'll take a full backup for that day and incremental backups for subsequent days.

Advanced examples

For examples of advanced BACKUP and RESTORE use cases, see Back up and Restore Data - Advanced Options. Advanced examples include:

Perform Core backup and restore

If you do not have an Enterprise license, you can perform a core backup. Run the cockroach dump command to dump all the tables in the database to a new file (e.g., backup.sql):

copy
icon/buttons/copy
$ cockroach dump <database_name> <flags> > backup.sql

To restore a database from a core backup, use the IMPORT PGDUMP statement:

copy
icon/buttons/copy
$ cockroach sql --execute="IMPORT PGDUMP 's3://your-external-storage/backup.sql?AWS_ACCESS_KEY_ID=[placeholder]&AWS_SECRET_ACCESS_KEY=[placeholder]'" \
 <flags>

You can also use the cockroach sql command to execute the CREATE TABLE and INSERT statements in the backup file:

copy
icon/buttons/copy
$ cockroach sql --database=[database name] < backup.sql
Tip:

If you created a backup from another database and want to import it into CockroachDB, see the Migration Overview.

See also



Yes No