如何从从另一台机器复制的快照(rdb 文件)中恢复 redis 数据



我使用 scp 将我的 redis 快照(dump.rdb文件)传输到远程服务器。我需要在此遥控器上运行 redis 服务器并从dump.rdb文件中恢复数据。我该怎么做?

对于 appendonly 标志设置为 no 的数据库,可以执行以下操作:

  1. 停止 Redis(因为 Redis 在退出时会覆盖当前的 rdb 文件)。
  2. 将备份 rdb 文件复制到 Redis 工作目录(这是 Redis 配置中的dir选项)。此外,请确保您的备份文件名与dbfilename配置选项匹配。
  3. 启动雷迪斯。

另一方面,如果您需要将 rdb 文件还原到 appendonly 数据库,则应执行以下操作:

  1. 停止 Redis(因为 Redis 在退出时会覆盖当前的 rdb 文件)。
  2. 将备份 rdb 文件复制到 Redis 工作目录(这是 Redis 配置中的dir选项)。此外,请确保您的备份文件名与dbfilename配置选项匹配。
  3. 将 Redis 配置appendonly标志更改为 no(否则 Redis 将在启动时忽略您的 rdb 文件)。
  4. 启动雷迪斯。
  5. 运行 redis-cli BGREWRITEAOF 以创建新的仅追加文件。
  6. 将 Redis 配置appendonly标志恢复为 yes

具体来说,这是 Redis 配置文件注释中的相关文档:

# Note that you can have both the async dumps and the append only file if you                                                     
# like (you have to comment the "save" statements above to disable the dumps).                                                    
# >> Still if appendonly mode is enabled Redis will load the data from the                                                          
# >> log file at startup ignoring the dump.rdb file. 

没有什么具体的事情要做。只需在新机器上安装 redis 服务器,然后编辑配置文件即可。只需更改以下参数以指向刚刚复制的转储文件的位置。

# The filename where to dump the DB
dbfilename mydump.rdb
# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
# 
# Also the Append Only File will be created inside this directory.
# 
# Note that you must specify a directory here, not a file name.
dir /data/mydirectory/

最后,可以正常启动 redis 服务器。

假设您运行 Redis 2.6 或更高版本,您的 Redis 快照文件名为 dump.rdb ,并且它存在于目录 /home/user/dbs 中,以下命令就可以解决问题:

redis-server --dbfilename dump.rdb --dir /home/user/dbs

官方文档中的相关部分:通过命令行传递参数

或者

你可以:

  1. 停止您的 Redis 服务器/实例,例如service redis6379 stop
  2. 将 dump.rdb 文件复制到正确的位置,例如 cp /path/to/dump-6379.rdb /var/lib/redis/dump-6379.rdb .赋予它正确的权限(用户:组应该是 redis:Redis 和模式 644)
  3. 启动您的 Redis 服务器/实例,例如service redis6379 start

在将文件复制到正确位置之前停止 redis 服务器非常重要,因为 Redis 会在终止之前保存快照,因此它将替换您的文件。

此外,您可能希望先备份现有的dump.rdb文件。

我想

在这里添加一个没有提到的小细节,我不将使用配置文件,而是在命令行中指定所有内容。

当 mydump.rdb 和 appendonly.aof 文件在启动 redis-server 时都指定时,它将是appendonly.aof文件获胜,以便加载 appendonly.aof 中的数据。例如:

redis-server --dbfilename mydump001.rdb --dir /data --appendonly yes

上面的启动调用将使用/dir位置来查找是否存在mydump001.rdbappendonly.aof文件。在这种情况下,redis-server将从appendonly.aof加载内容。如果appendonly.aof不存在,它将创建一个空/data/appendonly.aof,并且 redis 服务器将为空。

如果要加载特定的转储文件,可以执行以下操作:

redis-server --dbfilename mydump001.rdb --dir /data

我添加了这个答案,因为哪个是哪个并不明显。在存在 2 个备份文件的情况下,这通常不会被提及。

第二台服务器上启动 redis,如下所示:

$ > redis-server /path/to/my/redis/configuration/file/redis.conf

当 Redis 启动时,它会找到您的 RDB 文件,因为它会查找名称以及您在以下情况下提供的配置文件redis.conf) 中的文件路径启动 Redis 服务器,如上所述。

要提供文件名和路径,只需在 redis.conf 文件模板中编辑两行(在 Redis 源的根目录中提供。将修订后的版本另存为 redis.conf 在启动服务器时提供的目录位置。

您可以在源顶级目录的 redis.conf 模板中找到所需的设置,位于第 127 行和第 137 行(redis 版本 2.6.9)。

# The filename where to dump the DB
dbfilename dump.rdb
# The working directory
dir ./

如您所见,两种设置都提供了默认值

因此,只需更改这两行中的第一行(127)即可识别您的RDB文件在第二个 (137) 中,将默认的"./"替换为实际文件路径为您的快照 RDB 文件;保存 redis.conf 和你的更改,然后开始 Redis 传递这个新的 conf 文件。

此解决方案适用于 redis-cluster,但也适用于 redis。

安装此依赖项 https://github.com/sripathikrishnan/redis-rdb-tools

pip install rdbtools python-lzf

之后执行这个

rdb -c protocol /path/to/dump.rdb | redis-cli -h host -p port --pipe

如果这是集群,则端口应为主端口。

尝试设置附录否。在我的情况下,*.aof 文件为空(0 字节),必须设置 appendonly=no 然后让它加载 dump.rdb

安装 https://github.com/leonchen83/redis-rdb-cli

rmt -s ./your-dump.rdb -m redis://host:port -r

最新更新