You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS/Android应用如何每日自动导出Firebase/Firestore数据至邮箱并清空数据库?

Hey there! Let's walk through exactly how to build your daily data backup and cleanup workflow for either Firestore or Firebase Realtime Database. Both platforms can handle this seamlessly using Firebase's serverless tools—here's the step-by-step breakdown:

Core Workflow Overview

We'll use Cloud Functions for Firebase with a scheduled Pub/Sub trigger to run a daily job that:

  1. Exports all data from your database
  2. Sends the exported data to your specified email as an attachment
  3. Clears all data from the database
  4. Repeats automatically every day

Prerequisites First

Before diving in, make sure you have:

  • A Firebase project set up with either Firestore or Realtime Database enabled
  • The firebase-tools CLI installed locally (npm install -g firebase-tools)
  • Node.js installed to write Cloud Functions

Option 1: Implementation for Firestore

Firestore is great for document-based data. Here's how to build the daily job:

1. Write the Scheduled Cloud Function

Create a function with a cron trigger set to run daily (we'll use 0 0 * * * to execute at midnight UTC—adjust the cron expression if you need a different time).

const functions = require("firebase-functions");
const admin = require("firebase-admin");
const nodemailer = require("nodemailer");

admin.initializeApp();

// Configure email transporter (use Gmail, SendGrid, or your preferred service)
const transporter = nodemailer.createTransport({
  service: "Gmail",
  auth: {
    user: "your-app-email@gmail.com",
    pass: "your-app-specific-password" // For Gmail, use an App Password (not your regular password)
  }
});

// Scheduled function to run daily
exports.dailyBackupAndCleanup = functions.pubsub.schedule("0 0 * * *")
  .timeZone("UTC") // Adjust to your local time zone if needed
  .onRun(async (context) => {
    try {
      // Step 1: Export all Firestore data
      const db = admin.firestore();
      const allCollections = await db.listCollections();
      const backupData = {};

      for (const collection of allCollections) {
        const docsSnapshot = await collection.get();
        backupData[collection.id] = docsSnapshot.docs.map(doc => ({
          id: doc.id,
          ...doc.data()
        }));
      }

      // Step 2: Send backup email
      const dateString = new Date().toISOString().split("T")[0];
      const mailOptions = {
        from: "Your App <your-app-email@gmail.com>",
        to: "your-specified-recipient@example.com",
        subject: `Daily Firestore Backup - ${dateString}`,
        text: "Attached is your daily database backup.",
        attachments: [
          {
            filename: `firestore-backup-${dateString}.json`,
            content: JSON.stringify(backupData, null, 2)
          }
        ]
      };
      await transporter.sendMail(mailOptions);
      functions.logger.info("Backup email sent successfully");

      // Step 3: Clear all Firestore data (handle 500-document batch limit)
      for (const collection of allCollections) {
        let batch = db.batch();
        let docCount = 0;

        const docsSnapshot = await collection.get();
        docsSnapshot.docs.forEach(doc => {
          batch.delete(doc.ref);
          docCount++;

          // Execute batch every 500 documents
          if (docCount % 500 === 0) {
            await batch.commit();
            batch = db.batch();
          }
        });

        // Commit any remaining documents
        if (docCount % 500 !== 0) {
          await batch.commit();
        }
      }
      functions.logger.info("Firestore database cleared successfully");

      return null;
    } catch (error) {
      functions.logger.error("Workflow failed:", error);
      throw error;
    }
  });

Key Notes for Firestore

  • Firestore has a 500-document limit per batch operation, so our code handles batch deletion in chunks
  • If you have subcollections, extend the export logic to recursively fetch them (the example above only pulls top-level collections)

Option 2: Implementation for Firebase Realtime Database

Realtime Database is simpler for key-value data, and cleanup is more straightforward since you can delete the entire root node in one go.

1. Write the Scheduled Cloud Function

const functions = require("firebase-functions");
const admin = require("firebase-admin");
const nodemailer = require("nodemailer");

admin.initializeApp();

// Configure email transporter (same as Firestore example)
const transporter = nodemailer.createTransport({
  service: "Gmail",
  auth: {
    user: "your-app-email@gmail.com",
    pass: "your-app-specific-password"
  }
});

// Scheduled function to run daily
exports.dailyBackupAndCleanup = functions.pubsub.schedule("0 0 * * *")
  .timeZone("UTC")
  .onRun(async (context) => {
    try {
      // Step 1: Export all Realtime Database data
      const db = admin.database();
      const snapshot = await db.ref("/").once("value");
      const backupData = snapshot.val() || {};

      // Step 2: Send backup email
      const dateString = new Date().toISOString().split("T")[0];
      const mailOptions = {
        from: "Your App <your-app-email@gmail.com>",
        to: "your-specified-recipient@example.com",
        subject: `Daily Realtime DB Backup - ${dateString}`,
        text: "Attached is your daily database backup.",
        attachments: [
          {
            filename: `realtime-db-backup-${dateString}.json`,
            content: JSON.stringify(backupData, null, 2)
          }
        ]
      };
      await transporter.sendMail(mailOptions);
      functions.logger.info("Backup email sent successfully");

      // Step 3: Clear entire Realtime Database
      await db.ref("/").remove();
      functions.logger.info("Realtime Database cleared successfully");

      return null;
    } catch (error) {
      functions.logger.error("Workflow failed:", error);
      throw error;
    }
  });

Key Notes for Realtime Database

  • Deleting the root node (ref("/").remove()) clears all data in one operation—no batch handling needed
  • If you only want to clear specific paths instead of the entire DB, adjust the ref to target those paths

Deployment & Final Checks

  1. Install dependencies in your functions folder: npm install nodemailer firebase-admin firebase-functions
  2. Deploy the function to Firebase: firebase deploy --only functions
  3. Verify the scheduled trigger is active in the Firebase Console > Functions > Triggers
  4. Test manually by running firebase functions:shell and calling dailyBackupAndCleanup() to ensure everything works

Choosing Between Firestore and Realtime Database

Both work perfectly for your workflow. Pick:

  • Firestore if you need document-based data, complex queries, or multi-region support
  • Realtime Database if you want simpler key-value storage, lower latency for real-time updates, or easier full-database cleanup

内容的提问来源于stack exchange,提问作者Nikita Koniukh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:42:55