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

时间:2013-01-24 08:51:35

标签: redis snapshot

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

7 个答案:

答案 0 :(得分:54)

没有具体的事情要做。只需在新机器上安装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服务器可以正常方式启动。

答案 1 :(得分:44)

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

  1. 停止redis(因为redis在退出时会覆盖当前的rdb文件)。
  2. 将备份rdb文件复制到redis工作目录(这是redis配置中的dir选项)。另外,请确保您的备份文件名与dbfilename配置选项匹配。
  3. 启动redis。
  4. 另一方面,如果您需要将rdb文件还原到仅附加数据库,则应该执行以下操作:

    1. 停止redis(因为redis在退出时会覆盖当前的rdb文件)。
    2. 将备份rdb文件复制到redis工作目录(这是redis配置中的dir选项)。另外,请确保您的备份文件名与dbfilename配置选项匹配。
    3. 将redis config appendonly标志更改为no(否则redis会在启动时忽略您的rdb文件)。
    4. 启动redis。
    5. 运行redis-cli BGREWRITEAOF以创建新的附加文件。
    6. 将redis config appendonly标记恢复为yes
    7. 具体来说,这是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 append only mode is enabled Redis will load the data from the                                                          
      # >> log file at startup ignoring the dump.rdb file. 
      

答案 2 :(得分:11)

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

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

官方文件中的相关部分:Passing arguments via the command line

答案 3 :(得分:8)

或者你可以:

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

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

答案 4 :(得分:5)

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

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

当redis启动时,它会找到你的 rdb 文件,因为它会查找名称 您提供的 配置文件 redis.conf )中的文件路径 启动redis服务器,如上所述。

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

您可以在源顶级目录的 redis.conf 模板中找到所需的设置,行 127 和< em> 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文件。

答案 5 :(得分:3)

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

答案 6 :(得分:1)

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

如果在启动$questions = App\Question::with(['answer' => function ($query) { $query->where('answers.user_id', auth()->user()->id); // Other Conditions }]->get(); 时指定了mydump.rdb和appendonly.aof文件,则会获得redis-server文件,以便加载appendonly.aof中的数据。例如:

appendonly.aof

上述开始调用将使用redis-server --dbfilename mydump001.rdb --dir /data --appendonly yes 位置来查找/dirmydump001.rdb个文件的存在。在这种情况下,appendonly.aof将从redis-server加载内容。如果appendonly.aof不存在,则会创建一个空的appendonly.aof,并且redis-server将为空。

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

/data/appendonly.aof

我添加了这个答案因为它不明显是哪个。在有2个备份文件的情况下,这通常没有提到。