Create Automatic Firestore Backups with GitHub Actions
Tags: DevOps, Google Cloud Platform, Firestore, Github, Firebase

DevOps

Create Automatic Firestore Backups with GitHub Actions

Backup your customers’ data automatically with GitHub Actions and Google Cloud Platform.

Have ever experienced one of the following situations? You just released your app, but somehow your users’ data started to disappear. Or, you deployed your app without writing any Firestore rules, and some malicious users got access to it and deleted every document.

Cloud Firestore by itself does not create any backups of your data for you to ask them to roll back the database. To prevent this from happening, first and foremost, you need to write strong security rules, making sure to only allow access to the data that users need. And second, you should create backups of your Cloud Firestore data.

In case you encounter any problems, there is a troubleshooting section at the end of this article.

Firestore is part of Google Cloud Platform (GCP for short). We will use GCP to make the backups (I will also teach you how to roll back the data), and GitHub actions to trigger the backup (in our case I want it to happen every two days).

First, you need a GitHub repo with your project — in case you are already using it, great! Otherwise, consider switching to GitHub, it has a ton of free features, but you can achieve this with other services using their pipelines, or tools like Jenkins. However, in this post, we will focus on the GitHub way.

You might have seen a .github folder in some projects, this is where our actions will live. Inside another folder called workflows , we will create a firestore.yml for our backups config. Create this structure in the root of your project: .github/workflows/firestore.yml

This is what the fields mean:

name : The name of your GitHub action

: The name of your GitHub action on : This is what triggers GitHub to do the backup

: This is what triggers GitHub to do the backup workflow_dispatch : This is to create a button on the GitHub actions which will allow us to run the backup by clicking. In case you’re about to do something important and don’t want the data to be lost, note that you can altogether remove this block if you wish

: This is to create a button on the GitHub actions which will allow us to run the backup by clicking. In case you’re about to do something important and don’t want the data to be lost, note that you can altogether remove this block if you wish schedule : This is where we provide GitHub with a cronjob of how often theAction should trigger. In my case, every two days. Here is a tool if you want to set yours, replace the string in the cron value.

: This is where we provide GitHub with a cronjob of how often theAction should trigger. In my case, every two days. Here is a tool if you want to set yours, replace the string in the cron value. jobs: This is what GitHub triggers when any on block meets the condition, there are two things you should change inside this, replace yourappid and yourbucket . You can use the Firebase Storage bucket, or create a new one on GCP.

If you read the YAML file, there is a part where it says secrets.GCP_SA_KEY

This is the GCP ServiceAccount Key for your project. To get this key, go to this link.