redis备份与恢复

2023-11-16 21:22:34 浏览数 (2)

  • 发表评论
  • 4,383
  • A

所属分类:数据库

说明:默认rdb方式保存,redis支持主从和哨兵等,但是在某些情况下我们会单机跑,所以有时候我们就会需要设计到备份恢复

环境:原始redis:192.168.1.200

         新redis:192.168.1.201

原始redis:192.168.1.200,查看导出备份地址以及备份

Shell

[root@redis-master ~]#redis-cli 127.0.0.1:6379> auth 123456 #认证 OK 127.0.0.1:6379> save #保存还未写入本地文件的redis数据 OK (1.06s) 127.0.0.1:6379> CONFIG GET dir #查看备份文件路径,可以看到是root目录 1) "dir" 2) "/root" 127.0.0.1:6379> exit [root@redis-master ~]# ll -h /root/ total 24M -rw-r--r-- 1 root root 24M Sep 20 15:11 dump.rdb 这就是备份下来的文件

1234567891011121314

[root@redis-master ~]#redis-cli 127.0.0.1:6379> auth 123456     #认证OK127.0.0.1:6379> save                #保存还未写入本地文件的redis数据OK(1.06s)127.0.0.1:6379> CONFIG GET dir #查看备份文件路径,可以看到是root目录1) "dir"2) "/root"127.0.0.1:6379> exit[root@redis-master ~]# ll -h /root/total 24M-rw-r--r-- 1 root root  24M Sep 20 15:11 dump.rdb    这就是备份下来的文件

新redis:192.168.1.201搭建和恢复:

Shell

[root@redis-backup ~]cd /usr/local [root@redis-backup local]wget http://download.redis.io/releases/redis-4.0.1.tar.gz [root@redis-backup local]tar xzf redis-4.0.1.tar.gz [root@redis-backup local]cd redis-4.0.1 [root@redis-backup redis-4.0.1]make [root@redis-backup redis-4.0.1]./src/redis-server & ##查看rdb路径 [root@redis-backup redis-4.0.1]./src/redis-cli 127.0.0.1:6379> CONFIG GET dir 1) "dir" 2) "/usr/local/redis-4.0.1" ###停止redis,将远程的rdb文件复制到/usr/local/redis-4.0.1 [root@redis-backup redis-4.0.1]killall redis-server [root@redis-backup redis-4.0.1]scp root@192.168.1.200:/root/dump.rdb /usr/local/redis-4.0.1 #启动redis,验证数据 [root@redis-backup redis-4.0.1]./src/redis-server & [root@redis-backup redis-4.0.1]./src/redis-cli 127.0.0.1:6379> keys * ...... 36300) "exp:userId:login:103829" 36301) "exp:userId:login:92291" 36302) "exp:userId:login:1328" 36303) "exp:userId:login:26009" 36304) "exp:userId:login:102208" 36305) "exp:userId:login:107040" 36306) "exp:userId:login:106752" 36307) "exp:userId:login:34952" 36308) "exp:userId:login:33017" 36309) "exp:userId:login:39154" 36310) "exp:userId:login:61283" 36311) "exp:userId:login:102089" 36312) "exp:userId:login:24389" 36313) "exp:userId:login:34239" 36314) "exp:userId:login:88830" 36315) "exp:userId:login:15970" 36316) "exp:userId:login:105425" 36317) "exp:userId:login:52535" 36318) "exp:userId:login:90636" 36319) "exp:userId:login:73731" (0.81s)

123456789101112131415161718192021222324252627282930313233343536373839404142

[root@redis-backup ~]cd /usr/local[root@redis-backup local]wget http://download.redis.io/releases/redis-4.0.1.tar.gz[root@redis-backup local]tar xzf redis-4.0.1.tar.gz[root@redis-backup local]cd redis-4.0.1[root@redis-backup redis-4.0.1]make[root@redis-backup redis-4.0.1]./src/redis-server &##查看rdb路径[root@redis-backup redis-4.0.1]./src/redis-cli127.0.0.1:6379>  CONFIG GET dir1) "dir"2) "/usr/local/redis-4.0.1"###停止redis,将远程的rdb文件复制到/usr/local/redis-4.0.1[root@redis-backup redis-4.0.1]killall redis-server[root@redis-backup redis-4.0.1]scp root@192.168.1.200:/root/dump.rdb /usr/local/redis-4.0.1#启动redis,验证数据[root@redis-backup redis-4.0.1]./src/redis-server &[root@redis-backup redis-4.0.1]./src/redis-cli127.0.0.1:6379> keys *......36300) "exp:userId:login:103829"36301) "exp:userId:login:92291"36302) "exp:userId:login:1328"36303) "exp:userId:login:26009"36304) "exp:userId:login:102208"36305) "exp:userId:login:107040"36306) "exp:userId:login:106752"36307) "exp:userId:login:34952"36308) "exp:userId:login:33017"36309) "exp:userId:login:39154"36310) "exp:userId:login:61283"36311) "exp:userId:login:102089"36312) "exp:userId:login:24389"36313) "exp:userId:login:34239"36314) "exp:userId:login:88830"36315) "exp:userId:login:15970"36316) "exp:userId:login:105425"36317) "exp:userId:login:52535"36318) "exp:userId:login:90636"36319) "exp:userId:login:73731"(0.81s)

到此  恢复完成

0 人点赞