Skip to content

Instantly share code, notes, and snippets.

@alswl
Created July 9, 2017 03:18
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 alswl/e96a5308ebac4f69f809f9ba56dfe168 to your computer and use it in GitHub Desktop.
Save alswl/e96a5308ebac4f69f809f9ba56dfe168 to your computer and use it in GitHub Desktop.
# coding=utf-8
import sys
import time
import logging
import redis
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler(sys.stderr)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
FROM_REDIS_HOST_PORT_DB = ('192.168.172.29', 6379, 5)
TO_REDIS_HOST_PORT_DB = ('192.168.172.46', 6379, 0)
EACH_SCAN_SIZE = 1000
def migrate_keys(from_redis, to_redis, keys):
from_pipe = from_redis.pipeline(transaction=False)
to_pipe = to_redis.pipeline(transaction=False)
for key in keys:
from_pipe.pttl(key)
from_pipe.dump(key)
res = from_pipe.execute()
res_map = {x: (y[0], y[1]) for x, y in zip(keys, zip(res[::2], res[1::2]))}
for key in keys:
if res_map[key][0] == -2:
logger.warning('x, old not exists, key: %s' % key)
continue
to_pipe.delete(key)
to_pipe.restore(key, ttl=max(res_map[key][0], 0), value=res_map[key][1])
to_pipe.execute()
from_pipe = from_redis.pipeline(transaction=False)
for key in keys:
from_pipe.delete(key)
from_pipe.execute()
def main():
from_redis = redis.StrictRedis(host=FROM_REDIS_HOST_PORT_DB[0], port=FROM_REDIS_HOST_PORT_DB[1],
db=FROM_REDIS_HOST_PORT_DB[2])
to_redis = redis.StrictRedis(host=TO_REDIS_HOST_PORT_DB[0], port=TO_REDIS_HOST_PORT_DB[1],
db=TO_REDIS_HOST_PORT_DB[2])
cursor = 0
start = time.time()
count = 0
while True:
cursor, keys = from_redis.scan(cursor=cursor, count=EACH_SCAN_SIZE)
migrate_keys(from_redis, to_redis, keys)
if cursor == 0:
logger.info('v, dumps keys complete')
break
count += len(keys)
logger.info('v, dumps keys %d/%d/..., time: %s' % (len(keys), count, time.time() - start))
time.sleep(0.001)
def test_one():
from_redis = redis.StrictRedis(host=FROM_REDIS_HOST_PORT_DB[0], port=FROM_REDIS_HOST_PORT_DB[1],
db=FROM_REDIS_HOST_PORT_DB[2])
to_redis = redis.StrictRedis(host=TO_REDIS_HOST_PORT_DB[0], port=TO_REDIS_HOST_PORT_DB[1],
db=TO_REDIS_HOST_PORT_DB[2])
keys = ['u:881332:alive', 'u:881332:ua', 'u:881332:fu:lts', 'u:881332:fu:ots', 'u:881332:fa:lts', 'u:881332:fa:ots',
'u:881332:cf:p', 'u:881332:f', 'follow-mutual:881332', 'following:881332', 'follower:881332']
migrate_keys(from_redis, to_redis, keys)
if __name__ == '__main__':
main()
# test_one()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment