mongodb遠程備份

來源:互聯網
上載者:User

標籤:else   dmi   any   str   popen   cal   mongodb   mongod   ODB   

建立備份使用者
db.createUser({user: 'backup',pwd: 'back123' ,roles : [{role : 'userAdminAnyDatabase' ,db : 'admin' },{role : 'readAnyDatabase' ,db : 'admin' },{role : 'dbOwner' ,db : 'admin' },{role : 'userAdmin' ,db : 'admin' },{role : 'root' ,db : 'admin' },{role : 'dbAdmin' ,db : 'admin' }]})
命令備份
mongodump -h 10.92.0.26 --port=27017 -ubackup -pbackup123  --authenticationDatabase admin -o /opt/data/back/
備份指令碼
#coding:utf-8import sys,subprocess,os,time,datetimeimport shutildef pay_mongodb_back():    try:        db_host = '10.92.0.26'        db_port = '27017'        db_user = 'backup'        db_pass = 'backup123'        back_dir = '/opt/data/back/{0}/'.format(db_host) + time.strftime('%Y-%m-%d', time.localtime(time.time()))        date =time.strftime('%Y-%m-%d-%H:%M:%S', time.localtime(time.time()))        TAR_BAK="mongodb_bak_{0}.zip".format(date)        tmp_dir= '/tmp/{ip}tmp_mongodb_bak'.format(ip=db_host)        if os.path.exists(back_dir):            pass        else:            os.makedirs(back_dir)        if os.path.exists(tmp_dir):            pass        else:            os.makedirs(tmp_dir)        ret = subprocess.Popen('mongodump -h {0} --port={1} -u {2} -p {3}  --authenticationDatabase admin -o {4} &&cd {5} && zip  -r {6}/{7}  {8}/*'.format(db_host,db_port,db_user,db_pass,tmp_dir,tmp_dir,back_dir,TAR_BAK,tmp_dir),shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE, stderr=subprocess.PIPE)        print ('本次{ip}mongodb備份成功執行').format(ip=db_host)        time.sleep(0.5)        shutil.rmtree(tmp_dir)    except Exception as e:        print ('備份失敗,原因:',e)if __name__ == '__main__':    date =time.strftime('%Y-%m-%d-%H:%M:%S', time.localtime(time.time()))    print ('備份時間:',date)    pay_mongodb_back()

mongodb遠程備份

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.