Back Up Your Firestore Daily Using A Cloud Function
Problem
Backing up your database is an extremely critical feature of making sure you can restore your data if it ever gets corrupted or lost. It would seem natural for Firebase to expose a feature to automatically back up your Firestore data
The only way to backup is to use the Import/Export feature in the Google Cloud Console, but this is all manual. After a few days of doing manual Firestore backups, you are going to start to forget to make backups, and bad things always go wrong when you least expect them.
If you are looking for an automated approach to making Firestore backups daily, I got you covered. I will show you how to use Google Cloud Functions to automatically back up your Firestore database every day.
How to Automate Firestore Backups
You can use a scheduled Google Cloud Function to make your Firestore backups.
Firebase exposes a Firestore.v1.FirestoreAdminClient class with an exportDocuments method that you can use to export your Firestore documents into a Cloud Storage bucket.
This guide assumes that you have already set up your Google Cloud Functions repo.
There are two steps you need to follow to get this working.
- Add your service account key to your Google Cloud repo
- Add a new Cloud Function backup function that runs every 24 hours
Add your Service Account key
You'll need to add a new Service Account Key (or use an existing key) from the Google Cloud console that will allow your Google Cloud Function to make backups on your behalf.
Go to the Service Accounts section of the Google Cloud Console.
Go through the process of adding a new key, making sure to select JSON as the Key type.
Download the Service Account Key as a .json file, which will look like this (but filled out with your actual data, of course):
{
"type": "service_account",
"project_id": "PROJECT_ID",
"private_key_id": "KEY_ID",
"private_key": "-----BEGIN PRIVATE KEY-----\nPRIVATE_KEY\n-----END PRIVATE KEY-----\n",
"client_email": "SERVICE_ACCOUNT_EMAIL",
"client_id": "CLIENT_ID",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/SERVICE_ACCOUNT_EMAIL"
}
Add this file into your Google Cloud repo.
Add Your Google Cloud Backup Function
Next, you will need to create a new scheduled function within your Google Cloud repo that will run every 24 hours to export all of your Firestore documents and save the snapshot into a Cloud Storage bucket.
Here is the code:
import * as functions from 'firebase-functions';
const { Firestore } = require('@google-cloud/firestore');
// This is the relative path to the Service Account Key that you downloaded in the first step
const serviceAccountKey = require('../serviceAccountKey.json');
const client = new Firestore.v1.FirestoreAdminClient({
credentials: serviceAccountKey,
});
// Replace this with your real Cloud Storage bucket path
const bucket = 'gs://${YOUR_CLOUD_STORAGE_BUCKET}/backups/';
export const scheduledBackups = functions.pubsub
.schedule('every 24 hours')
.onRun(() => {
const projectId =
process.env.GCP_PROJECT || process.env.GCLOUD_PROJECT || '';
const databaseName = client.databasePath(projectId, '(default)');
const date = new Date().toISOString().split('T')[0];
const outputUriPrefix = bucket + 'backup_' + date;
return client
.exportDocuments({
name: databaseName,
outputUriPrefix: outputUriPrefix,
collectionIds: [],
})
.then((responses: any) => {
const response = responses[0];
console.log(`Operation Name: ${response['name']}`);
})
.catch((err: any) => {
console.error(err);
throw new Error('Export operation failed');
});
});
You'll need to make sure you make the following changes in the above code:
- Make sure the relative path to the service account key is correct
- Make sure to replace ${YOUR_CLOUD_STORAGE_BUCKET} with your actual Cloud Storage bucket
Conclusion
Once you've added the above code, deploy your new scheduled Cloud Function, and you should start generating new Firestore backups every 24 hours.
Did this guide work for you? Feel free to reach out to me with feedback about this process.