[Blaze](https://firebase.google.com/pricing.md.txt) plan users can set up their Firebase Realtime Database for
automatic backups, a self-service feature that enables daily backups of your
Database application data and [Security Rules](https://firebase.google.com/database/security) in
JSON format to a [Cloud Storage](https://cloud.google.com/storage/docs/)
bucket.

## Setup

1. In the Firebase console, navigate to **Databases \& Storage** \>
   [**Realtime Database**](https://console.firebase.google.com/project/_/database/).

2. In the **Backups** tab, use the in-console workflow to set up your
   automated backups.

To save on storage costs, we enable [Gzip](https://firebase.google.com/docs/database/backups.md.txt#gzip_compression)
compression by default, and you can choose to enable a
[30-day lifecycle policy](https://firebase.google.com/docs/database/backups.md.txt#storage_lifecycle)
on your bucket to have backups older than 30 days automatically deleted.

You can view the status and backup activity directly in the Firebase console
where you can also start a manual backup. This can be useful for taking specific
timed snapshots or as a safety action before you perform any code changes.

Once set up, a new Cloud Storage bucket will be created for you with the
[WRITER permission](https://cloud.google.com/storage/docs/access-control/lists#permissions)
for Firebase. You should not store data in this bucket you are not comfortable
with Firebase having access to. Firebase will have no additional access to your
other Cloud Storage buckets or any other areas of Google Cloud.

## Restoring from backups

To restore your Firebase from a backup, first download the file from
Cloud Storage to your local disk. This can be done by clicking the filename
within the backup activity section or from the Cloud Storage bucket
interface. If the file is Gzip compressed, first
[decompress](https://firebase.google.com/docs/database/backups.md.txt#gzip_compression) the file.

You have two options for how to import your data:

- **Option 1** : Use the Firebase console.

  1. Navigate to the **Databases \& Storage** \>
     **Realtime Database** \> [**Data** tab](https://console.firebase.google.com/project/_/database/data).

  2. Click **Import JSON**, and select your application data JSON file.

- **Option 2**: Issue a CURL request from your command line.

  1. Retrieve a secret from Firebase, which you can get from the
     **Databases \& Storage** \> **Realtime Database** \>
     [**Settings** tab](https://console.firebase.google.com/project/_/database/settings).

  2. Enter the following into your terminal, replacing the `DATABASE_NAME`
     and `SECRET` fields with your own values:

     ```bash
     curl 'https://<DATABASE_NAME>.firebaseio.com/.json?auth=<SECRET>&print=silent' -X PUT -d @<DATABASE_NAME>.json
     ```

If you are having trouble restoring a backup from a very large database,
reach out to [Firebase Support](https://firebase.google.com/support/contact/troubleshooting).

## Scheduling

Your Database backup is assigned to a specific hour each day that ensures even
load and highest availability for all backup customers. This scheduled backup
will occur regardless of if you do any manual backups throughout the day.

## File naming

Files transferred to your Cloud Storage bucket will be timestamped
(ISO 8601 standard) and use the following naming conventions:

- Database data: `YYYY-MM-DDTHH:MM:SSZ_<DATABASE_NAME>_data.json`
- Database rules: `YYYY-MM-DDTHH:MM:SSZ_<DATABASE_NAME>_rules.json`

If [Gzip](https://firebase.google.com/docs/database/backups.md.txt#gzip_compression)
is enabled, a `.gz` suffix will be appended to the filenames. You can
easily find the backups from a specific date or time using Cloud Storage
prefix searching.

## Gzip compression

By default, we compress your backup files using Gzip compression to save on
storage costs and decrease transfer times. The compressed filesize varies
depending on the data characteristics of your Database, but typical Databases
may shrink to ⅓ their original size, saving you on storage costs and decreasing
the upload time for your backups.

To decompress your Gzipped JSON files, issue a command line command using the
`gunzip` binary which is shipped by default for OS-X and most Linux
distributions.

    gunzip <DATABASE_NAME>.json.gz  # Will unzip to <DATABASE_NAME>.json

## Storage 30 day lifecycle

We offer an easy to use configuration switch that enables a default 30 day
object lifecycle policy for your Cloud Storage bucket. When enabled, files in
your bucket will be automatically deleted after 30 days. This helps to reduce
unwanted old backups, saving you on storage costs, and keeping your bucket
directory clean. If you place other files into your Automated Backups bucket,
they will also be deleted with the same policy.

## Costs

The backups feature can be enabled for projects on the [Blaze](https://firebase.google.com/pricing.md.txt) plan
for no additional cost. However, you will be charged at the [standard
rates](https://cloud.google.com/storage/pricing) for the backup files
placed in your Cloud Storage bucket. You can enable [Gzip
Compression](https://firebase.google.com/docs/database/backups#gzip_compression) and [Storage 30 day
Lifecycle](https://firebase.google.com/docs/database/backups#storage_lifecycle) to reduce your storage
costs.