Open Venryx opened 2 years ago
That took longer than I expected...
Anyway, the "missing steps" are as follows:
1) Make sure you have a "functions" section specified in your firebase.json
:
{
[...]
"functions": {
"runtime": "nodejs12",
"source": "Firebase/CloudFunctions" // you can choose a different path, of course
}
}
2) In your cloud-functions folder, create a package.json
file with these contents:
{
"name": "functions",
"private": true,
"main": "index.js",
"dependencies": {
"firebase-admin": "9.12.0",
"firebase-functions": "3.15.7",
"simple-firestore-backup": "1.0.6" // can add a caret to have this auto-update, but if you're paranoid about malicious actors (since these sorts of scripts are highly privileged), it may be better to lock to a specific version
}
}
3) Install the dependencies listed in package.json
, by running the following in the cloud-functions folder:
npm install
4) In your cloud-functions folder, create an index.js
file:
exports.BackupDB = require("./BackupDB.js");
5) In your cloud-functions folder, create a BackupDB.js
file: (based on the snippet in the readme)
const functions = require("firebase-functions");
const firestoreBackup = require("simple-firestore-backup");
// Optional: 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')
const bucket = undefined; //"your-project-backups";
// Optional: The path inside the bucket. Defaults to 'firestore'
const bucketPath = undefined; //"path/to/backups";
// Optional: The Firestore instance id to backup. If you did not create a second Firestore instance, you can leave this out. Defaults to '(default)'
const firestoreInstance = undefined; //"firestore-instance-id";
const cloudFunc = 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") // if you don't care what time of day the backup runs
.schedule("every day 00:00").timeZone("America/Los_Angeles") // if you have a preference
.onRun(firestoreBackup.createBackupHandler(bucket, bucketPath, firestoreInstance));
module.exports = cloudFunc;
6) Actually deploy the cloud-function, by running the following in the cloud-functions folder: (you must, of course, have the Firebase CLI installed)
firebase --project YOUR_PROJECT_ID deploy --only functions:BackupDB
If you get the error
Error: HTTP Error: 401, Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.
, then fix it by first running:firebase login --reauth
(as mentioned here)
Also, if you want to manually trigger a backup, you can do that by:
1) Navigating to: https://console.cloud.google.com/functions/list
2) Making sure you have the correct project selected.
3) Opening the BackupDB
cloud function in the list. (or whatever you named it)
4) Opening the "Testing" tab.
5) Pressing "Test the function".
The logs that show (either below the button, or in the "Logs" panel) will tell you whether it succeeded or not. (you can of course also just check the cloud-storage bucket yourself)
Could instructions along the lines of the above be added to the readme? Without it, it can take an hour or more for newcomers to cloud functions to piece everything together. (as it did for me)
The final step -- of how to actually deploy the cloud function -- is less clear than it could be.
I know people can look through the Firestore documentation to find out how, but it's unfortunate because all the other steps one can complete without having to do that.
I will update this issue with the exact deployment commands, once I've found out how, and confirmed it works.