-
-
Save chamnan/a8d247f7ef296fa0ce2082bfe0973329 to your computer and use it in GitHub Desktop.
A simple script to migrate all keys from one Redis to another
This file contains hidden or 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 | |
| import argparse | |
| import redis | |
| import ast | |
| def connect_redis(conn_dict): | |
| conn = redis.StrictRedis(host=conn_dict['host'], | |
| port=conn_dict['port'], | |
| db=int(conn_dict['db']), | |
| password=conn_dict['auth']) | |
| return conn | |
| def conn_string_type(string): | |
| format = "{'host':'127.0.0.1', 'port':3679, 'db': 1, 'auth':'*Passwword*'}" | |
| try: | |
| conn_dict = ast.literal_eval(string) | |
| if conn_dict.keys() != {'host', 'port', 'db', 'auth'}: | |
| raise argparse.ArgumentTypeError('incorrect format, should be: %s' % format) | |
| except: | |
| raise argparse.ArgumentTypeError('incorrect format, should be: %s' % format) | |
| return conn_dict | |
| def migrate_redis(source, destination): | |
| src = connect_redis(source) | |
| dst = connect_redis(destination) | |
| for key in src.keys('Testing:*'): | |
| ttl = src.ttl(key) | |
| # we handle TTL command returning -1 (no expire) or -2 (no key) | |
| if ttl < 0: | |
| ttl = 0 | |
| print("Dumping key: %s" % key) | |
| value = src.dump(key) | |
| if value is None: | |
| value = '' | |
| print("Restoring key: %s" % key) | |
| try: | |
| dst.restore(key, ttl * 1000, value, replace=True) | |
| except redis.exceptions.ResponseError: | |
| print("Failed to restore key: %s" % key) | |
| pass | |
| return | |
| def run(): | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument('source', type=conn_string_type) | |
| parser.add_argument('destination', type=conn_string_type) | |
| options = parser.parse_args() | |
| migrate_redis(options.source, options.destination) | |
| if __name__ == '__main__': | |
| run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated args format to string of dictionary and prevent None value error.