Redis Backup and Recovery Using RDB, AOF, and Master‑Slave Replication
This article demonstrates how to back up and restore Redis data using semi‑persistent RDB snapshots, full‑persistent AOF logging, and master‑slave replication, including command‑line examples for setting keys, checking replication status, saving snapshots, promoting a slave to master, and verifying data integrity.
All Redis data resides in memory, and backup can be performed periodically by asynchronously writing snapshots to disk (the semi‑persistent RDB mode). If every write operation is appended to an AOF file, it becomes full persistence. Redis backup and recovery can also be achieved through master‑slave replication.
Connect to the master node: ./redis-cli -h 192.168.210.85 -p 6379 Check replication information: info replication Output:
role:master
connected_slaves:0
master_replid:145346920da531a82dd62c0b7130c9ea48e8101f
master_replid2:982b93c14b73dda8088029b664f377361fa5e7b9
master_repl_offset:503252
second_repl_offset:497019
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:338764
repl_backlog_histlen:164489Set several keys and retrieve them:
set k1 v1
OK
set k2 v2
OK
set k3 v3
OK
set k4 v4
OK
get k1
"v1"
get k2
"v2"
get k3
"v3"
get k4
"v4"Force a snapshot to be saved:
SAVE
OKSwitch to the slave node (IP 192.168.210.177) and verify that the data has been replicated:
get k1
"v1"
get k2
"v2"
get k3
"v3"
get k4
"v4"Query the Redis configuration to see the backup directory:
CONFIG GET dir
1) "dir"
2) "/data/redis"The above command shows that the Redis backup directory is /data/redis.
Shut down the original master: shutdown Attempting to reconnect fails:
Could not connect to Redis at 192.168.210.85:6379: Connection refusedAfter restarting, promote the former slave (IP 192.168.210.177) to become the new master.
Verify the data on the new master:
DBSIZE
(integer) 4
keys *
1) "k4"
2) "k1"
3) "k2"
4) "k3"Related articles:
1. Redis Sentinel Setup
2. Redis Installation and Master‑Slave Replication
3. Detailed Summary of Redis Configuration Files
4. Common Redis Data Types
5. Three‑Master Three‑Slave Redis Cluster
6. Join Our Technical Community
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Practical DevOps Architecture
Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.
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.
