This is a simple npm package to backup Firestore databases on a schedule using Google or Firebase Cloud Functions.
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if false;
}
}
}
Add the correct permission to the Cloud Function's service account
Create a new Firebase Cloud Function with below code. See here for scheduling options. Below example runs once a day.
const firestoreBackup = require('simple-firestore-backup')
exports.firestore_backup = functions.runWith({
timeoutSeconds: 540, // Increase timeout to maximum. You can remove this line if your database is not terribly large.
memory: '128MB' // We only do one HTTP request, so we don't need many resources. Let's save money!
}).pubsub.schedule('every 24 hours').onRun(firestoreBackup.createBackupHandler(
'your-project-backups', // Optionally: The Google Cloud Storage Bucket to use (without gs://). Use the name you gave your bucket in step 1 or remove this line if you skipped step 1. Defaults to the default bucket ('your-project-id.appspot.com')
'path/to/backups', // Optionally: the path inside the bucket. Defaults to 'firestore'
'firestore-instance-id' // Optionally: the Firestore instance id to backup. If you did not create a second Firestore instance, you can leave this out. Defaults to '(default)'
))
Let's hope we never need this :) Refer to Firebase's import manual if you need to restore data backed up by this script.
Although I personally use this package in my projects, please note I am not responsible and can't guarantee successful backups and/or restores. Use this at your own risk.