Backups are underrated … until you really need them. So here is how to backup your Cloud Firestore database. Forget about external tools or self-written Cloud Functions and use the provided tools instead!
I store quite a lot of stuff in Firestore. It’s mostly for personal use but my GDash – Advanced Gumroad Dashboard also caches a lot of Gumroad data with it. In the last months, I often thought about backups. But I have to admit … I am lazy when it comes to boring stuff. However, some of the data is really valuable and will be lost in case of a disaster. So I finally stopped doing cooler things and investigate backup solutions. Here is my approach on how to backup your Cloud Firestore database.
💡 Tip
This feature is still in a preview state but I haven’t encountered any issues yet!
Using the Google Cloud tools
While I could write a manual exporter/importer and run it as a scheduled Cloud Function, I prefer existing official solutions because I might miss things that Google hopefully doesn’t.
So I went with the official Firestore backup guide. Here is a summary of what you need to do:
- Install Google Cloud SDK or Firebase CLI.
I will give you examples for both the Google Cloud CLI (that comes with the Google Cloud SDK) and the Firebase CLI. - Log in.
Executegcloud auth login
orfirebase login
. This grants access to your Firebase project. - Choose your project.
Run the commandgclound config set project PROJECT-ID
orfirebase use PROJECT-ID
. If you don’t know your project id, run the commandgcloud projects list
orfirebase projects:list
to get an overview. - Acquire needed roles
If you are an administrator, there is nothing to do here. But if not, you might require additional roles to be able to work with backups. Click HERE for a description of all required roles. And read THIS to assign roles with the Google Cloud CLI. - Create a scheduled backup
Run the commandgcloud alpha firestore backups schedules create
orfirebase firestore:backups:schedules:create
You might need to install the alpha components for the Google Cloud CLI (just follow the instructions) since this feature currently is only in a preview state. You can find your database name in the Firebase Console. Make sure to type the correct name including the brackets for the default database and surround it by quotes.
💡 Tip
You can create up to one daily backup and one weekly backup per database!
Here are some important parameters to configure your backup properly:
recurrence
– you can choose betweendaily
andweekly
retention-period
– you can choose any time period between1w
and14w
day-of-week
– only available for weekly backups. Choose the day (MON
–SUN
for Google Cloud CLI,MONDAY
–SUNDAY
for Firebase CLI) you want the backup to run.
Here are example commands
gcloud alpha firestore backups schedules create \
--database='(default)' \
--recurrence=weekly \
--retention=7w \
--day-of-week=SUN
firebase firestore:backups:schedules:create \
--database '(default)' \
--recurrence 'WEEKLY' \
--retention 7w \
--day-of-week SUNDAY
💡 Tip
You cannot specify exactly when you want your backup to run. For weekly backups, you can specify the day but you have no control about the time.
How can I list all backup schedules?
To get a list of all backup schedules, use the following command:
gcloud alpha firestore backups schedules list \
--database='(default)'
firebase firestore:backups:schedules:list \
--database '(default)'
How can I delete a backup schedule?
Deleting a backup schedule can be achieved with this command:
gcloud alpha firestore backups schedules delete \
--database='(default)' \
--backup-schedule=BACKUP-SCHEDULE-ID
firebase firestore:backups:schedules:delete BACKUP-SCHEDULE
You can find the BACKUP-SCHEDULE-ID
and the BACKUP-SCHEDULE
by listing all backup schedules (see previous section).
You only delete the scheduler so that no more backups will be created in the future. All existing backups will not be deleted until their retention period expires. Click HERE if you want to delete backups manually.
How can I list all created backups?
To list all backups, use this command:
gcloud alpha firestore backups list
firebase firestore:backups:list
It will show you all backups, when they were created, for what database they are, and when they expire. Here is an example output
database: projects/xxx/databases/(default)
databaseUid: 39d9a097-5662-4a27-b1e2-054c4f9ad486
expireTime: '2024-05-13T22:33:31.540559Z'
name: projects/xxx/locations/eur3/backups/yyy
snapshotTime: '2024-05-06T22:33:31.461258Z'
state: READY
The name
value is especially useful when you want to restore a backup (see next section).
How can I restore my database from a backup?
You cannot fill an existing database from a backup. Instead, you pass a database ID to the restore operation. The database will then be created and filled with the data from the backup. It is up to you how you proceed from that point.
Here is an example command to copy the content of an existing backup into a new database:
gcloud alpha firestore databases restore \
--source-backup=projects/PROJECT-ID/locations/LOCATION-ID/backups/BACKUP-ID \
--destination-database=YOUR-NEW-DATABASE-NAME
firebase firestore:databases:restore \
--backup 'BACKUP' \
--database 'YOUR-NEW-DATABASE-NAME'
You can find PROJECT-ID
, LOCATION-ID
, BACKUP-ID
, and BACKUP
easily by listing all backups (see section above). Name your new database and you are ready to go.
Since this process takes quite a while, you can inspect the progress with the command (this isn’t possible with the FirebaseCLI)
gcloud firestore operations list --database=YOUR-NEW-DATABASE-NAME
The output contains a progress report like this
...
operationState: PROCESSING
progressPercentage:
completedWork: '30'
estimatedWork: '100'
startTime: '2024-05-07T18:12:15.025456Z'
...
Don’t expect this to be fast. It can take up to an hour even for small databases!
Eventually, you will end up with an additional database in your project that contains the backup data.
💡 Tip
Security rules are not part of the backup!
What about pricing? How much does it cost?
The service is not for free. You have to be on the Blaze pricing plan and you are charged for storage space and restore operations. The bigger your database is and the higher your retention rate is, the higher your monthly bill will be.
But if you backup your default database, you still benefit from the free quota while all other databases in your project don’t.
To give you a glimpse of what you can expect:
- The current storage price per GiB in the US multi-region (typically the or one of the most expensive regions) is $0.03.
- The current restore operation cost per GiB in the same region is $0.40.
Click HERE for a price overview and details about Firebase pricing.
Firebase Cloud Firestore
Learn about Firebase Firestore and write mobile apps with the power of a modern and fast NoSQL database.
Conclusion
In this article, I showed you how to backup your Cloud Firestore database. The Firebase backup solution might be perfect in terms of restoring backups. But it provides an easy way to add recurring data copies with little setup effort. A simple safety net that can save you a lot of trouble. It takes you less than 30 minutes to read this article and set up a backup schedule and it’s worth the effort!