How to clone Firebase Project to Another Account

Introduction

If you want to migrate your project or clone your project from one account to another account [different gmail accounts] or within same account but different project [usecase where we clone dev to test or production], then follow the steps mentioned below.

Steps to Setup/Configure for Clone or Migration of Firebase Project

1. Login to your respective accounts [use different browser or incognito so that none mess with each other].

2. Both of the project should have billing enabled, as migration can be done only for the “Pay as you go” plan.

3. You need to have following roles in both the projects “Owner, Cloud Datastore Owner, or Cloud Datastore Import Export Admin”. Ignore if you are the owner of both the projects, you will have these by default.

4. Enable Firestore Database [if it is being required] with right location [the one nearest to the potential user of the apps. You can find the location here.]

5. Once Firestore is enable, visit the Indexes tab and replicate the entire composite indexed from the source project [the one that we are going to clone].

It can be even done after migration, but it is recommended by Google to do this step before migration.

6. Now Start Cloud Shell for each of the projects. To do, open this link in both the browsers [in new tab. To avoid any confusion we have already suggested opening the firebase in different browser or incognito]. Once you open the link you will see Google Cloud Shell like this:

7. Once you have reached to the above screen for both of your projects, we need to make sure that the gcloud (Google Cloud) is configured for the correct project. For that, you need to enter the following in the above shell (the black screen in the browser):
gcloud config set project [YOUR_PROJECT_ID]

The project ID may be different for both source and destination.

Authorize, if asked.

Now you have successfully done the setup.

To remove any further confusion, we will divide the steps into two parts, i.e. Export (From Source) and Import (To Destination).

Steps to Export a Firebase Project

1. The very first step is to configure your gcloud with the right project that you plan to export, using the below command:

gcloud config set project [SOURCE_PROJECT_ID]

2. Now, we will be zipping all your configuration and data and uploading it to your firebase storage (also known as bucket). In case you have not enabled it, you will have to.

The bucket name created by default is in this format: PROJECT_ID@appspot.gserviceaccount.com

Whatever it is, copy as shown below:

Once you get the Project ID and Bucket name, then replace them in below code:

gsutil iam ch serviceAccount:[SOURCE_PROJECT_ID]@appspot.gserviceaccount.com:admin \
gs://[BUCKET_NAME]

Now enter the above code in the console of your GCloud [attention, this is for source project, close the destination one if you are confused]. With this you have given required authorization.

3. Now, we will exporting everything from source [you can also export specific things, it is recommended to transfer everything and delete unrequired later]

Enter the following command to start export:

gcloud firestore export gs://[SOURCE_BUCKET] –async

4. While the export is in progress, take a note of the “outputURIPrefix “. It will be required later. This timestamp will be added to your exported file

outputUriPrefix: gs://[SOURCE_BUCKET]/2022-10-09T11:37:55_16224

5. Now, you can always check the status of your export using the code mentioned below:

gcloud firestore operations list

If the operationState: SUCCESSFUL is shown, then we will start with the import steps.

6. Now, we need to give access of the above object store to the destination project. To do that, prepare your code using below command and paste in the console:

gsutil iam ch serviceAccount:[DESTINATION_PROJECT_ID]@appspot.gserviceaccount.com:legacyBucketReader,legacyObjectReader \
gs://[SOURCE_BUCKET]

Steps to Import a Firebase Project

1. The very first step is to configure your gcloud with the right project that you plan to export, using the below command [in the destination command line]:

gcloud config set project [DESTINATION_PROJECT_ID]

2. Now prepare the code for import via using the below command. Here Export Prefix is the pre-fix in your export operation’s outputUriPrefix (The timestamp that you have saved, example 2022-10-09T11:37:55_16224) [in the destination command line]

gcloud firestore import gs://[SOURCE_BUCKET]/[EXPORT_PREFIX] –async

Example: gcloud firestore import gs://[SOURCE_BUCKET]/ 2022-10-09T11:37:55_16224 –async

If you get an error “PERMISSION_DENIED: Service account does not have access to Google Cloud Storage file”. Then with it you will also get an service account ID which doesn’t have the access. Example: service-12345@gcp-sa-firestore.iam.gserviceaccount.com

Then, create a command as shown below:

gsutil iam ch serviceAccount:<the account ID not having access>:roles/storage.admin \<the source bucket>

And run this command in the source console.

3. As earlier you can track the process using the below command [in the destination command line]

gcloud firestore operations list

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.