redis数据备份和恢复无法生效

redis提供了一个save来备份当前数据库信息

[lihui@localhost ~]$ /usr/local/bin/redis-cli 
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> lpush nums 1 2 3
(integer) 3
127.0.0.1:6379> set lihui 30
OK
127.0.0.1:6379> hset lilei sex male
(integer) 1
127.0.0.1:6379> keys *
1) "lilei"
2) "nums"
3) "lihui"
127.0.0.1:6379> 

此时数据库里还是有一大堆东西的,完成了插入之后,一定要重启redis,下面说的文件才会update

[lihui@localhost ~]$ sudo service redis_6379 restart
[sudo] password for lihui: 
Stopping ...
Waiting for Redis to shutdown ...
Redis stopped
Starting Redis server...

直接命令SAVE,然后将备份文件dump.rdb放到其它地方

127.0.0.1:6379> SAVE
OK

 

[lihui@localhost ~]$ su Password: [root@localhost lihui]# locate dump.rdb /var/lib/redis/6379/dump.rdb [root@localhost lihui]# ll /var/lib/redis/6379/dump.rdb -rw-r--r--. 1 root root 83 Feb 23 22:43 /var/lib/redis/6379/dump.rdb [root@localhost lihui]# cp /var/lib/redis/6379/dump.rdb /home/lihui/ cp: overwrite ?.home/lihui/dump.rdb?. y

也就是说此刻原始3个key的数据库备份放在了/home/lihui下面,接着删掉一个key

[lihui@localhost ~]$ /usr/local/bin/redis-cli 
127.0.0.1:6379> del lilei
(integer) 1
127.0.0.1:6379> keys *
1) "lihui"
2) "nums"
127.0.0.1:6379> 
[lihui@localhost ~]$ sudo service redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...

[root@localhost lihui]# ll /var/lib/redis/6379/dump.rdb 
-rw-r--r--. 1 root root 53 Feb 23 22:47 /var/lib/redis/6379/dump.rdb

可见dump.rdb的确是update了一下,字节变小了;此时,假如想要恢复开始未del之前的3个key的数据库,只需要将备份在/home/lihui下的文件copy到redis的安装目录下

查找redis的安装目录

127.0.0.1:6379> config get dir
1) "dir"
2) "/var/lib/redis/6379"

于是直接copy

[root@localhost lihui]# cp /home/lihui/dump.rdb /var/lib/redis/6379/
cp: overwrite ?.var/lib/redis/6379/dump.rdb?. y
[root@localhost lihui]# ll /var/lib/redis/6379/dump.rdb 
-rw-r--r--. 1 root root 83 Feb 23 22:56 /var/lib/redis/6379/dump.rdb

此刻清楚地看到文件大小是83字节,可惜无论重启redis否,依旧是del之后的数据库了,而且重启了redis后,文件大小又变成53字节了

[lihui@localhost ~]$ /usr/local/bin/redis-cli 
127.0.0.1:6379> keys *
1) "nums"
2) "lihui"
127.0.0.1:6379> 
[lihui@localhost ~]$ sudo service redis_6379 restart
[sudo] password for lihui: 
Stopping ...
Waiting for Redis to shutdown ...
Redis stopped
Starting Redis server...
[lihui@localhost ~]$ /usr/local/bin/redis-cli 
127.0.0.1:6379> keys *
1) "nums"
2) "lihui"

[root@localhost lihui]# ll /var/lib/redis/6379/dump.rdb 
-rw-r--r--. 1 root root 53 Feb 23 22:56 /var/lib/redis/6379/dump.rdb

这就很奇怪了,也就是备份根本没完全生效,或者是存在某种缓存机制,没有实时刷新到磁盘~!

发表回复