python implementation of MongoDB backup and import
Background:
192.168.122.1 python
192.168.122.11 MongoDB
Python backup MONGO
Code:
#-*-Coding:utf-8-*-
Import timeimport os dbuser= ' MyAdmin ' #用户
dbpass= ' Redhat ' #密码
ip= ' 192.168.122.11 ' #主机
Data_dir= '/data ' #目录
path_dump= '/usr/local/mongodb/bin/mongodump ' #命令路径
backitems=["%s-h%s:27017-u%s-p%s-o%s"% (Path,ip,dbuser,dbpass,data_dir)]
Def backdata ():
Try
For item in Backitems:
Print Item
Print Os.system (item)
Except Runtimeerror,e:
Print str (e)
If __name__== "__main__":
Backdata ()
Operation Result:
As below, has been backed up to the host directory;
Python Importing MongoDB data
Code:
#-*-Coding:utf-8-*-
Import time
Import OS
Dbuser= ' MyAdmin '
dbpass= ' Redhat '
ip= ' 192.168.122.1 ' #将数据导入改主机
Data_dir= '/data '
path_res= '/usr/local/mongodb/bin/mongorestore '
backitems=[
"%s-h%s:27017--dir%s"% (Path_res,ip,data_dir)
]
Def backdata ():
Try
For item in Backitems:
Print Item
Print Os.system (item)
Except Runtimeerror,e:
Print str (e)
If __name__== "__main__":
Backdata ()
Operation Result:
No running code this is a completely new host, no data in MongoDB
After importing the data:
Python implementation of MongoDB backup and import