# -*- coding: utf-8 -*- #!/usr/bin/env python import os import sys #: current_dir - 2 PROJECT_ROOT = os.path.join(os.path.abspath(os.path.split(os.path.realpath(__file__))[0] + "/.."), '..') sys.path.insert(0, PROJECT_ROOT) import subprocess import click from apps.web.core.helpers import named_arguments MONGODUMP = 'mongodump' MONGORESTORE = 'mongorestore' DEFAULT_OUTPUT_DIR = 'D:\data' configs = { 'production': { 'washpay': { '--username': 'washpayer', '--password': 'UY3Bw%lK8NM56eQG', '--authenticationDatabase': 'washpay', '--port': '27118', '--host': '120.26.227.50', '--db': 'washpay' }, 'report': { '--username': 'reporter', '--password': '1Cb%5O0*NtUTn3WN', '--authenticationDatabase': 'report', '--port': '27118', '--host': '120.26.227.50', '--db': 'report' } }, 'staging': { 'washpay': { '--username': 'admin', '--password': 'dayuan5tao5ai', '--authenticationDatabase': 'admin', '--port': '27017', '--host': '121.41.75.233', '--db': 'washpay' }, 'report': { '--username': 'admin', '--password': 'dayuan5tao5ai', '--authenticationDatabase': 'admin', '--port': '27017', '--host': '121.41.75.233', '--db': 'report' } }, 'local': { 'washpay': { '--db': 'washpay' }, 'report': { '--db': 'report' } } } def operation(exe, config, options): config.update(**options) _ = [exe] + sum(map(list, config.items()), []) print _ return subprocess.check_call(_) mongodump = lambda config, options: operation(exe=MONGODUMP, config=config, options=options) mongorestore = lambda config, options: operation(exe=MONGORESTORE, config=config, options=options) @click.group() def cli(): """ 同步mongoDB :return: """ click.echo(u"欢迎进入数据迁移脚本") @cli.command() @click.option('-d', '--direction', help='sync direction e.g. production2local') @click.option('-c', '--collection', help='特定表名,默认为所有', default=None) @click.option('-e', '--excludeCollection', default=None) @click.option('-o', '--out', help='目标目录', default=DEFAULT_OUTPUT_DIR) def sync(direction, collection, excludecollection, out): reserved = ['direction', 'in_reserved'] in_reserved = lambda k, v : (k not in reserved) and v options = { ('--' + k) : v for k, v in named_arguments().items() if in_reserved(k, v) } click.echo('options:%s' % (options,)) source, target = direction.split('2') mongodump(config=configs[source]['washpay'], options=options) mongodump(config=configs[source]['report'], options=options) mongorestore(config=configs[target]['washpay'], options=options) mongorestore(config=configs[target]['report'], options=options) @cli.command() @click.option('--conf', help='配置') @click.option('-c', '--collection', help='特定表名,默认为所有', default=None) @click.option('-e', '--excludeCollection', default=None) @click.option('-d', '--dir', help='目标目录', default=DEFAULT_OUTPUT_DIR) def restore(conf, collection, excludeCollection, dir): reserved = ['conf'] p = lambda k, v : (k not in reserved) and v options = { ('--' + k) : v for k, v in named_arguments().items() if p(k, v) } mongorestore(config=conf, options=options) if __name__ == '__main__': cli()