您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
redis 恢复数据
发布时间:2021-11-15 23:06:54编辑:雪饮阅读()
如果需要恢复数据,只需将备份文件 (dump.rdb) 移动到 redis 安装目录并启动服务即可。获取 redis 目录可以使用 CONFIG 命令,
首先我当前有如下key
127.0.0.1:6379> keys *
1) "sicily"
2) "user2_user1_queue"
3) "user1_user2_queue"
那么当前redis的dump.rdb存储路径在这里:
127.0.0.1:6379> config get dir
1) "dir"
2) "/root"
即:
[root@localhost ~]# ls /root
anaconda-ks.cfg dump.rdb harbor-offline-installer-v1.4.0.tgz
那么我拷贝出一个副本:
[root@localhost ~]# ls /root
anaconda-ks.cfg dump.rdb harbor-offline-installer-v1.4.0.tgz
[root@localhost ~]# cp dump.rdb dump.rdb.bk
那么此时我删掉库中当前所存储的这些key:
127.0.0.1:6379> save
1575:M 15 Nov 2021 09:56:36.163 * DB saved on disk
OK
127.0.0.1:6379> keys *
(empty array)
然后我重启redis服务后发现key是持久化的被删除了的:
[root@localhost ~]# kill -9 1575
[root@localhost ~]# /usr/local/redis-6.2.5/src/redis-server &
[2] 3709
[1] Killed /usr/local/redis-6.2.5/src/redis-server
[root@localhost ~]# 3709:C 15 Nov 2021 09:57:31.374 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3709:C 15 Nov 2021 09:57:31.374 # Redis version=6.2.5, bits=64, commit=00000000, modified=0, pid=3709, just started
3709:C 15 Nov 2021 09:57:31.374 # Warning: no config file specified, using the default config. In order to specify a config file use /usr/local/redis-6.2.5/src/redis-server /path/to/redis.conf
3709:M 15 Nov 2021 09:57:31.375 * Increased maximum number of open files to 10032 (it was originally set to 1024).
3709:M 15 Nov 2021 09:57:31.375 * monotonic clock: POSIX clock_gettime
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.2.5 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 3709
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | https://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
3709:M 15 Nov 2021 09:57:31.375 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
3709:M 15 Nov 2021 09:57:31.375 # Server initialized
3709:M 15 Nov 2021 09:57:31.375 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
3709:M 15 Nov 2021 09:57:31.375 * Loading RDB produced by version 6.2.5
3709:M 15 Nov 2021 09:57:31.375 * RDB age 55 seconds
3709:M 15 Nov 2021 09:57:31.375 * RDB memory usage when created 0.89 Mb
3709:M 15 Nov 2021 09:57:31.375 * DB loaded from disk: 0.000 seconds
3709:M 15 Nov 2021 09:57:31.375 * Ready to accept connections
[root@localhost ~]# /usr/local/redis-6.2.5/src/redis-cli
127.0.0.1:6379> keys *
(empty array)
那么此时我将之前的备份覆盖掉当前dump.rdb:
[root@localhost ~]# rm -rf dump.rdb
[root@localhost ~]# cp dump.rdb.bk dump.rdb
然后呢我再次重启redis服务就可以发现又恢复到一开始我们第一次干掉的哪些key了:
[root@localhost ~]# kill -9 3709
[root@localhost ~]# /usr/local/redis-6.2.5/src/redis-server &
[3] 4585
[2] Killed /usr/local/redis-6.2.5/src/redis-server
[root@localhost ~]# 4585:C 15 Nov 2021 09:59:55.775 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4585:C 15 Nov 2021 09:59:55.775 # Redis version=6.2.5, bits=64, commit=00000000, modified=0, pid=4585, just started
4585:C 15 Nov 2021 09:59:55.775 # Warning: no config file specified, using the default config. In order to specify a config file use /usr/local/redis-6.2.5/src/redis-server /path/to/redis.conf
4585:M 15 Nov 2021 09:59:55.775 * Increased maximum number of open files to 10032 (it was originally set to 1024).
4585:M 15 Nov 2021 09:59:55.775 * monotonic clock: POSIX clock_gettime
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.2.5 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 4585
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | https://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
4585:M 15 Nov 2021 09:59:55.776 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
4585:M 15 Nov 2021 09:59:55.776 # Server initialized
4585:M 15 Nov 2021 09:59:55.776 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
4585:M 15 Nov 2021 09:59:55.776 * Loading RDB produced by version 6.2.5
4585:M 15 Nov 2021 09:59:55.776 * RDB age 87100 seconds
4585:M 15 Nov 2021 09:59:55.776 * RDB memory usage when created 0.90 Mb
4585:M 15 Nov 2021 09:59:55.776 * DB loaded from disk: 0.000 seconds
4585:M 15 Nov 2021 09:59:55.776 * Ready to accept connections
[root@localhost ~]# /usr/local/redis-6.2.5/src/redis-cli
127.0.0.1:6379> keys *
1) "sicily"
2) "user1_user2_queue"
3) "user2_user1_queue"
题外:
下面是个人的一些理解
上面不是说了用config get dir可以获取那个dump.rdb吗?那么上面获取的是/root这个目录,实际上这个dir是根据redis的配置文件来的,一般没有显式指定配置文件都是"./"即当前路径,这里root账号当前目录就是/root。
你比如说如果我redis-server指定了配置文件,则这个dir路径也就是指定配置文件中所配置的dir:
[root@localhost ~]# /usr/local/redis-6.2.5/src/redis-server /usr/local/redis-6.2.5/redis.conf &
[4] 5251
[3] Killed /usr/local/redis-6.2.5/src/redis-server
[root@localhost ~]# 5251:C 15 Nov 2021 10:01:44.712 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5251:C 15 Nov 2021 10:01:44.712 # Redis version=6.2.5, bits=64, commit=00000000, modified=0, pid=5251, just started
5251:C 15 Nov 2021 10:01:44.712 # Configuration loaded
5251:M 15 Nov 2021 10:01:44.712 * Increased maximum number of open files to 10032 (it was originally set to 1024).
5251:M 15 Nov 2021 10:01:44.712 * monotonic clock: POSIX clock_gettime
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.2.5 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 5251
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | https://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
5251:M 15 Nov 2021 10:01:44.713 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
5251:M 15 Nov 2021 10:01:44.713 # Server initialized
5251:M 15 Nov 2021 10:01:44.713 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
5251:M 15 Nov 2021 10:01:44.725 * Loading RDB produced by version 6.2.5
5251:M 15 Nov 2021 10:01:44.725 * RDB age 85562 seconds
5251:M 15 Nov 2021 10:01:44.725 * RDB memory usage when created 0.84 Mb
5251:M 15 Nov 2021 10:01:44.725 * DB loaded from disk: 0.012 seconds
5251:M 15 Nov 2021 10:01:44.725 * Ready to accept connections
[root@localhost ~]# /usr/local/redis-6.2.5/src/redis-cli
127.0.0.1:6379> keys *
(empty array)
127.0.0.1:6379> config get dir
1) "dir"
2) "/usr/local/redis-6.2.5"
不妨可以看看我这个配置文件中的dir的配置如:
[root@localhost ~]# cat /usr/local/redis-6.2.5/redis.conf | grep dir
# line as value of a configuration directive, you'd better put includes
# it will abort. It is possible to use multiple loadmodule directives.
# By default, if no "bind" configuration directive is specified, Redis listens
# the "bind" configuration directive, followed by one or more IP addresses.
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# following bind directive, that will force Redis to listen only on the
# "bind" directive.
# are explicitly listed using the "bind" directive.
# directive can be used to define TLS-listening ports. To enable TLS on the
# connections. To do that, use the following directives:
# Configure a CA certificate(s) bundle or directory to authenticate TLS/SSL
# tls-ca-cert-dir /etc/ssl/certs
# Use the following directive to enable TLS on replication links.
# TLS for the bus protocol, use the following directive:
# reconnections by clients that support it. Use the following directive to disable
# 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/
# directive below) it is possible to tell the replica to authenticate before
# 2) Diskless: The Redis master creates a new process that directly writes the
# Replica can load the RDB it reads from the replication link directly from the
# However, parsing the RDB file directly from the socket may mean that we have
# the data directly from the socket. note that this requires
# This directive clears the "nopass" flag (see later).
# password will work against this user. If this directive is
# directive will clear this condition.
# acl-pubsub-default configuration directive, which accepts one of these values:
# the directive's default of 'allchannels' will be changed to 'resetchannels' in
# configuration directive.
# was called, using the following configuration directives.
# directive:
# commands. When neither flag is passed, this directive will be used to determine
# enable I/O threads use the following configuration directive:
# protocol parsing using the following configuration directive, by setting
# NOTE 1: This configuration directive cannot be changed at runtime via
# When oom-score-adj is used, this directive controls the specific values used
# latency-monitor-threshold configuration directive. When its value is set
# threshold. These thresholds can be configured using the following directives.
# The syntax of every client-output-buffer-limit directive is the following:
# possible to this via Redis configuration directly, both in Linux and FreeBSD.
关键字词:redis,恢复数据
上一篇:Redis 数据备份与恢复
下一篇:redis Bgsave