Skip to content

Instantly share code, notes, and snippets.

@ei-grad
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ei-grad/2f506a7553a9b992e80f to your computer and use it in GitHub Desktop.
Save ei-grad/2f506a7553a9b992e80f to your computer and use it in GitHub Desktop.
Copy keys from one Redis server to another
#!/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