Python implementation of MongoDB backup and import

Source: Internet
Author: User
Tags myadmin


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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.