{"id":796,"date":"2010-08-24T01:56:40","date_gmt":"2010-08-23T18:56:40","guid":{"rendered":"http:\/\/adityo.blog.binusian.org\/?p=796"},"modified":"2010-08-24T02:02:29","modified_gmt":"2010-08-23T19:02:29","slug":"mysql-backup-script-with-auto-delete-the-old-one","status":"publish","type":"post","link":"https:\/\/adityo.blog.binusian.org\/?p=796","title":{"rendered":"mysql backup script with auto delete the old one"},"content":{"rendered":"<p>It&#8217;s been a while i haven&#8217;t post anything \ud83d\ude42 , i got this request on how to backup all of mysql databaseand since the hdd is not quite big it also required to delete the old backup. So here is the backup script<\/p>\n<p>###################################################################################################<\/p>\n<p>#!\/bin\/sh<\/p>\n<p>DATE=`date +%d%b-%H%M%S`<\/p>\n<p>unset PATH<br \/>\n# USER VARIABLES<br \/>\nMYSQLUSER=user<br \/>\nMYSQLPWD=password<br \/>\nMYSQLHOST=localhost<br \/>\nMYSQLBACKUPDIR=\/home\/backup\/mysql\/mysql_backup$DATE<\/p>\n<p>FILE=$MYSQLBACKUPDIR.tgz; # No need to modify this one<\/p>\n<p># PATH VARIABLES &#8211; It depend on your server program<br \/>\nMK=\/bin\/mkdir;<br \/>\nRM=\/bin\/rm;<br \/>\nGREP=\/bin\/grep;<br \/>\nMYSQL=\/usr\/local\/mysql\/bin\/mysql;<br \/>\nMYSQLDUMP=\/usr\/local\/mysql\/bin\/mysqldump;<br \/>\nXARGS=\/usr\/bin\/xargs;<br \/>\nFIND=\/usr\/bin\/find;<br \/>\nTAR=\/bin\/tar;<br \/>\nUUENCODE=\/usr\/bin\/uuencode;<\/p>\n<p># CREATE MYSQL BACKUP<\/p>\n<p># Delete the old ones. The +7 means 7 days ago<br \/>\n$FIND \/home\/backup\/mysql\/ -mtime +7 |$XARGS $RM -rf<\/p>\n<p># Create new backup dir<br \/>\n$MK -p $MYSQLBACKUPDIR<\/p>\n<p>#Dump new files<br \/>\nfor i in $(echo &#8216;SHOW DATABASES;&#8217; | $MYSQL -u$MYSQLUSER -p$MYSQLPWD -h$MYSQLHOST|$GREP -v &#8216;^Database$&#8217;); do<br \/>\n$MYSQLDUMP\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \\<br \/>\n-u$MYSQLUSER -p$MYSQLPWD -h$MYSQLHOST\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \\<br \/>\n-Q -c -C &#8211;add-drop-table &#8211;add-locks &#8211;quick &#8211;skip-lock-tables\u00a0\u00a0 \\<br \/>\n$i &gt; $MYSQLBACKUPDIR\/$i.sql;<br \/>\ndone;<\/p>\n<p># collect and compress the sqls<br \/>\n$TAR -cvzpf $MYSQLBACKUPDIR.tgz $MYSQLBACKUPDIR<br \/>\n$RM -r $MYSQLBACKUPDIR<\/p>\n<p>###################################################################################################<\/p>\n<p>Then save it , i save it as mysql_backup file. Then open the crontab to run it weekly<\/p>\n<p># crontab -e<\/p>\n<p>then put this<\/p>\n<p>0 0 * * 0 \/root\/mysql_backup &gt; \/dev\/null 2&gt;&amp;1<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s been a while i haven&#8217;t post anything \ud83d\ude42 , i got this request on how to backup all of mysql databaseand since the hdd is not quite big it also required to delete the old backup. So here is the backup script ################################################################################################### #!\/bin\/sh DATE=`date +%d%b-%H%M%S` unset PATH # USER VARIABLES MYSQLUSER=user MYSQLPWD=password MYSQLHOST=localhost [&hellip;]<\/p>\n","protected":false},"author":386,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[491],"tags":[6631],"class_list":["post-796","post","type-post","status-publish","format-standard","hentry","category-mysql","tag-mysql-backup-script"],"_links":{"self":[{"href":"https:\/\/adityo.blog.binusian.org\/index.php?rest_route=\/wp\/v2\/posts\/796","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/adityo.blog.binusian.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/adityo.blog.binusian.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/adityo.blog.binusian.org\/index.php?rest_route=\/wp\/v2\/users\/386"}],"replies":[{"embeddable":true,"href":"https:\/\/adityo.blog.binusian.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=796"}],"version-history":[{"count":3,"href":"https:\/\/adityo.blog.binusian.org\/index.php?rest_route=\/wp\/v2\/posts\/796\/revisions"}],"predecessor-version":[{"id":798,"href":"https:\/\/adityo.blog.binusian.org\/index.php?rest_route=\/wp\/v2\/posts\/796\/revisions\/798"}],"wp:attachment":[{"href":"https:\/\/adityo.blog.binusian.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=796"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/adityo.blog.binusian.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=796"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/adityo.blog.binusian.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=796"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}