專案開發環境由於伺服器切換問題,需要將Redis資料遷移一次,所以寫了下邊指令碼做遷移。
環境:Ubuntu 20.04python 3.8redis-py 3.5.3指令碼#!/usr/bin/env python# -*- coding:utf-8 -*-import datetimeimport loggingimport osimport tracebackfrom redis import StrictRedisexecute_file_name = os.path.basename(__file__).split(".")[0]logging.basicConfig(filename=execute_file_name+".log", format='%(message)s', filemode='w')logger = logging.getLogger()logger.setLevel(logging.INFO)def move_data(src_host=None, src_port=6379, src_db=0, desc_host=None, desc_port=6379, desc_db=0): src_redis = StrictRedis(host=src_host, port=src_port, db=src_db, decode_responses=True) dest_redis = StrictRedis(host=desc_host, port=desc_port, db=desc_db, decode_responses=True) try: # 獲取所有key keys = src_redis.keys() for key in keys: # key對應的資料型別 key_type = src_redis.type(key) print('\n=====================\nkey:{}'.format(key)) logger.info('\n=====================\nkey:{}'.format(key)) # String型別 if key_type == 'string': src_value = str(src_redis.get(key)) dest_redis.set(key, src_value) dest_value = dest_redis.get(key) print('Moved type:"{}", data{}={} to desc db: {}'.format( key_type, key, src_value, dest_value)) logger.info('Moved type:"{}", data{}={} to desc db: {}'.format( key_type, key, src_value, dest_value)) # Hash型別 elif key_type == 'hash': src_value = src_redis.hgetall(key) # 遍歷field for field in src_value: field = str(field) field_value = str(src_redis.hget(key, field)) dest_redis.hset(key, field, field_value) dest_value = dest_redis.hgetall(key) print('Moved type:"{}", data{}={} to desc db: {}'.format( key_type, key, src_value, dest_value)) logger.info('Moved type:"{}", data{}={} to desc db: {}'.format( key_type, key, src_value, dest_value)) # List型別 elif key_type == 'list': src_value = src_redis.lrange(key, 0, src_redis.llen(key)) for value in src_value: dest_redis.rpush(key, str(value)) dest_value = dest_redis.lrange(key, 0, src_redis.llen(key)) print('Moved type:"{}", data{}={} to desc db: {}'.format( key_type, key, src_value, dest_value)) logger.info('Moved type:"{}", data{}={} to desc db: {}'.format( key_type, key, src_value, dest_value)) # Set型別 elif key_type == 'set': src_value = src_redis.scard(key) for value in src_redis.smembers(key): dest_redis.sadd(key, str(value)) dest_value = dest_redis.scard(key) print('Moved type:"{}", data{}={} to desc db: {}'.format( key_type, key, src_value, dest_value)) logger.info('Moved type:"{}", data{}={} to desc db: {}'.format( key_type, key, src_value, dest_value)) # ZSet型別 elif key_type == 'zset': src_value = src_redis.zcard(key) for value in src_redis.zrange(key, 0, 100): value = str(value) score = src_redis.zscore(key, value) dest_redis.zadd(key, {value: score}) dest_value = dest_redis.zcard(key) print('Moved type:"{}", data{}={} to desc db: {}'.format( key_type, key, src_value, dest_value)) logger.info('Moved type:"{}", data{}={} to desc db: {}'.format( key_type, key, src_value, dest_value)) else: print('Dont support key type: {} - {}'.format(key_type, key)) logger.info('Dont support key type: {} - {}'.format(key_type, key)) # expire time ttl = src_redis.ttl(key) if ttl > 0: dest_redis.expire(key, ttl) print('Expire key:{}, ttl:{}'.format(key, str(ttl))) logger.info('Expire key:{}, ttl:{}'.format(key, str(ttl))) except Exception as e: traceback.print_exc() logger.error(traceback.format_exc())if __name__ == '__main__': starttime = datetime.datetime.now() print('Begin time: {}'.format(starttime.strftime("%Y-%m-%d %H:%M:%S"))) logger.info('Begin time: {}'.format( starttime.strftime("%Y-%m-%d %H:%M:%S"))) # 遷移資料 move_data( src_host='192.168.2.32', src_port=6379, src_db=0, desc_host='192.168.1.150', desc_port=6379, desc_db=0 ) endtime = datetime.datetime.now() print('\nEnd time: {}'.format(endtime.strftime("%Y-%m-%d %H:%M:%S"))) logger.info('\nEnd time: {}'.format(endtime.strftime("%Y-%m-%d %H:%M:%S"))) print('Using time: {}s'.format((endtime-starttime).seconds)) logger.info('Using time: {}s'.format((endtime-starttime).seconds))
最新評論