/firebase-tutorials

How to restore Firestore from backup?

Restore Firestore data from a backup using Firebase CLI and Node.js. Follow our guide to set up your environment, import data, and verify the restoration process.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to restore Firestore from backup?

 

Step 1: Set Up Your Environment

 

Before you begin restoring Firestore from a backup, ensure your environment is properly set up:

  • Make sure you have the Firebase CLI installed. You can do so by running:
    npm install -g firebase-tools
  • Authorize the Firebase CLI by running:
    firebase login

Ensure you have access to the project from which you are restoring data. List all your projects to verify:

firebase projects:list

 

Step 2: Prepare Your Backup Files

 

Locate your Firestore backup files, which can be in JSON or another structured data format. Ensure that these files are accessible, and remember their paths for later use. Note that the structure of these files should mirror your Firestore database for a smooth restoration process.

 

Step 3: Set Your Firebase Project

 

You need to set the correct Firebase project in the CLI. Do this by executing:

firebase use <project-id>

Replace <project-id> with the ID of the project you plan to restore data to.

 

Step 4: Restore Firestore Data

 

To begin the restoration of your Firestore data, utilize the Firestore import command. This command will be executed in conjunction with a script that handles the conversion and importation of your data files to Firestore.

Use the following code snippet as a guide for a JavaScript script that reads JSON formatted data and restores it into Firestore. Save this script as restore-firestore.js.


const admin = require('firebase-admin');
const serviceAccount = require('./path/to/your/serviceAccountKey.json');

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount)
});

const firestore = admin.firestore();
const backupData = require('./path/to/your/backup.json'); // Your JSON backup data

const restoreData = async (collection, data) => {
  const batch = firestore.batch();
  data.forEach(doc => {
    const docRef = firestore.collection(collection).doc(doc.id);
    batch.set(docRef, doc.data);
  });

  await batch.commit();
  console.log(`Restored ${data.length} documents to ${collection}`);
};

// Example usage
const runRestore = async () => {
  for (let collection in backupData) {
    await restoreData(collection, backupData[collection]);
  }
  console.log('Restoration complete.');
};

runRestore().catch(console.error);

Customize the file paths for serviceAccountKey.json and backup.json according to your file locations. Then execute the script.

Run the script using:

node restore-firestore.js

 

Step 5: Verify the Restoration

 

After running the restoration script, verify that your data has been successfully restored to Firestore:

  • Open the Firebase console.
  • Navigate to Firestore Database and check if the collections and documents appear as expected.

By doing this, you ensure that the restoration process was successful, and the data integrity is maintained.

 

Step 6: Handle Errors and Debugging

 

In case of errors during restoration, carefully inspect:

  • Error messages output in the console. These can pinpoint issues in parsing the backup files or permission problems.
  • The format of the backup file to ensure it aligns with the expected structure in Firestore.
  • Verify that the service account has the necessary permissions to write data to Firestore.

Modify any scripts or data files as necessary and reattempt the restoration process. Use console logging or debugging to address specific issues.

 

Conclusion

 

Restoring data to Firestore from a backup involves setting up your environment, preparing and formatting your data correctly, and using a script to automate the restoration process. Always verify the data post-restoration and troubleshoot errors effectively to maintain the integrity of your database.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022