redis-move.py (Источник)

#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
redis-move.py: Script for move keys via redis DB
'''
__author__ = 'Nikolay Gatilov'
__copyright__ = 'Nikolay Gatilov'
__license__ = 'GPL'
__version__ = '1.0.20150408'
__maintainer__ = 'Nikolay Gatilov'
__email__ = 'eking.work@gmail.com'
from optparse import OptionParser
from multiprocessing import Pool
import redis
import sys
def mig(c):
    fromredis.execute_command("MIGRATE",
                              options.th,
                              options.tp,
                              c,
                              options.td,
                              options.t)
parser = OptionParser(description='Script for move keys via redis DB',
                      usage='usage: %prog [options] keymask ')
parser.add_option('--fh', type='string', dest='fh',
                  help='from host; default 127.0.0.1', default='127.0.0.1')
parser.add_option('--fp', type='int', dest='fp',
                  help='from port; default 6379', default=6379)
parser.add_option('--th', type='string', dest='th',
                  help='to host; default 127.0.0.2', default='127.0.0.2')
parser.add_option('--tp', type='string', dest='tp',
                  help='to port; default 6379', default='6379')
parser.add_option('--fd', type='int', dest='fd',
                  help='from DB; default 0', default=0)
parser.add_option('--td', type='string', dest='td',
                  help='to DB; default 0', default='0')
parser.add_option('-t', type='string', dest='t',
                  help='timeout for MIGRATE command; default 1000',
                  default='1000')
parser.add_option('-p', type='int', dest='p',
                  help='number of processes; default 32', default=32)
(options, args) = parser.parse_args()
try:
    keymask = args[0]
except:
    parser.print_help()
    sys.exit()
fromredispool = redis.ConnectionPool(host=options.fh,
                                     port=options.fp,
                                     db=options.fd)
fromredis = redis.Redis(connection_pool=fromredispool)
L = fromredis.keys(keymask)
print '\nFound {0} keys, migrating...\n'.format(len(L))
pool = Pool(processes=options.p)
pool.map(mig, iter(L))