npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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.
  1. 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;
      }
    }
  }
  1. 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.
  1. 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.