标签:scree redhat res def 密码 proc restore 实现 from
python实现mongodb的备份与导入
背景:
192.168.122.1 python
192.168.122.11 mongodb
Python备份mongo
代码:
# -*- coding:utf-8 -*-
import time import 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()
运行结果:
如下,已经备份到主机目录下;
Python导入mongodb数据
代码:
# -*- 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()
运行结果:
没有运行代码这是一个全新的主机,mongodb中没有数据
导入数据后:
标签:scree redhat res def 密码 proc restore 实现 from
原文地址:http://blog.51cto.com/13362895/2159197