Learn Labs
Persistence

Backups & Restore

Taking, shipping, and restoring RDB backups, and testing them

Persistence keeps data across restarts on one machine. Backups keep it across losing the machine, a bad deploy that corrupts data, or a FLUSHALL typed into the wrong terminal. Replicas don't help with the last two: they faithfully replicate the mistake.

Taking a backup

An RDB file is the backup format: compact, a single file, and safe to copy once written (Redis writes to a temp file and renames it).

# trigger a fresh snapshot and wait for it
docker exec redis redis-cli --user default --pass redis123 --no-auth-warning BGSAVE
docker exec redis redis-cli --user default --pass redis123 --no-auth-warning INFO persistence | grep rdb_bgsave_in_progress
# rdb_bgsave_in_progress:0  -> done

# copy it out, timestamped
docker cp redis:/data/dump.rdb ./backups/redis-$(date +%Y%m%dT%H%M%S).rdb

Before copying, compare LASTSAVE with the time you called BGSAVE to be sure the file you copy is the new one.

Remote alternatives that don't need filesystem access:

redis-cli --user default --pass redis123 --rdb ./backup.rdb      # streams an RDB over the network
redis-cli ... --functions-rdb ./functions.rdb                     # just the Functions libraries

A backup policy

  1. Schedule snapshots: hourly copies kept for 2 days, daily copies kept for 30 days, depending on how far back you might need to go.
  2. Ship them off the host to object storage (S3, or the SeaweedFS module in this repo), encrypted.
  3. Monitor the age of the newest backup, and alert if it's older than expected. Silent backup failure is the classic failure.
  4. Test restores regularly. A backup you've never restored is a hope, not a backup.

Restoring

Restoring an RDB file into an instance that runs with appendonly yes is less obvious than it looks. Two traps were found while writing this lesson, both by testing the restore on this repo's stack.

Trap 1: the AOF wins. At startup, if AOF is enabled, Redis loads the AOF and ignores dump.rdb. Copy in a backup but leave the old appendonlydir, and the restore silently does nothing.

Trap 2: deleting the AOF isn't enough. If AOF is enabled and no AOF exists, Redis 8 doesn't fall back to dump.rdb. It starts empty, creates a fresh empty AOF, and writes an empty snapshot over your dump.rdb. The backup you just copied in is gone.

The procedure that works: load the RDB once with AOF off, then turn AOF on while running, which writes a new AOF from what's in memory.

# 1. stop Redis, put the backup in place, remove the old AOF
docker compose -f compose_prod.yaml stop redis
docker run --rm -v redis_data:/data -v "$PWD/backups:/backups" redis:8.8.3 sh -c '
rm -rf /data/appendonlydir &&
cp /backups/redis-20261012T081500.rdb /data/dump.rdb &&
chown redis:redis /data/dump.rdb'

# 2. start a one-off Redis on the same volume with AOF disabled: it loads dump.rdb
docker run -d --name redis-restore -v redis_data:/data -v "$PWD/config:/usr/local/etc/redis:ro" redis:8.8.3 redis-server /usr/local/etc/redis/redis.conf --appendonly no
docker exec redis-restore redis-cli --user default --pass redis123 DBSIZE    # check the data

# 3. enable AOF live; Redis writes a new AOF from memory. Wait for the rewrite to finish.
docker exec redis-restore redis-cli --user default --pass redis123 CONFIG SET appendonly yes
docker exec redis-restore redis-cli --user default --pass redis123 INFO persistence | grep aof_rewrite
#   aof_rewrite_in_progress:0 and aof_rewrite_scheduled:0  -> done

# 4. shut it down cleanly and start the real service
docker exec redis-restore redis-cli --user default --pass redis123 SHUTDOWN
docker rm redis-restore
docker compose -f compose_prod.yaml start redis

After every restore, verify: DBSIZE, a few known keys, and the startup log line saying which file was loaded.

Point in time

RDB backups restore to the moment they were taken. There's no built-in point-in-time recovery like Postgres's WAL archiving. If you need "the data as of 14:02," the closest options are frequent snapshots, or keeping copies of the AOF files and truncating the incremental file at the right command (possible, but manual and fiddly).

This is another way of saying: data you can't afford to lose belongs in a database designed for it. Redis backups are for fast recovery of data that's expensive, but not impossible, to rebuild.

Lab

  1. Seed data (seed.py), BGSAVE, and copy dump.rdb out with docker cp.
  2. FLUSHALL (as default). DBSIZE is 0.
  3. Restore using the steps above, and check DBSIZE and HGETALL app:product:1001.
  4. Reproduce trap 2: delete appendonlydir, copy the backup in, and start the normal service. Check DBSIZE, the startup log, and the size of /data/dump.rdb afterwards.
  5. If the ha profile is running, reproduce the Sentinel trap, then repeat the restore correctly.

On this page