{"id":209,"date":"2023-02-22T22:25:38","date_gmt":"2023-02-22T14:25:38","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=209"},"modified":"2023-04-30T15:28:35","modified_gmt":"2023-04-30T07:28:35","slug":"python-implement-data-migration-between-different-redis-instances","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/22\/python-implement-data-migration-between-different-redis-instances\/","title":{"rendered":"Python\u5b9e\u73b0Redis\u4e0d\u540c\u5b9e\u4f8b\u95f4\u6570\u636e\u8fc1\u79fb"},"content":{"rendered":"<p>Redis\u5355\u673a\u7248\u6570\u636e\u8fc1\u79fb\u3002\u4ece\u4e00\u4e2a\u5b9e\u4f8b\u8fc1\u79fb\u5230\u53e6\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u4f7f\u7528\u4e8e\u672c\u673a\u4e0d\u540c\u7aef\u53e3\u8fc1\u79fb\u5907\u4efd\u3001\u5176\u4ed6\u673a\u5668\u95f4\u8fc1\u79fb\u5907\u4efd\u3002<\/p>\n<p>\u5de5\u5177\u662f\u4e00\u4e2a\u7b80\u5355\u7684 python \u811a\u672c\uff0c\u6307\u5b9a redis \u7684\u6e90\u548c\u76ee\u6807\u4fe1\u606f\u5373\u53ef\u3002<\/p>\n<p>\u811a\u672c\u94fe\u63a5\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/staugur\/scripts\/blob\/master\/services\/migrateredis.py\">https:\/\/github.com\/staugur\/scripts\/blob\/master\/services\/migrateredis.py<\/a><\/p>\n<p><!-- more --><\/p>\n<p>\u5176\u5185\u5bb9\u76f4\u63a5\u8d34\u5728\u4e0b\u9762\uff1a<\/p>\n<pre><code class=\"language-python\"># -*- coding: utf-8 -*-\n&quot;&quot;&quot;\n    migrateredis.py\n    ---------------\n    \u8fc1\u79fbredis\u6570\u636e\uff0c\u53ef\u4ee5\u4ece\u672c\u673a\u8fc1\u79fb\u5230\u5176\u4ed6\u673a\u5668\uff0c\u8fc1\u79fb\u4e00\u4e2a\u5e93\u6216\u591a\u4e2a\u5e93\u4e2d\u6570\u636e\u3002\n    \u539f\u7406\uff1aredis dump\/restore\u547d\u4ee4\n    \u4f9d\u8d56\uff1apip install redis&gt;=2.10.5\n&quot;&quot;&quot;\n\nfrom redis import from_url, RedisError\n\ndef migrate(src_url, dst_url):\n    src = from_url(src_url)\n    dst = from_url(dst_url)\n    for key in src.keys():\n        try:\n            dst.restore(key, src.ttl(key), src.dump(key))\n        except RedisError:\n            print(&#039;Migrate %s failed&#039; % key)\n\nif __name__ == &quot;__main__&quot;:\n    # \u6e90redis\u7684url\uff0c\u683c\u5f0f\uff1a\n    #redis:\/\/[:password]@host:port\/db\n    #host,port\u5fc5\u586b\u9879,\u5982\u6709\u5bc6\u7801,\u8bb0\u5f97\u5bc6\u7801\u524d\u52a0\u5192\u53f7,\u6bd4\u5982redis:\/\/localhost:6379\/0\n    src_url = &quot;redis:\/\/@127.0.0.1:6379\/0&quot;\n    # \u8fc1\u79fb\u76ee\u6807redis\u7684url\n    dst_url = &quot;redis:\/\/@127.0.0.1:16379\/0&quot;\n    # \u6267\u884c\n    if src_url and dst_url:\n        migrate(src_url, dst_url)<\/code><\/pre>\n<p>\u6ce8\u610f\uff1a<\/p>\n<p>\u4f7f\u7528\u65f6\u5148\u5b89\u88c5\u4f9d\u8d56\u6a21\u5757\uff0c\u547d\u4ee4\u662f\uff1a<code>pip install redis&gt;=2.10.5<\/code><\/p>\n<p>\u518d\u4fee\u6539<code>src_url<\/code>\u548c<code>dst_url<\/code>\uff0c\u53c2\u89c1\u6ce8\u91ca\uff0c\u7136\u540e\u7528python\u6267\u884c\u6b64\u811a\u672c\u3002<\/p>\n<p>\u82e5<code>pip install<\/code>\u65f6\u63d0\u793a<code>not found command<\/code>\uff0cCentOS\u7cfb\u7edf\u4f7f\u7528<code>yum install -y python-pip<\/code>\u5b89\u88c5\uff0cUbuntu\u4f7f\u7528<code>apt-get install python-pip<\/code><\/p>\n<p>\u5c01\u88c5\uff1a<\/p>\n<pre><code class=\"language-python\"># -*- coding: utf-8 -*-\n&quot;&quot;&quot;\n    migrateredis.py\n    ---------------\n    \u8fc1\u79fbredis\u6570\u636e\uff0c\u53ef\u4ee5\u4ece\u672c\u673a\u8fc1\u79fb\u5230\u5176\u4ed6\u673a\u5668\uff0c\u8fc1\u79fb\u4e00\u4e2a\u5e93\u6216\u591a\u4e2a\u5e93\u4e2d\u6570\u636e\u3002\n    \u539f\u7406\uff1aredis dump\/restore\u547d\u4ee4\n    \u4f9d\u8d56\uff1apip install redis&gt;=2.10.5\n&quot;&quot;&quot;\n\nfrom sys import argv\nfrom redis import from_url, RedisError\n\ndef migrate(src_url, dst_url):\n    src = from_url(src_url)\n    dst = from_url(dst_url)\n    for key in src.keys():\n        try:\n            dst.restore(key, src.ttl(key) or 0, src.dump(key))\n        except RedisError as e:\n            print(e)\n            print(&#039;Migrate %s failed&#039; % key)\n\nif __name__ == &quot;__main__&quot;:\n    # \u6e90redis\u7684url\uff0c\u683c\u5f0f\uff1a\n    #redis:\/\/[:password]@host:port\/db\n    #host,port\u5fc5\u586b\u9879,\u5982\u6709\u5bc6\u7801,\u8bb0\u5f97\u5bc6\u7801\u524d\u52a0\u5192\u53f7,\u6bd4\u5982redis:\/\/localhost:6379\/0\n    src_url = argv[1]\n    # \u8fc1\u79fb\u76ee\u6807redis\u7684url\n    dst_url = argv[2]\n    # \u6267\u884c\n    if src_url and dst_url:\n        migrate(src_url, dst_url)<\/code><\/pre>\n<blockquote>\n<p>\u8f6c\u8f7d\u81f3\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/blog.saintic.com\/blog\/265.html\">https:\/\/blog.saintic.com\/blog\/265.html<\/a><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Redis\u5355\u673a\u7248\u6570\u636e\u8fc1\u79fb\u3002\u4ece\u4e00\u4e2a\u5b9e\u4f8b\u8fc1\u79fb\u5230\u53e6\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u4f7f\u7528\u4e8e\u672c\u673a\u4e0d\u540c\u7aef\u53e3\u8fc1\u79fb\u5907\u4efd\u3001\u5176\u4ed6\u673a\u5668\u95f4\u8fc1\u79fb\u5907\u4efd\u3002 \u5de5\u5177\u662f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[],"class_list":["post-209","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/209","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=209"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/209\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}