发布时间:2019-09-15 09:57:05编辑:auto阅读(1901)
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中没有数据
导入数据后:
上一篇: Python中的PyDoc介绍
下一篇: 初学python案例 字典
47846
46397
37285
34736
29317
25974
24918
19952
19548
18031
5794°
6419°
5930°
5963°
7067°
5913°
5947°
6440°
6405°
7782°