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

如何通过Cron Jobs定时调度Firestore备份工具?新手求助

How to Schedule Your Firestore Backup with Cron or Windows Task Scheduler

Got it, let's break down how to set up scheduled Firestore backups using Cron (for Linux/macOS) or Windows Task Scheduler (for Windows)—since you already have the backup command working, this should be straightforward. I’ll cover both platforms just in case you’re not sure which one you’re using!

For Linux/macOS (Using Cron Jobs)

Cron is the built-in scheduler for Unix-like systems, perfect for repeating tasks like backups. Here’s your step-by-step guide:

  1. Get the absolute path to firestore-backup-restore
    Cron runs in a minimal environment, so it won’t recognize relative paths or user-specific shortcuts. Run this command to find the full path:

    which firestore-backup-restore
    

    You’ll get something like /usr/local/bin/firestore-backup-restore—save this path for later.

  2. Create a backup shell script
    Instead of pasting the full command directly into Cron, make a script to keep things clean and add logging. Create a file (e.g., firestore-backup.sh) with this content:

    #!/bin/bash
    # Firestore daily backup script
    
    # Replace these paths with your actual absolute paths
    BACKUP_TOOL="/usr/local/bin/firestore-backup-restore"
    CREDS="/home/your-username/Backups/Firestore/budget-my-reno-firebase-adminsdk-lh8d5-5bfad6f090.json"
    BACKUP_DIR="/backups/myDatabase"
    LOG_FILE="/var/log/firestore-backup.log"
    
    # Create backup directory if it doesn't exist
    mkdir -p "$BACKUP_DIR"
    
    # Run the backup and log all output
    "$BACKUP_TOOL" --accountCredentials "$CREDS" --backupPath "$BACKUP_DIR" >> "$LOG_FILE" 2>&1
    

    Update the paths to match your system (especially your-username and the credential file location). Then make the script executable:

    chmod +x firestore-backup.sh
    
  3. Add the script to Cron
    Open your user’s Cron table with:

    crontab -e
    

    Add a line to schedule the backup. For example, to run it every day at 2 AM, add:

    0 2 * * * /home/your-username/firestore-backup.sh
    

    Let’s break down the Cron syntax: 0 (minute), 2 (hour), * (every day), * (every month), * (every day of the week). Adjust the numbers to fit your preferred schedule (e.g., 30 1 * * 0 for 1:30 AM every Sunday).

  4. Verify and test
    Check that your Cron job was added successfully:

    crontab -l
    

    Manually run the script to confirm it works:

    ./firestore-backup.sh
    

    Then check the log file to make sure there are no errors:

    tail /var/log/firestore-backup.log
    

For Windows (Using Task Scheduler)

Windows doesn’t have Cron, but Task Scheduler does the same job. Here’s how to set it up:

  1. Create a batch script
    First, find the full path to firestore-backup-restore by running where firestore-backup-restore in Command Prompt—this will give you a path like C:\Users\YourName\AppData\Roaming\npm\firestore-backup-restore.cmd.

    Create a .bat file (e.g., firestore-backup.bat) with this content:

    @echo off
    REM Firestore daily backup script
    
    REM Replace these paths with your actual paths
    SET BACKUP_TOOL="C:\Users\YourName\AppData\Roaming\npm\firestore-backup-restore.cmd"
    SET CREDS="D:\Backups\Firestore\budget-my-reno-firebase-adminsdk-lh8d5-5bfad6f090.json"
    SET BACKUP_DIR="D:\backups\myDatabase"
    SET LOG_FILE="D:\backups\firestore-backup-log.txt"
    
    REM Create backup directory if it doesn't exist
    if not exist %BACKUP_DIR% mkdir %BACKUP_DIR%
    
    REM Run the backup and log all output
    %BACKUP_TOOL% --accountCredentials %CREDS% --backupPath %BACKUP_DIR% >> %LOG_FILE% 2>&1
    
  2. Set up Task Scheduler

    • Open Task Scheduler (search for it in the Start Menu).
    • Click Create Basic Task in the right-hand pane.
    • Name your task (e.g., "Daily Firestore Backup") and add a description if you want.
    • Choose a trigger (e.g., "Daily") and set your preferred time (like 2 AM).
    • For the action, select Start a program, then browse to your .bat file.
    • Click Finish, then right-click the task in the list and select Run to test it works.
    • Optional: Go to the task’s Properties > Settings tab, check "Run whether user is logged on or not" so the backup runs even if you’re not signed in.

Important Notes

  • Check backups regularly: Don’t just set it and forget it—periodically verify that backup files are being created and that the log has no errors.
  • Permissions: Make sure the user running the Cron/Task Scheduler has read access to the service account credentials and write access to the backup directory.
  • Environment variables: On Linux, if your backup fails, add PATH=/usr/local/bin:$PATH at the top of your shell script to ensure Cron can find Node.js and firestore-backup-restore.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 07:40:43