diff --git a/tools/migratedb/migratedb.py b/tools/migratedb/migratedb.py new file mode 100644 index 0000000..de85e8c --- /dev/null +++ b/tools/migratedb/migratedb.py @@ -0,0 +1,64 @@ +# -*- coding: utf-8 -*- +#!/usr/bin/python + +import pymysql +import hashlib +import json +import urllib.request +import base64 +import time +import datetime +import redis +import os +import functools + +CONFIG_DIR = '.' +context = { + 'channel': 0, + 'dbname_prefix': '' +} + +def md5Str(data): + m = hashlib.md5() + m.update(data) + return m.hexdigest() + +def fetchAllAccount(context, db_conf, all_account_hash): + for conf in db_conf: + conn = pymysql.connect(host = conf['host'], + port = conf['port'], + user = conf['user'], + passwd = conf['passwd'], + db = context['dbname_prefix'] + str(conf['instance_id']), + charset = 'utf8' + ) + cursor = conn.cursor() + last_idx = 0 + while True: + cursor.execute('SELECT idx, accountid FROM user ' + 'WHERE idx > %s AND accountid LIKE "%s_\%" LIMIT 0, 10000;' % + (last_idx, + context['dbname_prefix'] + )) + has_data = False + for row in cursor: + has_data = True + all_account_hash[row[1]] = { + 'md5' : md5Str(row[1]) + } + if row[0] > last_idx: + last_idx = row[0] + if not has_data: + break + +def main(): + global context + db_conf = json.loads(open(CONFIG_DIR + '/mysql_list.json', 'r').read()) + redis_conf = json.loads(open(CONFIG_DIR + '/redis_list.json', 'r').read()) + db_rule_conf = json.loads(open(CONFIG_DIR + '/mysql_rule.json', 'r').read()) + redis_rule_conf = json.loads(open(CONFIG_DIR + '/redis_rule.json', 'r').read()) + all_account_hash = {} + fetchAllAccount(context, db_conf, all_account_hash) + +if __name__ == "__main__": + main() diff --git a/tools/migratedb/redis_rule.json b/tools/migratedb/redis_rule.json new file mode 100644 index 0000000..ddc0dd3 --- /dev/null +++ b/tools/migratedb/redis_rule.json @@ -0,0 +1,6 @@ +[ + { + "key_prefix": "", + "pattern": "md5" + } +]