simple-firestore-backup
v1.0.6
Published
This is a simple npm package to backup Firestore databases on a schedule using Google or Firebase Cloud Functions.
Downloads
110
Readme
This is a simple npm package to backup Firestore databases on a schedule using Google or Firebase Cloud Functions.
Schedule backups
- Billing needs to be enabled in order to use Firestore Import/Export. Go to your Firebase console and change your plan to 'Blaze' / 'Pay-As-You-Go'. Please note that the free tier limits are still free.
- It's advised to create a separate Storage Bucket for your backups. If you want to use the default bucket, skip this step.
- In your Firebase console, go to 'Storage' and select 'Add bucket' in the three-dot-menu in the upper right. Name your bucket e.g. 'your-project-backups'.
- Go to the 'Rules' tab, select your new bucket and use this rules to prevent 3rd party access:
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
- Go to the IAM page and make sure the right project is selected in the top left. Look for 'App Engine default service account' in the user list and click the pen icon next to it.
- Add the role 'Datastore Import Export Admin', 'Storage Admin' and 'Editor'.
- Click save and verify that all three roles are listed in the 'Roles' column
- It can take a couple of minutes for the new roles to be applied
- Explanation: Import/Export is currently only available over Rest/RPC. To trigger the export this module needs to authenticate over HTTP. If the roles mentioned above are not added, the generated access token lacks the required permissions resulting in an 'PERMISSION_DENIED' error.
- 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)'
))
Restore data
Let's hope we never need this :) Refer to Firebase's import manual if you need to restore data backed up by this script.
Disclaimer
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.