Last active
August 29, 2015 14:07
-
-
Save ei-grad/2f506a7553a9b992e80f to your computer and use it in GitHub Desktop.
Copy keys from one Redis server to another
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# encoding: utf-8 | |
# the default py2.7 print doesn't accept 'file' argument | |
from __future__ import print_function | |
import sys | |
from redis import Redis | |
def cp_redis(src, dst, src_prefix, dst_prefix=None): | |
if dst_prefix is None: | |
dst_prefix = src_prefix | |
src_redis = Redis.from_url(src) | |
keys = src_redis.keys('%s*' % src_prefix) | |
p = src_redis.pipeline() | |
for i in keys: | |
p.get(i) | |
values = p.execute() | |
p2 = Redis.from_url(dst).pipeline() | |
for k, v in zip(keys, values): | |
p2.set(dst_prefix + k[len(src_prefix):], v) | |
p2.execute() | |
if __name__ == "__main__": | |
if len(sys.argv) not in (4, 5): | |
usage = "Usage: %s src_redis_url dst_redis_url src_prefix [dst_prefix]" | |
print(usage % sys.argv[0], file=sys.stderr) | |
sys.exit(1) | |
else: | |
cp_redis(*sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment