firebase / firebase-tools

The Firebase Command Line Tools
MIT License
4.04k stars 951 forks source link

Unable to restore database back-up #6862

Closed MichielvanBeers closed 8 months ago

MichielvanBeers commented 8 months ago

Environment info

firebase-tools: 13.4.0

Platform: Windows

Test case

Steps to reproduce

  1. Install Firebase-tools version 13.4.0
  2. Setup a Firestore database with the name (default)
  3. Create a backup schedule using the command firebase firestore:backups:schedules:create --recurrence DAILY --retention 7d
  4. Validate that backups are created successfully through firebase firestore:backups:list
  5. Retrieve specific backup through firebase firestore:backups:get projects/[project_id]/locations/eur3/backups/[backup_id]
  6. Try to restore backup using command firebase firestore:databases:restore --database '(default)' --backup projects/[project_id]/locations/eur3/backups/[backup_id]
  7. Retry same comment with different database id.
  8. Look up the Database in the Google Cloud Firestore

Expected behavior

Actual behavior

Output of command, including --debug flag

[2024-03-09T06:25:53.125Z] > command requires scopes: ["email","openid","https://www.googleapis.com/auth/cloudplatformprojects.readonly","https://www.googleapis.com/auth/firebase","https://www.googleapis.com/auth/cloud-platform"]
[2024-03-09T06:25:53.127Z] > authorizing via FIREBASE_TOKEN environment variable
!  Authenticating with `FIREBASE_TOKEN` is deprecated and will be removed in a future major version of `firebase-tools`. Instead, use a service account key with `GOOGLE_APPLICATION_CREDENTIALS`: https://cloud.google.com/docs/authentication/getting-started 
[2024-03-09T06:25:53.129Z] [iam] checking project [project_id] for permissions ["datastore.backups.restoreDatabase","firebase.projects.get"]
[2024-03-09T06:25:53.133Z] > refreshing access token with scopes: []
[2024-03-09T06:25:53.137Z] >>> [apiv2][query] POST https://www.googleapis.com/oauth2/v3/token [none]
[2024-03-09T06:25:53.138Z] >>> [apiv2][body] POST https://www.googleapis.com/oauth2/v3/token [omitted]
[2024-03-09T06:25:53.302Z] <<< [apiv2][status] POST https://www.googleapis.com/oauth2/v3/token 200
[2024-03-09T06:25:53.304Z] <<< [apiv2][body] POST https://www.googleapis.com/oauth2/v3/token [omitted]
[2024-03-09T06:25:53.310Z] >>> [apiv2][query] POST https://cloudresourcemanager.googleapis.com/v1/projects/[project_id]:testIamPermissions [none]
[2024-03-09T06:25:53.310Z] >>> [apiv2][(partial)header] POST https://cloudresourcemanager.googleapis.com/v1/projects/[project_id]:testIamPermissions x-goog-quota-user=projects/[project_id]
[2024-03-09T06:25:53.311Z] >>> [apiv2][body] POST https://cloudresourcemanager.googleapis.com/v1/projects/[project_id]:testIamPermissions {"permissions":["datastore.backups.restoreDatabase","firebase.projects.get"]}
[2024-03-09T06:25:53.607Z] <<< [apiv2][status] POST https://cloudresourcemanager.googleapis.com/v1/projects/[project_id]:testIamPermissions 200
[2024-03-09T06:25:53.608Z] <<< [apiv2][body] POST https://cloudresourcemanager.googleapis.com/v1/projects/[project_id]:testIamPermissions {"permissions":["datastore.backups.restoreDatabase","firebase.projects.get"]}
[2024-03-09T06:25:53.610Z] >>> [apiv2][query] POST https://firestore.googleapis.com/v1/projects/[project_id]/databases:restore databaseId=%28default%29
[2024-03-09T06:25:53.610Z] >>> [apiv2][body] POST https://firestore.googleapis.com/v1/projects/[project_id]/databases:restore {"databaseId":"(default)","backup":"projects/[project_id]/locations/eur3/backups/[backup_id]"}
[2024-03-09T06:26:01.946Z] <<< [apiv2][status] POST https://firestore.googleapis.com/v1/projects/[project_id]/databases:restore 409
[2024-03-09T06:26:01.947Z] <<< [apiv2][body] POST https://firestore.googleapis.com/v1/projects/[project_id]/databases:restore {"error":{"code":409,"message":"Database already exists. Please use another database_id","status":"ALREADY_EXISTS"}}

Error: HTTP Error: 409, Database already exists. Please use another database_id
[2024-03-09T06:26:01.957Z] Error Context: {
  "body": {
    "error": {
      "code": 409,
      "message": "Database already exists. Please use another database_id",
      "status": "ALREADY_EXISTS"
    }
  },
  "response": {
    "statusCode": 409
  }
}

Thank you in advance!

rwhogg commented 8 months ago

Hi @MichielvanBeers, thanks for bringing this up!

If you are going to restore to a database which already exists, you have to:

  1. Delete that database (in this case '(default)'). You can use firebase firestore:databases:delete '(default)' to accomplish this.
  2. Wait 5 minutes
  3. Attempt the restore, using the same command you listed in step 6 of your issue
MichielvanBeers commented 8 months ago

Thanks for your answer @rwhogg. What would be your recommended way to do this with a production database? Since first deleting the whole database and then hoping that the restore will go correctly, feels a bit risky to me 😅

rwhogg commented 5 months ago

@MichielvanBeers sorry for the delay, the recommendations are now available here: https://firebase.google.com/docs/firestore/restore-in-place

MichielvanBeers commented 5 months ago

Hi @rwhogg, that is great news, thank you! I will give this a try :)