{"id":441,"date":"2007-11-30T00:00:00","date_gmt":"2007-11-29T16:00:00","guid":{"rendered":"http:\/\/www.strongd.net\/?p=441"},"modified":"2011-07-15T09:55:22","modified_gmt":"2011-07-15T01:55:22","slug":"mysql-backup-shell-script","status":"publish","type":"post","link":"https:\/\/www.strongd.net\/?p=441","title":{"rendered":"mysql backup shell script"},"content":{"rendered":"<p><P>mysql backup shell script<BR>#!\/bin\/sh<BR># mysql_backup.sh: backup mysql databases and keep newest 5 days backup.<BR>#<BR># Last updated: 2006.9.6<BR># &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<BR># This is a free shell script under GNU GPL version<BR># Feedback\/comment\/suggestions : <A href=\"http:\/\/www.strongd.net\">http:\/\/www.strongd.net<\/A><BR># &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/P><br \/>\n<P># your mysql login information<BR># db_user is mysql username<BR># db_passwd is mysql password<BR># db_host is mysql host<BR># &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<BR>db_user=&#8221;root&#8221;<BR>db_passwd=&#8221;passwd&#8221;<BR>db_host=&#8221;localhost&#8221;<\/P><br \/>\n<P># the directory for story your backup file.<BR>backup_dir=&#8221;\/backup&#8221;<\/P><br \/>\n<P># date format for backup file (dd-mm-yyyy)<BR>time=&#8221;$(date +&#8221;%d-%m-%Y&#8221;)&#8221;<\/P><br \/>\n<P># mysql, mysqldump and some other bin&#8217;s path<BR>MYSQL=&#8221;$(which mysql)&#8221;<BR>MYSQLDUMP=&#8221;$(which mysqldump)&#8221;<BR>MKDIR=&#8221;$(which mkdir)&#8221;<BR>RM=&#8221;$(which rm)&#8221;<BR>MV=&#8221;$(which mv)&#8221;<BR>GZIP=&#8221;$(which gzip)&#8221;<\/P><br \/>\n<P># check the directory for store backup is writeable<BR>test ! -w $backup_dir &amp;&amp; echo &#8220;Error: $backup_dir is un-writeable.&#8221; &amp;&amp; exit 0<\/P><br \/>\n<P># the directory for story the newest backup<BR>test ! -d &#8220;$backup_dir\/backup.0\/&#8221; &amp;&amp; $MKDIR &#8220;$backup_dir\/backup.0\/&#8221;<\/P><br \/>\n<P># get all databases<BR>all_db=&#8221;$($MYSQL -u $db_user -h $db_host -p$db_passwd -Bse &#8216;show databases&#8217;)&#8221;<\/P><br \/>\n<P>for db in $all_db<BR>do<BR>&nbsp;&nbsp;&nbsp; $MYSQLDUMP -u $db_user -h $db_host -p$db_passwd $db | $GZIP -9 &gt; &#8220;$backup_dir\/backup.0\/$time.$db.gz&#8221;<BR>done<\/P><br \/>\n<P># delete the oldest backup<BR>test -d &#8220;$backup_dir\/backup.5\/&#8221; &amp;&amp; $RM -rf &#8220;$backup_dir\/backup.5&#8221;<\/P><br \/>\n<P># rotate backup directory<BR>for int in 4 3 2 1 0<BR>do<BR>&nbsp;&nbsp;&nbsp; if(test -d &#8220;$backup_dir&#8221;\/backup.&#8221;$int&#8221;)<BR>&nbsp;&nbsp;&nbsp; then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; next_int=`expr $int + 1`<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $MV &#8220;$backup_dir&#8221;\/backup.&#8221;$int&#8221; &#8220;$backup_dir&#8221;\/backup.&#8221;$next_int&#8221;<BR>&nbsp;&nbsp;&nbsp; fi<BR>done<\/P><br \/>\n<P>exit 0; <\/P><br \/>\n<DIV><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>mysql backup shell script#!\/bin\/sh# mysql_backup.sh: backup mysql databases and keep newest 5 days backup.## Last updated: 2006.9.6# &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-# This is a free shell script under GNU GPL version# Feedback\/comment\/suggestions : http:\/\/www.strongd.net# &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- # your mysql login information# db_user is mysql username# db_passwd is mysql password# db_host is mysql host# &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;db_user=&#8221;root&#8221;db_passwd=&#8221;passwd&#8221;db_host=&#8221;localhost&#8221; # the directory for story &hellip; <a href=\"https:\/\/www.strongd.net\/?p=441\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">mysql backup shell script<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[133],"class_list":["post-441","post","type-post","status-publish","format-standard","hentry","category-mysql","tag-mysql-backup"],"_links":{"self":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/441","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=441"}],"version-history":[{"count":1,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/441\/revisions"}],"predecessor-version":[{"id":936,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/441\/revisions\/936"}],"wp:attachment":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}