Advertisement
Guest User

Untitled

a guest
Nov 8th, 2016
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. """
  2. Script that receives two Mongo URI defined databases and copies data from the
  3. first to the second.
  4. """
  5. import argparse
  6. import os
  7. import subprocess
  8. import sys
  9.  
  10. try:
  11. from pymongo.uri_parser import parse_uri
  12. except ImportError:
  13. print("Please install PyMongo")
  14. sys.exit(1)
  15.  
  16.  
  17. # Template for the mongodump command
  18. MONGODUMP_TEMPLATE = "mongodump -h {host} -u {user} -p {password} -d {db} -o {output}/"
  19.  
  20. # Template for the mongorestore command
  21. MONGORESTORE_TEMPLATE = "mongorestore -h {host} -d {destination_db} -u {user} -p {password} {folder}/{origin_db}"
  22.  
  23. # Template for host:port
  24. HOST_TEMPLATE = "{host}:{port}"
  25.  
  26.  
  27. def main(origin, destination):
  28. """
  29. Function that receives two Mongo connection URIs, dumps the data from
  30. `origin` and restores it to `destination`
  31. """
  32.  
  33. # Checking if both URIs have the correct schema
  34. if not (origin.startswith("mongodb://") and
  35. destination.startswith("mongodb://")):
  36. raise Exception("Both origin and destination must be Mongo URLs")
  37.  
  38. # Parsing the URIs
  39. try:
  40. origin = parse_uri(origin)
  41. destination = parse_uri(destination)
  42. except:
  43. print("Mongo URL parsing error!")
  44. raise
  45.  
  46. # Defining the destination path for the dump
  47. dump_folder = "dump_" + origin['database']
  48. dump_path = os.path.join(os.getcwd(), dump_folder)
  49.  
  50. # If the path already exists, we exit to avoid data overwriting
  51. if os.path.exists(dump_path):
  52. raise Exception('Destination path ' + dump_path + ' already exists!')
  53.  
  54. # If the parser returns more than one host, we quit
  55. if len(origin['nodelist']) != 1 or len(destination['nodelist']) != 1:
  56. raise Exception("Currently, we do not support more than one host")
  57.  
  58. # Assembling the origin host string
  59. origin_host = HOST_TEMPLATE.format(
  60. host=origin['nodelist'][0][0],
  61. port=str(origin['nodelist'][0][1])
  62. )
  63.  
  64. # Assembling the mongodump command
  65. mongodump_command = MONGODUMP_TEMPLATE.format(
  66. host=origin_host,
  67. user=origin['username'],
  68. password=origin['password'],
  69. db=origin['database'],
  70. output=dump_path
  71. )
  72.  
  73. # Assembling the destination host string
  74. destination_host = HOST_TEMPLATE.format(
  75. host=destination['nodelist'][0][0],
  76. port=str(destination['nodelist'][0][1])
  77. )
  78.  
  79. # Assembling the mongorestore command
  80. mongorestore_command = MONGORESTORE_TEMPLATE.format(
  81. host=destination_host,
  82. user=destination['username'],
  83. password=destination['password'],
  84. destination_db=destination['database'],
  85. folder=dump_path,
  86. origin_db=origin['database']
  87. )
  88.  
  89. # Printing the generated commands
  90. print(mongodump_command)
  91. print(mongorestore_command)
  92.  
  93. print("** Importing from origin **")
  94.  
  95. # Dumping the data
  96. mongodump = subprocess.Popen(mongodump_command,
  97. shell=True,
  98. stdout=subprocess.PIPE)
  99. # Waiting the process to finish
  100. mongodump.wait()
  101.  
  102. print("** Exporting to destination **")
  103.  
  104. # Restoring the data
  105. mongorestore = subprocess.Popen(mongorestore_command,
  106. shell=True,
  107. stdout=subprocess.PIPE)
  108. # Waiting for the process to finish
  109. mongorestore.wait()
  110.  
  111. print("Done!")
  112.  
  113.  
  114. if __name__ == "__main__":
  115. # Registering an argument parser
  116. parser = argparse.ArgumentParser(description="Migrates data from one Mongo database to another, using Mongo URLs")
  117. # Adding the argument for the origin URI
  118. parser.add_argument('origin', help='The Mongo URI for the origin server')
  119. # Adding the argument for the destination URI
  120. parser.add_argument('destination', help='The Mongo URI for the destination server')
  121. # Parsing the arguments
  122. args = parser.parse_args()
  123. # Calling main
  124. main(args.origin, args.destination)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement