{"id":2191,"date":"2013-07-07T14:54:18","date_gmt":"2013-07-07T06:54:18","guid":{"rendered":"http:\/\/rmohan.com\/?p=2191"},"modified":"2013-07-07T14:54:52","modified_gmt":"2013-07-07T06:54:52","slug":"mysql-backup","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=2191","title":{"rendered":"MySQL Backup"},"content":{"rendered":"<p>#! \/bin\/bash<\/p>\n<p>&nbsp;<\/p>\n<p># your MySQL server\u2019s name<br \/>\nSERVER=test.net<\/p>\n<p># directory to backup to<br \/>\nBACKDIR=~\/backups<\/p>\n<p># date format that is appended to filename<br \/>\nDATE=`date +\u2019%m-%d-%Y\u2019`<\/p>\n<p>#\u2014\u2014\u2014\u2014\u2014\u2014\u2014-MySQL Settings\u2014\u2014\u2014\u2014\u2014\u2014\u2013#<\/p>\n<p># your MySQL server\u2019s location (IP address is best)<br \/>\nHOST=localhost<\/p>\n<p># MySQL username<br \/>\nUSER=username<\/p>\n<p># MySQL password<br \/>\nPASS=password<\/p>\n<p># List all of the MySQL databases that you want to backup in here,<br \/>\n# each separated by a space<br \/>\nDBS=\u201ddb1 db2?<\/p>\n<p># set to \u2018y\u2019 if you want to backup all your databases. this will override<br \/>\n# the database selection above.<br \/>\nDUMPALL=y<\/p>\n<p>#\u2014\u2014\u2014\u2014\u2014\u2014\u2014-Mail Settings\u2014\u2014\u2014\u2014\u2014\u2014\u2013#<\/p>\n<p># set to \u2018y\u2019 if you\u2019d like to be emailed the backup (requires mutt)<br \/>\nMAIL=y<\/p>\n<p># email addresses to send backups to, separated by a space<br \/>\nEMAILS=\u201d1@gmail.com 2@inbox.com 3@goowy.com\u201d<\/p>\n<p>SUBJECT=\u201dMySQL backup on $SERVER ($DATE)\u201d<\/p>\n<p>#\u2014\u2014\u2014\u2014\u2014\u2014\u2014-FTP Settings\u2014\u2014\u2014\u2014\u2014\u2014\u2013#<\/p>\n<p># set \u201cFTP=y\u201d if you want to enable FTP backups<br \/>\nFTP=n<\/p>\n<p># FTP server settings; should be self-explanatory<br \/>\nFTPHOST=\u201dftp.server.com\u201d<br \/>\nFTPUSER=\u201dusername\u201d<br \/>\nFTPPASS=\u201dpass\u201d<\/p>\n<p># directory to backup to. if it doesn\u2019t exist, file will be uploaded to<br \/>\n# first logged-in directory<br \/>\nFTPDIR=\u201dbackups\u201d<\/p>\n<p>#\u2014\u2014\u2014\u2014\u2014\u2014-Deletion Settings\u2014\u2014\u2014\u2014\u2014\u2014-#<\/p>\n<p># delete old files?<br \/>\nDELETE=y<\/p>\n<p># how many days of backups do you want to keep?<br \/>\nDAYS=3<\/p>\n<p>#\u2014\u2014\u2014\u2014\u2014\u2014\u2014-End of Settings\u2014\u2014\u2014\u2014\u2014\u2014#<\/p>\n<p># check of the backup directory exists<br \/>\n# if not, create it<br \/>\nif [ -e $BACKDIR ]<br \/>\nthen<br \/>\necho Backups directory already exists<br \/>\nelse<br \/>\nmkdir $BACKDIR<br \/>\nfi<\/p>\n<p>if [ $DUMPALL = &#8220;y&#8221; ]<br \/>\nthen<br \/>\necho \u201cCreating list of all your databases\u2026\u201d<\/p>\n<p>mysql -h $HOST \u2013user=$USER \u2013password=$PASS -e \u201cshow databases;\u201d &gt; dbs_on_$SERVER.txt<\/p>\n<p># redefine list of databases to be backed up<br \/>\nDBS=`sed -e \u2018:a;N;$!ba;s\/\\\\n\/ \/g\u2019 -e \u2018s\/Database \/\/g\u2019 dbs_on_$SERVER.txt`<br \/>\nfi<\/p>\n<p>echo \u201cBacking up MySQL databases\u2026\u201d<br \/>\nfor database in $DBS<br \/>\ndo<br \/>\nmysqldump -h $HOST \u2013user=$USER \u2013password=$PASS $database &gt; \\\\<br \/>\n$BACKDIR\/$SERVER-mysqlbackup-$database-$DATE.sql<br \/>\ngzip -f -9 $BACKDIR\/$SERVER-mysqlbackup-$database-$DATE.sql<br \/>\ndone<\/p>\n<p># if you have the mail program \u2018mutt\u2019 installed on<br \/>\n# your server, this script will have mutt attach the backup<br \/>\n# and send it to the email addresses in $EMAILS<\/p>\n<p>if [ $MAIL = &#8220;y&#8221; ]<br \/>\nthen<br \/>\nBODY=\u201dYour backup is ready! Find more useful scripts and info at http:\/\/www.ameir.net\u201d<br \/>\nATTACH=`for file in $BACKDIR\/*$DATE.sql.gz; do echo -n \u201c-a ${file} \u201c; done`<\/p>\n<p>echo \u201c$BODY\u201d | mutt -s \u201c$SUBJECT\u201d $ATTACH $EMAILS<\/p>\n<p>echo -e \u201cYour backup has been emailed to you! \\n\u201d<br \/>\nfi<\/p>\n<p>if [ $FTP = &#8220;y&#8221; ]<br \/>\nthen<br \/>\necho \u201cInitiating FTP connection\u2026\u201d<br \/>\ncd $BACKDIR<br \/>\nATTACH=`for file in *$DATE.sql.gz; do echo -n -e \u201cput ${file}\\n\u201d; done`<\/p>\n<p>ftp -nv &lt; open $FTPHOST<br \/>\nuser $FTPUSER $FTPPASS<br \/>\ncd $FTPDIR<br \/>\n$ATTACH<br \/>\nquit<br \/>\nEOF<br \/>\necho -e \u201cFTP transfer complete! \\n\u201d<br \/>\nfi<\/p>\n<p>if [ $DELETE = &#8220;y&#8221; ]<br \/>\nthen<br \/>\nfind $BACKDIR -name \u201c*.sql.gz\u201d -mtime $DAYS -exec rm {} \\\\;<\/p>\n<p>if [ $DAYS = &#8220;1&#8221; ]<br \/>\nthen<br \/>\necho \u201cYesterday\u2019s backup has been deleted.\u201d<br \/>\nelse<br \/>\necho \u201cThe backup from $DAYS days ago has been deleted.\u201d<br \/>\nfi<br \/>\nfi<\/p>\n","protected":false},"excerpt":{"rendered":"<p>#! \/bin\/bash<\/p>\n<p>&nbsp;<\/p>\n<p># your MySQL server\u2019s name SERVER=test.net<\/p>\n<p># directory to backup to BACKDIR=~\/backups<\/p>\n<p># date format that is appended to filename DATE=`date +\u2019%m-%d-%Y\u2019`<\/p>\n<p>#\u2014\u2014\u2014\u2014\u2014\u2014\u2014-MySQL Settings\u2014\u2014\u2014\u2014\u2014\u2014\u2013#<\/p>\n<p># your MySQL server\u2019s location (IP address is best) HOST=localhost<\/p>\n<p># MySQL username USER=username<\/p>\n<p># MySQL password PASS=password<\/p>\n<p># List all of the MySQL databases that you want [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/2191"}],"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=2191"}],"version-history":[{"count":3,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/2191\/revisions"}],"predecessor-version":[{"id":2194,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/2191\/revisions\/2194"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}