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.

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Step 1: Set Up Your Environment
Before you begin restoring Firestore from a backup, ensure your environment is properly set up:
npm install -g firebase-toolsfirebase loginEnsure 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:
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:
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.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.