mongodb資料備份指令碼和日誌切割指令碼

來源:互聯網
上載者:User

標籤:role   檔案   root   str   man   bin   任務   idf   mongod   

mongodb資料備份指令碼和日誌切割指令碼

1.登陸mongo
mongo --port=27018

2.建立系統管理使用者:

db.createUser(...  {...  user: "root",...    pwd: "Zytest2233",...     roles: [ { role: "root", db: "admin" } ]...  }...  )

3.開啟認證:

[[email protected] scripts]# cat /usr/local/mongodb/mongod.cnf logpath=/data/mongodb/logs/mongodb.loglogappend = true#fork and run in backgroundfork = trueport = 27018dbpath=/data/mongodb/data#location of pidfilepidfilepath=/data/mongodb/mongod.pidauth = true#keyFile = /tmp/mongo-keyfile#nohttpinterface=true#replSet=shard1

4.mongodb日誌按天切割
mongodb日誌預設寫在一個檔案,時間久了檔案太大,用cron計劃任務+指令碼 可以實現按日期存放log

[[email protected] scripts]# cat /data/scripts/rotatelog.sh

#!/bin/bash#mongodb client  use admincd /usr/local/mongodb/binMongoDB=‘./mongo 127.0.0.1:27018‘$MongoDB <<EOFuse admindb.auth("root","Zytest2233")db.runCommand( { logRotate : 1 } )exit;EOF

再用 crontab -e 開啟加入如下內容,並儲存:
59 23 * * * /bin/sh /data/scripts/rotatelog.sh

[[email protected] scripts]# ll /data/mongodb/logs/
total 76

-rw-r--r--. 1 root root   982 Feb  2 15:27 mongodb.log-rw-r--r--. 1 root root 53353 Feb  2 15:14 mongodb.log.2018-02-02T07-18-39-rw-r--r--. 1 root root  6163 Feb  2 15:21 mongodb.log.2018-02-02T07-22-02-rw-r--r--. 1 root root  1849 Feb  2 15:27 mongodb.log.2018-02-02T07-27-02

5.mongodbDatabase Backup:
指定多個資料庫來進行備份指令碼:
指令碼1:
[[email protected] ~]# cat /data/backup/mongobackup/test.sh

mongo 127.0.0.1:27018/admin -u root -p Zytest2233  << EOFshow dbsexit;EOF

指令碼2:
[[email protected] ~]# cat mongoback.sh

#/bin/bashsource /etc/profileuser="root"pass="Zytest2233"port="28018"Cmd="/usr/local/mongodb/bin/mongodump"Bakdir=/data/backup/mongobackupdbs="$(/bin/sh /data/backup/mongobackup/test.sh|sed ‘1,2d‘|grep -v "local"|awk ‘{ print $1 }‘|xargs -n4)"Dump="$Cmd -uroot -p$pass --port=$port -d"for n in $dbsdo$Dump $n -o $Bakdir done

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.