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

我使用scp将我的redis快照( dump.rdb文件)传输到远程服务器。 我需要在这个远程服务器上运行一个redis服务器,然后从dump.rdb文件中恢复数据。 我怎样才能做到这一点?

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

 # 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服务器可以正常启动。

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

  1. 停止redis(因为redis在退出时会覆盖当前的rdb文件)。
  2. 将备份rdb文件复制到redis工作目录(这是您的redisconfiguration中的dir选项)。 还要确保你的备份文件名与dbfilename config选项匹配。
  3. 开始redis。

另一方面,如果您需要将rdb文件还原到仅附加数据库,则应该按照以下步骤执行操作:

  1. 停止redis(因为redis在退出时会覆盖当前的rdb文件)。
  2. 将备份rdb文件复制到redis工作目录(这是redisconfiguration中的dir选项)。 还要确保你的备份文件名与dbfilename config选项匹配。
  3. 将redis config appendonly标志更改为no (否则redis将在启动时忽略您的rdb文件)。
  4. 开始redis。
  5. 运行redis-cli BGREWRITEAOF创build一个新的附件文件。
  6. 将redis config appendonly标志恢复为yes

具体来说,这是来自redisconfiguration文件注释的文档的相关位:

 # 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 append only mode is enabled Redis will load the data from the # >> log file at startup ignoring the dump.rdb file. 

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

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

当redis启动时,它会find你的rdb文件,因为它将在启动redis服务器时提供的configuration文件redis.conf )中查找名称文件path ,如上所述。

要提供文件名和path,只需编辑redis.conf文件模板中的两行 (在redis源的根目录中提供)。将修改后的版本保存为redis.conf,并将其保存在启动服务器时提供的目录位置中。

您可以在源代码顶级目录的redis.conf模板中的127137行 (redis版本2.6.9)find所需的设置。

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

如您所见,为这两个设置提供了默认设置

所以只需更改这两行中的第一行(127)以确定您的rdb文件,并在第二行(137)中将快照rdb文件的实际文件pathreplace为默认的“./” 保存你的修改redis.conf ,并开始redis传递这个新的conf文件。

或者你可以:

  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在终止之前会保存一个快照,所以它会replace您的文件。

另外,您可能需要先备份现有的dump.rdb文件。

假设你运行的是Redis 2.6或更高版本,你的Redis快照文件名是dump.rdb ,它存在于/home/user/dbs ,下面的命令可以做到这一点:

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

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

尝试设置appendonly没有。 在我的情况下,* .aof文件是空的(0字节),必须设置appendonly = no然后使它加载dump.rdb