mongosync.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # -*- coding: utf-8 -*-
  2. #!/usr/bin/env python
  3. import os
  4. import sys
  5. #: current_dir - 2
  6. PROJECT_ROOT = os.path.join(os.path.abspath(os.path.split(os.path.realpath(__file__))[0] + "/.."), '..')
  7. sys.path.insert(0, PROJECT_ROOT)
  8. import subprocess
  9. import click
  10. from apps.web.core.helpers import named_arguments
  11. MONGODUMP = 'mongodump'
  12. MONGORESTORE = 'mongorestore'
  13. DEFAULT_OUTPUT_DIR = 'D:\data'
  14. configs = {
  15. 'production': {
  16. 'washpay': {
  17. '--username': 'washpayer',
  18. '--password': 'UY3Bw%lK8NM56eQG',
  19. '--authenticationDatabase': 'washpay',
  20. '--port': '27118',
  21. '--host': '120.26.227.50',
  22. '--db': 'washpay'
  23. },
  24. 'report': {
  25. '--username': 'reporter',
  26. '--password': '1Cb%5O0*NtUTn3WN',
  27. '--authenticationDatabase': 'report',
  28. '--port': '27118',
  29. '--host': '120.26.227.50',
  30. '--db': 'report'
  31. }
  32. },
  33. 'staging': {
  34. 'washpay': {
  35. '--username': 'admin',
  36. '--password': 'dayuan5tao5ai',
  37. '--authenticationDatabase': 'admin',
  38. '--port': '27017',
  39. '--host': '121.41.75.233',
  40. '--db': 'washpay'
  41. },
  42. 'report': {
  43. '--username': 'admin',
  44. '--password': 'dayuan5tao5ai',
  45. '--authenticationDatabase': 'admin',
  46. '--port': '27017',
  47. '--host': '121.41.75.233',
  48. '--db': 'report'
  49. }
  50. },
  51. 'local': {
  52. 'washpay': {
  53. '--db': 'washpay'
  54. },
  55. 'report': {
  56. '--db': 'report'
  57. }
  58. }
  59. }
  60. def operation(exe, config, options):
  61. config.update(**options)
  62. _ = [exe] + sum(map(list, config.items()), [])
  63. print _
  64. return subprocess.check_call(_)
  65. mongodump = lambda config, options: operation(exe=MONGODUMP, config=config, options=options)
  66. mongorestore = lambda config, options: operation(exe=MONGORESTORE, config=config, options=options)
  67. @click.group()
  68. def cli():
  69. """
  70. 同步mongoDB
  71. :return:
  72. """
  73. click.echo(u"欢迎进入数据迁移脚本")
  74. @cli.command()
  75. @click.option('-d', '--direction', help='sync direction e.g. production2local')
  76. @click.option('-c', '--collection', help='特定表名,默认为所有', default=None)
  77. @click.option('-e', '--excludeCollection', default=None)
  78. @click.option('-o', '--out', help='目标目录', default=DEFAULT_OUTPUT_DIR)
  79. def sync(direction, collection, excludecollection, out):
  80. reserved = ['direction', 'in_reserved']
  81. in_reserved = lambda k, v : (k not in reserved) and v
  82. options = { ('--' + k) : v for k, v in named_arguments().items() if in_reserved(k, v) }
  83. click.echo('options:%s' % (options,))
  84. source, target = direction.split('2')
  85. mongodump(config=configs[source]['washpay'], options=options)
  86. mongodump(config=configs[source]['report'], options=options)
  87. mongorestore(config=configs[target]['washpay'], options=options)
  88. mongorestore(config=configs[target]['report'], options=options)
  89. @cli.command()
  90. @click.option('--conf', help='配置')
  91. @click.option('-c', '--collection', help='特定表名,默认为所有', default=None)
  92. @click.option('-e', '--excludeCollection', default=None)
  93. @click.option('-d', '--dir', help='目标目录', default=DEFAULT_OUTPUT_DIR)
  94. def restore(conf, collection, excludeCollection, dir):
  95. reserved = ['conf']
  96. p = lambda k, v : (k not in reserved) and v
  97. options = { ('--' + k) : v for k, v in named_arguments().items() if p(k, v) }
  98. mongorestore(config=conf, options=options)
  99. if __name__ == '__main__':
  100. cli()