Skip to content
Redis CLI

RDB / AOF Persistence & Backup

Trigger snapshots, manage AOF rewrite, and capture a safe backup.

By EZ4Code Team
persistencerdbaofbackup

Code

# Manual RDB snapshot (foreground, blocks briefly)
127.0.0.1:6379> BGSAVE
Background saving started
127.0.0.1:6379> LASTSAVE
(integer) 1748200000

# Inspect persistence status
127.0.0.1:6379> INFO persistence
# rdb_bgsave_in_progress:0
# rdb_last_save_time:1748200000
# aof_enabled:1
# aof_rewrite_in_progress:0

# AOF (append-only file) controls
127.0.0.1:6379> CONFIG SET appendonly yes
127.0.0.1:6379> BGREWRITEAOF           # compact the AOF
127.0.0.1:6379> CONFIG SET appendfsync everysec   # always|everysec|no

# Online backup without stopping Redis: BGSAVE then copy the dump file
$ redis-cli BGSAVE
$ while [ "$(redis-cli INFO persistence | grep rdb_bgsave_in_progress | tr -d '
')" != "rdb_bgsave_in_progress:0" ]; do sleep 1; done
$ cp /var/lib/redis/dump.rdb /backup/dump-$(date +%F).rdb

# DEBUG SLEEP (testing only) — blocks server for N seconds
127.0.0.1:6379> DEBUG SLEEP 2

# Shutdown options
127.0.0.1:6379> SHUTDOWN SAVE       # save then quit
127.0.0.1:6379> SHUTDOWN NOSAVE     # quit without saving

Explanation

Redis persists via RDB (point-in-time snapshots) and/or AOF (append log of every write). BGSAVE forks and writes dump.rdb in the background; LASTSAVE gives the last snapshot timestamp. BGREWRITEAOF compacts the AOF (replays commands into a minimal form). appendfsync controls durability: always (fsync every write, slow, safest), everysec (default — at most 1s of data loss), no (let OS decide). For safe online backups: BGSAVE, poll rdb_bgsave_in_progress until 0, then copy dump.rdb away — no downtime. SHUTDOWN SAVE persists before exit; SHUTDOWN NOSAVE drops unsaved data.

More Redis CLI Snippets