> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Take a backup or restore a backup using commands

> Page describing how to take a backup or restore a backup with your own bucket using commands

You can utilize `BACKUP` and `RESTORE` commands to export backups to their storage buckets,
in addition to backing up or restoring [via user interface](/docs/products/cloud/guides/backups/bring-your-own-backup/backup-restore-from-ui).
Commands for all three CSPs are given in this guide.

<h2 id="requirements">
  Requirements
</h2>

You will need the following details to export/restore backups to your own CSP storage bucket:

<Tabs>
  <Tab title="AWS">
    1. AWS S3 endpoint, in the format: `s3://<bucket_name>.s3.amazonaws.com/<optional_directory>`
       For example: `s3://testchbackups.s3.amazonaws.com/`
       Where:
       * `testchbackups` is the name of the S3 bucket to export backups to.
       * `backups` is an optional subdirectory.
    2. AWS access key and secret. AWS role based authentication is also supported and can be used in place of AWS access key and secret as described in the section above.

    <br />
  </Tab>

  <Tab title="GCP">
    1. GCS endpoint, in the format: `https://storage.googleapis.com/<bucket_name>/`
    2. Access HMAC key and HMAC secret.

    <br />
  </Tab>

  <Tab title="Azure">
    1. Azure storage connection string.
    2. Azure container name in the storage account.
    3. Azure Blob within the container.

    <br />
  </Tab>
</Tabs>

<h2 id="backup_restore_db">
  Backup / Restore specific DB
</h2>

Here we show the backup and restore of a *single* database.
See the [backup command summary](/docs/concepts/features/backup-restore/overview#command-summary) for full backup and restore commands.

<h3 id="aws-s3-bucket">
  AWS S3
</h3>

<Tabs>
  <Tab title="BACKUP">
    ```sql theme={null}
    BACKUP DATABASE test_backups 
    TO S3(
      'https://testchbackups.s3.amazonaws.com/<uuid>',
      '<key id>',
      '<key secret>'
    )
    ```

    Where `uuid` is a unique identifier, used to differentiate a set of backups.

    <Note>
      You will need to use a different uuid for each new backup in this subdirectory, otherwise you will get a `BACKUP_ALREADY_EXISTS` error.
      For example, if you're taking daily backups, you will need to use a new uuid each day.
    </Note>
  </Tab>

  <Tab title="RESTORE">
    ```sql theme={null}
    RESTORE DATABASE test_backups
    FROM S3(
      'https://testchbackups.s3.amazonaws.com/<uuid>',
      '<key id>',
      '<key secret>'
    )
    ```
  </Tab>
</Tabs>

<h3 id="google-cloud-storage">
  Google Cloud Storage (GCS)
</h3>

<Tabs>
  <Tab title="BACKUP">
    ```sql theme={null}
    BACKUP DATABASE test_backups 
    TO S3(
      'https://storage.googleapis.com/<bucket>/<uuid>',
      '<hmac-key>',
      '<hmac-secret>'
    )
    ```

    Where `uuid` is a unique identifier, used to identify the backup.

    <Note>
      You will need to use a different uuid for each new backup in this subdirectory, otherwise you will get a `BACKUP_ALREADY_EXISTS` error.
      For example, if you're taking daily backups, you will need to use a new uuid each day.
    </Note>
  </Tab>

  <Tab title="RESTORE">
    ```sql theme={null}
    RESTORE DATABASE test_backups
    FROM S3(
      'https://storage.googleapis.com/<bucket>/<uuid>',
      '<hmac-key>',
      '<hmac-secret>'
    )
    ```
  </Tab>
</Tabs>

<h3 id="azure-blob-storage">
  Azure Blob Storage
</h3>

<Tabs>
  <Tab title="BACKUP">
    ```sql theme={null}
    BACKUP DATABASE test_backups 
    TO AzureBlobStorage(
      '<AzureBlobStorage endpoint connection string>',
      '<container>',
      '<blob>/<>'
    )
    ```

    Where `uuid` is a unique identifier, used to identify the backup.

    <Note>
      You will need to use a different uuid for each new backup in this subdirectory, otherwise you will get a `BACKUP_ALREADY_EXISTS` error.
      For example, if you're taking daily backups, you will need to use a new uuid each day.
    </Note>
  </Tab>

  <Tab title="RESTORE">
    ```sql theme={null}
    RESTORE DATABASE test_backups
    FROM AzureBlobStorage(
      '<AzureBlobStorage endpoint connection string>',
      '<container>',
      '<blob>/<uuid>'
    )
    ```
  </Tab>
</Tabs>

<h2 id="backup_restore_entire_service">
  Backup / Restore entire service
</h2>

For backing up the entire service, use the commands below.
This backup will contain all user data and system data for created entities, settings profiles, role policies, quotas, and functions.
We list these here for AWS S3.
You can utilize these commands with the syntax described above to take backups for GCS and Azure Blob storage.

<Tabs>
  <Tab title="BACKUP">
    ```sql theme={null}
    BACKUP 
        TABLE system.users,
        TABLE system.roles,
        TABLE system.settings_profiles,
        TABLE system.row_policies,
        TABLE system.quotas,
        TABLE system.functions,
        ALL EXCEPT DATABASES INFORMATION_SCHEMA, information_schema, system
    TO S3(
        'https://testchbackups.s3.amazonaws.com/<uuid>',
        '<key id>',
        '<key secret>'
    )
    ```

    where `uuid` is a unique identifier, used to identify the backup.
  </Tab>

  <Tab title="RESTORE">
    ```sql theme={null}
    RESTORE ALL EXCEPT TABLES system.users, system.roles
    FROM S3(
        'https://testchbackups.s3.amazonaws.com/<uuid>',
        '<key id>',
        '<key secret>'
    )
    ```
  </Tab>
</Tabs>

<h2 id="backups-faq">
  FAQ
</h2>

<Accordion title="What happens to the backups in my cloud object storage? Are they cleaned up by ClickHouse at some point?">
  We provide you the ability to export backups to your bucket, however, we don't clean up or delete any of the backups once written. You're responsible for managing the lifecycle of the backups in your bucket, including deleting, or archiving as needed, or moving to cheaper storage to optimize overall cost.
</Accordion>

<Accordion title="What happens to the restore process if I move some of the existing backups to another location?">
  If any backups are moved to another location, the restore command will need to be updated to reference the new location where the backups are stored.
</Accordion>

<Accordion title="What if I change my credentials required to access the object storage?">
  You will need to update the changed credentials in the UI, for backups to start happening successfully again.
</Accordion>

<Accordion title="What if I change the location to export my external backups to?">
  You will need to update the new location in the UI, and backups will start happening to the new location. The old backups will stay in the original location.
</Accordion>

<Accordion title="How can I disable external backups on a service that I enabled them for?">
  To disable external backups for a service, go to the service setting screen, and click on Change external backup. In the subsequent screen, click on Remove setup to disable external backups for the service.
</Accordion>
