Enterprise‑Level Redis RDB Cold Backup and Recovery Guide
This article explains why Redis RDB snapshots are ideal for enterprise cold backups, provides step‑by‑step Linux scripts and cron jobs for hourly and daily backup retention, and details the complete restoration process from saved RDB files.
Redis RDB persistence creates memory snapshots that are low‑frequency, fast to restore, and highly configurable, making them suitable for enterprise cold‑backup solutions where the snapshot files are copied to other machines or cloud servers.
Reasons to use RDB for cold backup:
The generated RDB file changes rarely unless checkpoints are triggered.
RDB snapshots restore faster than AOF logs.
Generation strategies are fully configurable to match different usage scenarios.
Backup scheme
1. Use Linux crontab to schedule backup scripts. 2. Perform an hourly backup of the latest RDB snapshot, retaining the last 48 hours. 3. Perform a daily backup, retaining the last month. 4. Transfer backups to a remote cloud server each night.
Hourly backup
Create the necessary directories and a shell script:
mkdir /usr/local/redis
mkdir /usr/local/redis/copy
vi /usr/local/redis/copy/redis_rdb_copy_hourly.sh
mkdir /usr/local/redis/snapshotting
chmod 777 /usr/local/redisHourly script ( redis_rdb_copy_hourly.sh ) content:
#!/bin/sh
cur_date=`date +%Y%m%d%H`
rm -rf /usr/local/redis/snapshotting/$cur_date
mkdir /usr/local/redis/snapshotting/$cur_date
cp /var/redis/6379/dump.rdb /usr/local/redis/snapshotting/$cur_date
del_date=`date -d -48hour +%Y%m%d`
rm -rf /usr/local/redis/snapshotting/$del_dateSchedule it to run at the start of each hour:
crontab -e
0 * * * * sh /usr/local.redis/copy/redis_rdb_copy_hourly.shManually test the script:
cd /usr/local/redis/copy
./redis_rdb_copy_hourly.shThe script creates a directory named with the UTC hour (e.g., 2021080809 ) containing the dump.rdb file.
Daily backup
Create a daily backup script:
vi /usr/local/redis/copy/redis_rdb_copy_daily.sh
chomd 777 *Daily script content:
#!/bin/sh
cur_date=`date +%Y%m%d`
rm -rf /usr/local/redis/snapshotting/$cur_date
mkdir /usr/local/redis/snapshotting/$cur_date
cp /var/redis/6379/dump.rdb /usr/local/redis/snapshotting/$cur_date
del_date=`date -d -1month +%Y%m%d`
rm -rf /usr/local/redis/snapshotting/$del_dateSchedule it to run at midnight each day:
crontab -e
0 0 * * * sh /usr/local/redis/copy/redis_rdb_copy_daily.shManually test the daily script:
cd /usr/local/redis/copy
./redis_rdb_copy_daily.shThe script creates a directory named with the UTC date (e.g., 20210808 ) containing the dump.rdb file.
Recovery from backup
If data corruption occurs, restore by selecting an earlier backup:
Stop Redis and temporarily disable AOF persistence.
Delete existing AOF log and RDB snapshot files.
Copy the chosen backup dump.rdb into Redis's data directory.
Restart Redis and verify data integrity.
Re‑enable AOF so that Redis writes memory changes to the AOF file.
Stop Redis again, adjust the configuration to ensure AOF is active, and restart.
This procedure ensures that a clean snapshot can be restored quickly, providing a reliable safety net for production environments.
Wukong Talks Architecture
Explaining distributed systems and architecture through stories. Author of the "JVM Performance Tuning in Practice" column, open-source author of "Spring Cloud in Practice PassJava", and independently developed a PMP practice quiz mini-program.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.