/firebase-tutorials

How to backup Firestore data?

Backup your Firestore data with our step-by-step guide. Set up Google Cloud and Firebase, configure Node.js exports, automate backups, and verify your data.

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 backup Firestore data?

 

Step 1: Set Up Your Google Cloud Project

 

  • Navigate to the Google Cloud Console.
  • Choose the project that has your Firebase app.
  • If you haven’t already, enable billing on your project. This is necessary to use Google Cloud Storage, where you'll back up your data.

 

Step 2: Enable Cloud Firestore and Cloud Storage in Firebase

 

  • Go to the Firebase Console.
  • Select your project.
  • Ensure Cloud Firestore is enabled. If not, you'll need to enable it by following the Firestore setup process.
  • Go to the Storage section and enable Cloud Storage.

 

Step 3: Configure Firestore Exports

 

  • In the Google Cloud Console, search for “Firestore” and select it.
  • Click on the “Export” tab.
  • Setup export destination by specifying a cloud storage location. You can create a bucket in Google Cloud Storage if it doesn't exist yet.

 

Step 4: Set Up Firebase Admin SDK in Your Server Environment

 

  • Install Firebase Admin SDK in your project directory using npm or yarn:

    
    npm install firebase-admin
    
  • Initialize Firebase Admin SDK in your script:

    
    const admin = require('firebase-admin');
    
    admin.initializeApp({
      credential: admin.credential.applicationDefault(), // Ensure you have set the GOOGLE_APPLICATION_CREDENTIALS environment variable
      databaseURL: 'YOUR_FIREBASE_DATABASE\_URL'
    });
    
    const firestore = admin.firestore();
    

 

Step 5: Create Firestore Export Script

 

  • Write a Node.js script for exporting your Firestore data:
    
    const { exec } = require('child\_process');
    
    const projectId = 'YOUR_PROJECT_ID';
    const bucket = 'YOUR_BUCKET_NAME';
    const currentTime = new Date().toISOString();
    const collectionGroupIds = ['YOUR_COLLECTION_IDS']; 
    
    const command = `gcloud firestore export gs://${bucket}/${currentTime} --collection-ids=${collectionGroupIds.join(',')} --project=${projectId}`;
    
    exec(command, (error, stdout, stderr) => {
      if (error) {
        console.error(`Error: ${error.message}`);
        return;
      }
      if (stderr) {
        console.error(`Standard Error: ${stderr}`);
        return;
      }
      console.log(`Standard Output: ${stdout}`);
    });
    

 

Step 6: Schedule Automated Backups

 

  • Use a task scheduler like cron on UNIX-based systems or Task Scheduler on Windows to automate backup script execution.

For example, using cron:


0 2 _ _ * /path/to/node /path/to/your/export-script.js >/dev/null 2>&1

This runs the script every day at 2 AM.

 

Step 7: Verify Backup Data

 

  • After running the export script, go to your Google Cloud Storage Console to confirm that the exported Firestore data is available in your specified bucket.

 

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