{"id":6783,"date":"2017-06-20T15:00:33","date_gmt":"2017-06-20T07:00:33","guid":{"rendered":"http:\/\/rmohan.com\/?p=6783"},"modified":"2017-06-20T16:00:09","modified_gmt":"2017-06-20T08:00:09","slug":"mongodb-db-backup","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=6783","title":{"rendered":"MongoDB DB backup"},"content":{"rendered":"<p><strong>MongoDB DB backup<\/strong><\/p>\n<p>[root@rmohan scripts]# cat mongodb_backup.sh<br \/>\n#!\/bin\/bash<br \/>\nPATH=\/usr\/local\/sbin:\/usr\/local\/bin:\/sbin:\/bin:\/usr\/sbin:\/usr\/bin:\/usr\/local\/mysql\/bin:\/usr\/local\/mongodb\/bin:\/usr\/local\/mongodb\/bin<br \/>\nexport PATH<br \/>\nexport LANG=en_US.utf8<\/p>\n<p>Dbip=&#8221;192.168.1.10&#8243;<br \/>\nDport=&#8221;30000&#8243;<br \/>\nDatetime=$(date +%Y-%m-%d_%H_%M_%S)<br \/>\nLogfile=&#8221;\/backup\/mongodb\/mongodbbak_10010\/backmongo.log&#8221;<br \/>\nDblist=&#8221;$(echo &#8216;show dbs&#8217;|\/usr\/local\/mongodb\/bin\/mongo &#8211;host ${Dbip} &#8211;port ${Dport} |egrep -v &#8220;MongoDB|connecting|bye&#8221;|awk &#8216;{print$1}&#8217;)&#8221;<\/p>\n<p>echo -e &#8220;=======start for database backup `date +%F&#8217; &#8216;%T&#8217; &#8216;%u`=======\\n&#8221;  >>$Logfile<br \/>\nbackdbcmd(){<br \/>\necho &#8220;$(date) backup $dbname&#8221;<br \/>\n\/usr\/local\/mongodb\/bin\/mongodump &#8211;host ${Dbip} &#8211;port ${Dport} &#8211;db ${dbname} &#8211;out \/backup\/mongodb\/mongodbbak_10010\/${Datetime}<br \/>\necho -e &#8220;$(date) Done.\\n&#8221;<br \/>\n}<\/p>\n<p>for dbname in $Dblist<br \/>\n    do<br \/>\n      echo -e &#8220;&#8212;&#8212;&#8211;$dbname&#8212;&#8212;&#8211;\\n&#8221; >>$Logfile<br \/>\n      backdbcmd >>$Logfile 2>&#038;1<br \/>\n    done<\/p>\n<p>\/usr\/local\/mongodb\/bin\/mongorestore  &#8211;port=30000 &#8211;dir=\/home\/mongodb\/2016-07-28_00_12_05 &#8211;writeConcern='{w:0,wtimeout:200}&#8217; &#8211;numParallelCollections=8 &#8211;numInsertionWorkersPerCollection=10 &#8211;stopOnError  -v &#8211;drop<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MongoDB DB backup<\/p>\n<p>[root@rmohan scripts]# cat mongodb_backup.sh #!\/bin\/bash PATH=\/usr\/local\/sbin:\/usr\/local\/bin:\/sbin:\/bin:\/usr\/sbin:\/usr\/bin:\/usr\/local\/mysql\/bin:\/usr\/local\/mongodb\/bin:\/usr\/local\/mongodb\/bin export PATH export LANG=en_US.utf8<\/p>\n<p>Dbip=&#8221;192.168.1.10&#8243; Dport=&#8221;30000&#8243; Datetime=$(date +%Y-%m-%d_%H_%M_%S) Logfile=&#8221;\/backup\/mongodb\/mongodbbak_10010\/backmongo.log&#8221; Dblist=&#8221;$(echo &#8216;show dbs&#8217;|\/usr\/local\/mongodb\/bin\/mongo &#8211;host ${Dbip} &#8211;port ${Dport} |egrep -v &#8220;MongoDB|connecting|bye&#8221;|awk &#8216;{print$1}&#8217;)&#8221;<\/p>\n<p>echo -e &#8220;=======start for database backup `date +%F&#8217; &#8216;%T&#8217; &#8216;%u`=======\\n&#8221; >>$Logfile backdbcmd(){ echo &#8220;$(date) backup $dbname&#8221; \/usr\/local\/mongodb\/bin\/mongodump &#8211;host ${Dbip} &#8211;port ${Dport} &#8211;db ${dbname} &#8211;out \/backup\/mongodb\/mongodbbak_10010\/${Datetime} echo [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[87],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6783"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6783"}],"version-history":[{"count":2,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6783\/revisions"}],"predecessor-version":[{"id":6785,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6783\/revisions\/6785"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}