Skip to content

Instantly share code, notes, and snippets.

@leetschau
Created May 28, 2016 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leetschau/7b8cee027949223d5fd76453ce59b3ae to your computer and use it in GitHub Desktop.
Save leetschau/7b8cee027949223d5fd76453ce59b3ae to your computer and use it in GitHub Desktop.
Copy collection between MongoDB servers
#!/usr/bin/python
import sys, re
from os import system
usage = """
Usage:
Full style: ./cpColl sUser:sPwd@sHost:sPort/sDb-sColl dUser:dPwd@dHost:dPort/dDb-dColl
Abbr for source on localhost: ./cpColl sDb-sColl dUser:dPwd@dHost:dPort/dDb-dColl
Abbr for destination on localhost: ./cpColl sUser:sPwd@sHost:sPort/sDb-sColl dDb-dColl
"""
if len(sys.argv) != 3:
sys.exit(usage)
srcDefs = sys.argv[1]
dstDefs = sys.argv[2]
def buildMatch(param):
fullMatch = re.search(r'(.*):(.*)@(.*):(.*)/(.*)-(.*)', param)
abbrMatch = re.search(r'(.*)-(.*)', param)
if fullMatch:
return fullMatch.groups()
elif abbrMatch and "@" not in param and ":" not in param:
return abbrMatch.groups()
else:
sys.exit("bad format")
def matchEle(matchGrp):
user, pwd, host, port, db, collName = (None, None, None, None, None, None)
if len(matchGrp) == 6:
user, pwd, host, port, db, collName = matchGrp
elif len(matchGrp) == 2:
db, collName = matchGrp
return user, pwd, host, port, db, collName
sUser, sPwd, sHost, sPort, sDb, sColl = matchEle(buildMatch(srcDefs))
dUser, dPwd, dHost, dPort, dDb, dColl = matchEle(buildMatch(dstDefs))
print("Source parameters:")
print(sUser, sPwd, sHost, sPort, sDb, sColl)
print("Destination parameters:")
print(dUser, dPwd, dHost, dPort, dDb, dColl)
DATA_CACHE='/tmp/data'
dumpCmd = "mongodump -d %s -c %s -o %s" % ( sDb, sColl, DATA_CACHE) \
if sHost is None else \
"mongodump -u %s -p %s -h %s --port %s -d %s -c %s -o %s" \
% ( sUser, sPwd, sHost, sPort, sDb, sColl, DATA_CACHE)
system('rm -rf ' + DATA_CACHE)
system(dumpCmd)
print('Data dump complete!')
uo = raw_input("Are you sure to REMOVE existing data in %s"\
"(DB: %s, Host: %s)?(y/n) " \
% ( dColl, dDb, ("localhost" if dHost is None else dHost) ))
if uo != 'y':
sys.exit("User cancelled")
dropCollCmd = "db.%s.drop()" % dColl
dbStr = dDb if dHost is None else \
"-u %s -p %s %s:%s/%s" % (dUser, dPwd, dHost, dPort, dDb)
clearColl = 'mongo %s --eval "%s"' % (dbStr, dropCollCmd)
system(clearColl)
remoteStr = "" if dHost is None else "-u %s -p %s -h %s --port %s" \
% (dUser, dPwd, dHost, dPort)
srcPath = DATA_CACHE + '/' + sDb + '/' + sColl + '.bson'
resStr = "mongorestore %s -d %s -c %s %s" % (remoteStr, dDb, dColl, srcPath)
system(resStr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment