【发布时间】:2013-01-24 08:51:35
【问题描述】:
我使用scp 将我的redis 快照(dump.rdb 文件)传输到远程服务器。我需要在这个远程服务器上运行一个 redis 服务器并从dump.rdb 文件中恢复数据。我该怎么做?
【问题讨论】:
我使用scp 将我的redis 快照(dump.rdb 文件)传输到远程服务器。我需要在这个远程服务器上运行一个 redis 服务器并从dump.rdb 文件中恢复数据。我该怎么做?
【问题讨论】:
没有什么特别的事情要做。只需在新机器上安装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服务器了。
【讨论】:
appendonly 文件中的任何 appendonly 设置。任何时候你想恢复一个 redis dump.rdb 文件,你必须确保appendonly 在redis.conf 中设置为no。否则,它将完全清空您的 dump.rdb 文件并从点 0 重新开始,就好像什么都没发生一样。在小心的人之前!
对于appendonly 标志设置为no 的数据库,您可以执行以下操作:
dir 选项)。还要确保您的备份文件名与 dbfilename 配置选项匹配。另一方面,如果您需要将 rdb 文件恢复到仅附加数据库,您应该执行以下操作:
dir 选项)。还要确保您的备份文件名与 dbfilename 配置选项匹配。appendonly标志更改为no(否则redis启动时会忽略你的rdb文件)。redis-cli BGREWRITEAOF 以创建一个新的 appendonly 文件。appendonly标志恢复为yes。具体来说,这是来自 redis 配置文件 cmets 的相关文档:
# 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.
【讨论】:
chown redis:redis dump.rdb和chmod 644 dump.rdb
appendonly 标志设置为 yes 导致 RDB 文件被忽略和重写。
假设您运行 Redis 2.6 或更高版本,您的 Redis 快照文件名为 dump.rdb,并且它存在于目录 /home/user/dbs 中,以下命令可以解决问题:
redis-server --dbfilename dump.rdb --dir /home/user/dbs
【讨论】:
或者你可以:
service redis6379 stop
cp /path/to/dump-6379.rdb /var/lib/redis/dump-6379.rdb。给它正确的权限(user:group 应该是 redis:redis 和 mode 644)service redis6379 start
在将文件复制到正确位置之前停止 redis 服务器很重要,因为 Redis 在终止之前会保存快照,因此它将替换您的文件。
此外,您可能需要先备份现有的 dump.rdb 文件。
【讨论】:
在您的第二台服务器上启动 redis,如下所示:
$ > redis-server /path/to/my/redis/configuration/file/redis.conf
当 redis 启动时,它会找到你的 rdb 文件,因为它会寻找 name 和 文件路径 在您提供的 配置文件 (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 文件。
【讨论】:
我想在这里添加一个没有被提及的小细节,我不会使用配置文件,而是在命令行中指定所有内容。
如果在启动 redis-server 时同时指定了 mydump.rdb 和 appendonly.aof 文件,则将是 appendonly.aof 文件获胜,以便加载来自 appendonly.aof 的数据。例如:
redis-server --dbfilename mydump001.rdb --dir /data --appendonly yes
上述启动调用将使用/dir 位置来查找mydump001.rdb 或appendonly.aof 文件的存在。在这种情况下,redis-server 将从appendonly.aof 加载内容。如果appendonly.aof不存在,则会创建一个空的/data/appendonly.aof,redis-server将为空。
如果你想加载特定的转储文件,你可以这样做:
redis-server --dbfilename mydump001.rdb --dir /data
我添加了这个答案,因为不清楚哪个是哪个。在存在 2 个备份文件的情况下,这通常不会被提及。
【讨论】:
尝试设置 appendonly no。 在我的情况下,*.aof 文件为空(0 字节),必须设置 appendonly=no 然后使其加载 dump.rdb
【讨论】:
此解决方案适用于 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
如果这是一个集群,端口应该是master的端口。
【讨论】:
安装https://github.com/leonchen83/redis-rdb-cli
rmt -s ./your-dump.rdb -m redis://host:port -r
【讨论】: