2

I am using Firestore NoSQL for iOS application. While debugging, I occasionally executed setData instead of updateData, what led to lost of all the user data (one user).

Is there a way to reverse back the changes?

How to do Versioning for Firestore, for similar cases, so there's a way to cancel/ backup. I don't quite know, but I've read that it supposed to be Versioning.

MikeMaus
  • 385
  • 3
  • 22

3 Answers3

2

There is no option to undo changes in Firestore. I'd recommend creating a new database (project) for testing purposes so you don't accidentally write on the main database. For now if it's just a single user then I'd recommend manually add that back.

Unlike realtime database, you can only have one Firestore instance per project at the moment so you may have to create a new project but I think that'll be safe to prevent such accidental writes :)

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
  • thanks! Is there any way to secure Firestore database? maybe it is possible to make copies through *Firebase Functions*? – MikeMaus Jul 17 '21 at 17:03
  • @MikeMaus yes you can maintain versions of documents yourself in a sub-collection. Checkout [this answer](https://stackoverflow.com/questions/48820346/how-can-i-use-undo-redo-or-in-google-cloud-firestore) for detailed explanation. – Dharmaraj Jul 17 '21 at 17:04
  • 1
    Got it, but still seems prone to mistakes; – MikeMaus Jul 17 '21 at 17:48
1

We do a daily backup of our firestore data in our cloud functions. It's as simply as

const firestore = require('@google-cloud/firestore');
const client = new firestore.v1.FirestoreAdminClient();

export const dailyFirestoreBackup = functions
    .runWith({ timeoutSeconds: 540 })
    .pubsub.schedule('00 0 * * *')
    .onRun((context) => {

        const projectId = process.env.GCP_PROJECT || process.env.GCLOUD_PROJECT;
        const databaseName =
            client.databasePath(projectId, '(default)');

        return await client.exportDocuments({
            name: databaseName,
            outputUriPrefix: 'gs://your-backup-bucket',
            // Leave collectionIds empty to export all collections
            // or set to a list of collection IDs to export,
            // collectionIds: ['users', 'customers']
            collectionIds: []
        });
});

You can then in those cases recover your data from the previous day by downloading the bucket data from the day before.

enter image description here

When it's a specific id you are looking for you can even simply run

grep -R "<documentId>" .

to get the file you are looking for.

Or you try to setup a local firestore emulator and import the data there.

UPDATE

Since some time you also have the possibility to use the point-in-time recovery for your firestore database. Find more information about it here.

This might be a better option for you if you don't have functions set up. I by myself didn't try it yet though.

luckyhandler
  • 10,651
  • 3
  • 47
  • 64
  • thank you very much ! I learned so much from it. Nobody didn't want to help me with this or share with me. I asked many people IRL. And looked on internet. Thanks ! – MikeMaus Aug 18 '23 at 12:39
  • where do you put this code ? what service – MikeMaus Aug 18 '23 at 12:42
  • 1
    @MikeMaus This is a cloud function so you put it into you cloud functions code. Here is more information on how to set this up if you didn't so this so far. https://firebase.google.com/docs/functions/get-started?hl=en&gen=2nd I also added "Point in Time Recovery" information which is a new concept and can be done without cloud functions. – luckyhandler Aug 19 '23 at 09:26
0

"You can use the Cloud Firestore managed export and import service to recover from accidental deletion of data and to export data for offline processing."

That's what I've found for now: https://firebase.google.com/docs/firestore/manage-data/export-import

MikeMaus
  • 385
  • 3
  • 22