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

如何每日导出Redis数据库并在午夜清空其全部数据?

Nightly Redis Export & Midnight Database Flush Solution

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 SAVE is safe):
    redis-cli BGSAVE
    
  • Once the save finishes (confirm with redis-cli LASTSAVE to get the timestamp of the last successful save), copy the dump.rdb file 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.

  1. Open your crontab editor:
    crontab -e
    
  2. 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.rdb and 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 BGREWRITEAOF before copying the AOF file to ensure it’s consistent (or stick with BGSAVE to generate an RDB snapshot for backups).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 07:36:45