您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
Redis 数据备份与恢复
发布时间:2021-11-14 23:14:39编辑:雪饮阅读()
Redis SAVE 命令用于创建当前数据库的备份。
实例:
127.0.0.1:6379> save
1541:M 14 Nov 2021 09:48:15.460 * DB saved on disk
OK
菜鸟教程:该命令将在 redis 安装目录中创建dump.rdb文件。
但是实际上并不是这样的:
首先我看到我这里有dump.rdb文件:
[root@localhost ~]# ls /usr/local/redis-6.2.5/
00-RELEASENOTES CONDUCT COPYING dump.rdb Makefile README.md runtest runtest-moduleapi sentinel.conf tests utils
BUGS CONTRIBUTING deps INSTALL MANIFESTO redis.conf runtest-cluster runtest-sentinel src TLS.md
这是在我没有save之前的。
然后我删除了它:
[root@localhost ~]# rm -rf /usr/local/redis-6.2.5/dump.rdb
[root@localhost ~]# ls /usr/local/redis-6.2.5/
00-RELEASENOTES CONDUCT COPYING INSTALL MANIFESTO redis.conf runtest-cluster runtest-sentinel src TLS.md
BUGS CONTRIBUTING deps Makefile README.md runtest runtest-moduleapi sentinel.conf tests utils
此时我执行save命令后发现该目录中并没有产生dump.rdb文件,于是我看到我的redis.conf配置中对于dbfilename的配置:
# Enables or disables full sanitation checks for ziplist and listpack etc when
# loading an RDB or RESTORE payload. This reduces the chances of a assertion or
# crash later on while processing commands.
# Options:
# no - Never perform full sanitation
# yes - Always perform full sanitation
# clients - Perform full sanitation only for user connections.
# Excludes: RDB files, RESTORE commands received from the master
# connection, and client connections which have the
# skip-sanitize-payload ACL flag.
# The default should be 'clients' but since it currently affects cluster
# resharding via MIGRATE, it is temporarily set to 'no' by default.
#
# sanitize-dump-payload no
# The filename where to dump the DB
dbfilename dump.rdb
确认是没有修改过默认的配置,于是我整个根目录搜索到了其存储于/root/dump.rdb(可能因为我是root用户操作的原因),然后我删除了它,我再次执行save后就又产生了/root/dump.rdb:
[root@localhost ~]# find / -name "*dump.rdb*"
/root/dump.rdb
[root@localhost ~]# rm -rf /root/dump.rdb
[root@localhost ~]# find / -name "*dump.rdb*"
/root/dump.rdb
那么如何指定dump.rdb备份到指定目录呢?
这里假如我要让其备份到/usr/local/redis-6.2.5/dump.rdb路径,则修改配置文件redis.cnf:
# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
#dir ./
dir /usr/local/redis-6.2.5/
将原来的"dir ./"注释掉,换成如"dir /usr/local/redis-6.2.5/"
然后干掉之前的redis-server进程,重新开启redis-server并显示指定该redis.cnf为配置文件(为谨慎起见):
[root@localhost ~]# /usr/local/redis-6.2.5/src/redis-server /usr/local/redis-6.2.5/redis.conf &
然后再次从redis-cli中执行save后就可以看到我们配置的这个存储dump.rdb目录中就顺利的产生了dump.rdb文件了:
[root@localhost ~]# ls /usr/local/redis-6.2.5/
00-RELEASENOTES CONDUCT COPYING dump.rdb Makefile README.md runtest runtest-moduleapi sentinel.conf tests utils
BUGS CONTRIBUTING deps INSTALL MANIFESTO redis.conf runtest-cluster runtest-sentinel src TLS.md
关键字词:redis,备份,恢复
下一篇:redis 恢复数据