如何每日导出Redis数据库并在午夜清空其全部数据?
Got it, let's tackle your two requirements: nightly Redis data exports and midnight database flushes. Since your business only runs 8:00–20:00 and all daily tasks wrap up after that, we can safely schedule these operations during off-hours without disrupting anything.
1. Nightly Redis Data Export
You’ve got two solid options here, depending on whether you need a binary backup (for fast restoration) or a human-readable export.
Option 1: Use Redis' Built-in RDB Snapshot
If your Redis instance is already configured to generate RDB snapshots (check your redis.conf for save directives), you can leverage that for backups. To trigger a manual snapshot for your nightly schedule:
- Run the background save command to avoid blocking Redis (though since your business is offline, even the blocking
SAVEis safe):redis-cli BGSAVE - Once the save finishes (confirm with
redis-cli LASTSAVEto get the timestamp of the last successful save), copy thedump.rdbfile to your backup location. Add a date stamp to keep backups organized:cp /var/lib/redis/dump.rdb /path/to/your/backups/redis_backup_$(date +%Y%m%d).rdb
Adjust the paths to match your actual Redis installation—common RDB locations are /var/lib/redis/ or /etc/redis/.
Option 2: Export to a Human-Readable Format
If you need to inspect the exported data easily, use redis-cli to dump keys and values to a text file:
# For a structured text dump (good for small to medium datasets) redis-cli KEYS "*" | while read key; do echo "Key: $key | Value: $(redis-cli GET "$key")" done > /path/to/your/backups/redis_text_backup_$(date +%Y%m%d).txt
Note: For very large datasets, KEYS * can be slow—swap it out for the SCAN command if needed, but since Redis is quiet overnight, KEYS should work just fine.
2. Clear Redis Data at Midnight
To start fresh every day, use Redis' FLUSHALL command—it deletes all keys from all databases in your Redis instance.
Safe Execution
Since your business isn’t running after 20:00, running this at midnight carries zero risk. Execute it with:
redis-cli FLUSHALL
If your Redis requires password authentication, add the -a flag or authenticate first:
# With password flag redis-cli -a your_redis_password FLUSHALL # Or authenticate within the CLI redis-cli AUTH your_redis_password FLUSHALL
3. Automate Everything with Cron
To make these tasks run automatically every day, set up cron jobs on your Redis server.
- Open your crontab editor:
crontab -e - Add these lines to schedule the export (e.g., at 21:00, right after business hours) and flush at midnight:
# Export Redis data daily at 21:00 0 21 * * * redis-cli BGSAVE && cp /var/lib/redis/dump.rdb /path/to/your/backups/redis_backup_$(date +\%Y\%m\%d).rdb # Flush all Redis data daily at midnight 0 0 * * * redis-cli FLUSHALL
The \% escapes are necessary in cron because % has special meaning (it denotes newline characters). Adjust the paths and times to fit your needs.
Quick Checks Before Going Live
- Permissions: Make sure the user running the cron job has read access to
dump.rdband write access to your backup directory. - Test Manually: Run the export and flush commands once first to confirm they work as expected.
- AOF Persistence Note: If you use AOF instead of RDB, run
redis-cli BGREWRITEAOFbefore copying the AOF file to ensure it’s consistent (or stick withBGSAVEto generate an RDB snapshot for backups).
内容的提问来源于stack exchange,提问作者jdog

