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

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

问题描述

我尝试了redis的 DUMP 命令,重定向到文件(或管道),但是还原报告此错误:

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 ,但是毕竟 DUMP RESTORE 到底如何工作?

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

推荐答案

转储/还原命令并非真正设计用于命令行,因为序列化格式是二进制的(与RDB转储使用的格式相同)。因为外壳程序倾向于解释那些字符(即使使用可打印格式,也是如此)。

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
"\n\x15\x15\x00\x00\x00\x12\x00\x00\x00\x05\x00\x00\xf6\x02\xf5\x02\xf4\x02\xf3\x02\xf2\xff\x06\x00\x1c\x8a\xda\x0e}\xcb\xe1."

可打印格式不能用作-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                                             |..|

现在,不可能将-raw dump的结果直接传递给-x恢复,因为最后一个字符是错误的。比较--raw和printable转储的输出。您会注意到--raw选项在末尾添加了一个额外的\n。 raw选项不是100%raw;-)

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 \n 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天全站免登陆