redis-move.py

Скрипт для переноса ключей между базами redis

#!/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))

Пример использования:

eking@eking:~$ time python redis-move.py
Usage: redis-move.py [options] keymask

Script for move keys via redis DB

Options:
 -h, --help  show this help message and exit
 --fh=FH     from host; default 127.0.0.1
 --fp=FP     from port; default
 --th=TH     to host; default 127.0.0.2
 --tp=TP     to port; default 6379
 --fd=FD     from DB; default 0
 --td=TD     to DB; default 0
 -t T        timeout for MIGRATE command; default 1000
 -p P        number of processes; default 32

eking@eking:~$ time python redis-move.py --fh='10.10.10.110' --th='10.10.10.206' "*000000"

Found 3 keys, migrating...


real 0m0.989s
user 0m0.032s
sys  0m0.024s
eking@eking:~$

Скачать скрипт: redis-move.py.

Яндекс.Метрика