firebase/snippets-node

Q: same backup cronjob, multiple projects

naseemkullah opened this issue · 8 comments

Hi,

First off apologies if this is not the right place to ask this. But I am implementing the recommended firestore backup appengine cronjob in multiple projects and would like to avoid duplicating code, e.g. current setup:

appengine
├── README.md
├── gcpproject1
│   └── firestore-backup
│       ├── app.js
│       ├── app.yaml
│       ├── cron.yaml
│       ├── deploy.sh
│       └── package.json
└── gcpproject2
    └── firestore-backup
        ├── app.js
        ├── app.yaml
        ├── cron.yaml
        ├── deploy.sh
        └── package.json

Where only the deploy command in deploy.sh (which targets correct project) and url (query parameters outputUriPrefix and collections in particular) are the only differences in both folders.
What would be the recommended best practice to avoid duplicating the code? Symlinks to the common files?

@naseemkullah can you show an example of the differences? That would help to make a better recommendation. Also how are you deploying the appengine app?

Also it may be worth mentioning that since we published this guide, Firebase introduced Scheduled Functions which are (imo) easier to use than AppEngine Cron:
https://firebase.google.com/docs/functions/schedule-functions

Sure @samtstern,

Diff 1

Value of --project= in one-liner deploy.sh

#/bin/sh
gcloud app deploy app.yaml cron.yaml --project=gcp-project-1

vs

#/bin/sh
gcloud app deploy app.yaml cron.yaml --project=gcp-project-2

Diff 2

Value for cron[0].url in cron.yaml (query params outputUriPrefix and collections in particular)

cron:
- description: "Daily Cloud Firestore Export"
  url: /cloud-firestore-export?outputUriPrefix=gs://bucket-foo&collections=collection-foo
  target: cloud-firestore-admin
  schedule: every 24 hours

vs

cron:
- description: "Daily Cloud Firestore Export"
  url: /cloud-firestore-export?outputUriPrefix=gs://bucket-bar&collections=collection-bar
  target: cloud-firestore-admin
  schedule: every 24 hours

I am currently just deploying via CLI (deploy.sh one liner).
Will read up on schedule-functions now! Thanks.

@naseemkullah yeah I think if you used a scheduled functions you could just deploy one function that makes an HTTP call and uses and environment variable to decide on the project. Then you'd have 100% source sharing!

Great, thanks @samtstern !
Finally, is there any plan to update the backup guide with scheduled functions anytime soon?

@naseemkullah we'll probably leave it as-is since Scheduled Functions are a Firebase-specific thing and Cloud Firestore docs are shared between the Firebase and Google-Cloud sites, so we have to go with a generic option that works for everyone like AppEngine Cron.

@naseemkullah I'm going to close this issue since there's nothing to do in this repo to resolve it, but feel free to keep commenting and we can discuss your solution.

@naseemkullah well I guess I was wrong! See #56

w00t! Thanks for letting me know @samtstern!