如何通过Cron Jobs定时调度Firestore备份工具?新手求助
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:
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-restoreYou’ll get something like
/usr/local/bin/firestore-backup-restore—save this path for later.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>&1Update the paths to match your system (especially
your-usernameand the credential file location). Then make the script executable:chmod +x firestore-backup.shAdd the script to Cron
Open your user’s Cron table with:crontab -eAdd a line to schedule the backup. For example, to run it every day at 2 AM, add:
0 2 * * * /home/your-username/firestore-backup.shLet’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 * * 0for 1:30 AM every Sunday).Verify and test
Check that your Cron job was added successfully:crontab -lManually run the script to confirm it works:
./firestore-backup.shThen 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:
Create a batch script
First, find the full path tofirestore-backup-restoreby runningwhere firestore-backup-restorein Command Prompt—this will give you a path likeC:\Users\YourName\AppData\Roaming\npm\firestore-backup-restore.cmd.Create a
.batfile (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>&1Set 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
.batfile. - 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:$PATHat the top of your shell script to ensure Cron can find Node.js andfirestore-backup-restore.
内容的提问来源于stack exchange,提问作者Sampath

