如何使用redis的`DUMP`和`RESTORE`(离线)? [英] How to use redis' `DUMP` and `RESTORE` (offline)?

查看:37
本文介绍了如何使用redis的`DUMP`和`RESTORE`(离线)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了 redis 的 DUMP 命令,重定向到文件(或管道),但是 RESTORE 报这个错误:

I tried redis's DUMP command, redirect to file (or pipe), but RESTORE report this error:

$ redis-cli dump test > /tmp/test.dump
$ cat /tmp/test.dump | redis-cli -x restore test1 0
(error) ERR DUMP payload version or checksum are wrong
$ redis-cli dump test | redis-cli -x restore test1 0
(error) ERR DUMP payload version or checksum are wrong

我知道 MIGRATE 可以在线执行此操作,但是 MIGRATE 也会从原始服务器中删除该密钥,我不希望我的 redis 暴露在公共互联网上.

I am aware that MIGRATE can do this online, but MIGRATE also delete that key from original server, and I don't want my redis expose to public internet.

有一些第三方选项,例如redis-rdb-tools,但毕竟,DUMPRESTORE 究竟是如何工作的?

There are some third-party options, redis-rdb-tools for example, but after all, how exactly do DUMP and RESTORE work?

推荐答案

dump/restore 命令并不是真正设计为从命令行使用的,因为序列化格式是二进制的(它与 RDB 转储使用的格式相同)).这很不方便,因为 shell 倾向于解释这些字符(即使使用可打印"格式).

The dump/restore commands are not really designed to be used from the command line, because the serialization format is binary (it is the same one used for RDB dumps). It makes it inconvenient because the shell tends to interpret those characters (even when the "printable" format is used).

这是可打印"格式:

$ redis-cli lpush test 1 2 3 4 5
(integer) 5
$ redis-cli dump test
"
x15x15x00x00x00x12x00x00x00x05x00x00xf6x02xf5x02xf4x02xf3x02xf2xffx06x00x1cx8axdax0e}xcbxe1."

可打印"格式不能用作真正需要实际数据的 -x 选项的输入.这是redis-cli的误导行为.

The "printable" format cannot be used as input for the -x option which really expects the actual data. This is a misleading behavior of redis-cli.

但是,有一种简单的方法可以获取原始格式:

However, there is an easy way to get the raw format:

$ redis-cli --raw dump test | hexdump -C
00000000  0a 15 15 00 00 00 12 00  00 00 05 00 00 f6 02 f5  |................|
00000010  02 f4 02 f3 02 f2 ff 06  00 1c 8a da 0e 7d cb e1  |.............}..|
00000020  2e 0a                                             |..|

现在,无法在 -x 还原中直接通过管道传输 --raw 转储的结果,因为最后一个字符是错误的.比较 --raw 和可打印转储的输出.您会注意到 --raw 选项在末尾添加了一个额外的 .原始选项不是 100% 原始 ;-)

Now, it is not possible to directly pipe the result of a --raw dump in a -x restore, because the last character is wrong. Compare the output of the --raw and printable dump. You will notice the --raw option adds an extra at the end. The raw option is not 100% raw ;-)

在数据可以被 -x 选项处理之前,需要删除这个额外的字符.最后,在还原中通过管道传输转储输出的正确命令(在 GNU/Linux 系统上)是:

This extra character needs to be removed before the data can be processed by the -x option. Finally, the correct command (on a GNU/Linux system) to pipe the output of a dump in a restore is:

$ redis-cli --raw dump test | head -c-1 | redis-cli -x restore test1 0
OK

这不漂亮.我希望大多数人会依靠 perl/python/ruby 脚本而不是 shell 来完成这些任务.

This is not pretty. I expect most people would rely of a perl/python/ruby script rather than the shell to do such tasks.

这篇关于如何使用redis的`DUMP`和`RESTORE`(离线)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆