The real-time schema-less database Firestore from Firebase does not come with built-in automated backups. In this blog post, we will guide you through setting up automated Firestore backups inspired by how we do it at Kvalifik.
☝️ We assume that you are an owner of the Firebase project and that you have enabled billing. Read more here.
The Firestore backups are initiated by a cloud function and are stored in a dedicated storage bucket on Google Cloud Platform.
Open Google Cloud Platform and select the Google Cloud project associated with your Firebase project.
Go to IAM & Admin → IAM.
Edit the Member "[project-id][@appspot.gserviceaccount.com]".
Add another role: "Cloud Datastore Import Export Admin".
Click save.
Go to the Storage section in Google Cloud Platform.
Create bucket:
✅ This ensures that backups from the last seven days can not accidentally be deleted.
Click Create.
Feel free to adjust these lifecycle settings to fit your needs.
Go to Lifecycle settings and add the following rules:
We recommend using environment variables to be able to share the same cloud function across multiple Firebase projects. To do this, run the following command on your local machine (with the Firebase CLI installed):
⚠️ If you have several environments, then be sure to switch to the correct project using the command "firebase use [environment-name]".
Add the following Typescript cloud function firestoreBackupDaily. Note that this cloud function is adapted from the Javascript version found here.
Finally, deploy your cloud function using the following command:
Go to the Cloud Scheduler page and click "Run now" in the row corresponding to your newly created cloud function. Shortly after, you should see a Firestore backup within the folder "firestore" in the storage bucket created earlier.
To finish off this setup we recommend setting up monitoring and a plan for disaster recovery. In addition, please note that the solution presented here only performs backups of Firestore. You might also be interested in backing up Firebase Storage and Firebase Auth. The former can be achieved by configuring a daily transfer job on Google Cloud Platform, whereas the latter is more tricky (remember to also store a copy of the password hash parameters!). You can use the listUsers function presented here in a scheduled cloud function similar to the one presented earlier. We might cover some of these topics in a future blog post.