{"id":205,"date":"2004-07-07T13:41:08","date_gmt":"2004-07-07T13:41:08","guid":{"rendered":"http:\/\/fiveforks.com\/jeb\/2004\/07\/automysqlbackup_1\/"},"modified":"2004-07-07T13:41:08","modified_gmt":"2004-07-07T13:41:08","slug":"automysqlbackup_1","status":"publish","type":"post","link":"https:\/\/www.fiveforks.com\/jeb\/2004\/07\/automysqlbackup_1\/","title":{"rendered":"AutoMySQLbackup"},"content":{"rendered":"<p>This one worked for me. It is now sitting in \/private\/etc\/periodic\/daily\/ with other daily scripts being run by cron. Had to add the path to the MYSQLDUMP utility in this line:<\/p>\n<p>PATH=\/usr\/local\/bin:\/usr\/bin:\/bin:\/usr\/local\/mysql\/bin<\/p>\n<p><a title=\"Automatic MySQL Backup\" href=\"http:\/\/members.lycos.co.uk\/wipe_out\/automysqlbackup\/\">Automatic MySQL Backup<\/a><\/p>\n<p><!--more--><br \/>\nHere is the script:<\/p>\n<p>#!\/bin\/bash<\/p>\n<p>#<\/p>\n<p># MySQL Backup Script<\/p>\n<p># VER. 1.9 &#8211; http:\/\/sourceforge.net\/projects\/automysqlbackup\/<\/p>\n<p># Copyright (c) 2002-2003 wipe_out@lycos.co.uk<\/p>\n<p>#<\/p>\n<p># This program is free software; you can redistribute it and\/or modify<\/p>\n<p># it under the terms of the GNU General Public License as published by<\/p>\n<p># the Free Software Foundation; either version 2 of the License, or<\/p>\n<p># (at your option) any later version.<\/p>\n<p>#<\/p>\n<p># This program is distributed in the hope that it will be useful,<\/p>\n<p># but WITHOUT ANY WARRANTY; without even the implied warranty of<\/p>\n<p># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the<\/p>\n<p># GNU General Public License for more details.<\/p>\n<p>#<\/p>\n<p># You should have received a copy of the GNU General Public License<\/p>\n<p># along with this program; if not, write to the Free Software<\/p>\n<p># Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA<\/p>\n<p>#<\/p>\n<p>#=====================================================================<\/p>\n<p>#=====================================================================<\/p>\n<p># Set the following variables to your system needs<\/p>\n<p># (Detailed instructions below variables)<\/p>\n<p>#=====================================================================<\/p>\n<p># Username to access the MySQL server e.g. dbuser<\/p>\n<p>USERNAME=root<\/p>\n<p># Username to access the MySQL server e.g. password<\/p>\n<p>PASSWORD=H***** (masked)<\/p>\n<p># Host name (or IP address) of MySQL server e.g localhost<\/p>\n<p>DBHOST=localhost<\/p>\n<p># List of DBNAMES for Daily\/Weekly Backup e.g. &#8220;DB1 DB2 DB3&#8243;<\/p>\n<p># DBNAMES=&#8221;DB1 DB2 DB3&#8243;<\/p>\n<p>DBNAMES=&#8221;mov_type&#8221;<\/p>\n<p># Backup directory location e.g \/backups<\/p>\n<p>BACKUPDIR=&#8221;\/backups&#8221;<\/p>\n<p># Mail backup log? (yes or no)<\/p>\n<p>MAILLOG=no<\/p>\n<p># Email Address to send log to? (user@domain.com)<\/p>\n<p>MAILADDR=&#8221;my@mail.com&#8221;<\/p>\n<p># ============================================================<\/p>\n<p># === ADVANCED OPTIONS ( Read the doc&#8217;s below for details )===<\/p>\n<p>#=============================================================<\/p>\n<p># List of DBBNAMES for Monthly Backups.<\/p>\n<p>MDBNAMES=&#8221;mysql $DBNAMES&#8221;<\/p>\n<p># List of DBNAMES to EXLUCDE if DBNAMES are set to all (must be in &#8221; quotes)<\/p>\n<p>DBEXCLUDE=&#8221;&#8221;<\/p>\n<p># Include CREATE DATABASE in backup?<\/p>\n<p>CREATE_DATABASE=yes<\/p>\n<p># Separate backup directory and file for each DB? (yes or no)<\/p>\n<p>SEPDIR=yes<\/p>\n<p># Which day do you want weekly backups? (1 to 7 where 1 is Monday)<\/p>\n<p>DOWEEKLY=6<\/p>\n<p># Command to run before backups (uncomment to use)<\/p>\n<p>#PREBACKUP=&#8221;\/etc\/mysql-backup-pre&#8221;<\/p>\n<p># Command run after backups (uncomment to use)<\/p>\n<p>#POSTBACKUP=&#8221;\/etc\/mysql-backup-post&#8221;<\/p>\n<p>#=====================================================================<\/p>\n<p># Options documantation<\/p>\n<p>#=====================================================================<\/p>\n<p># Set USERNAME and PASSWORD of a user that has at least SELECT permission<\/p>\n<p># to ALL databases.<\/p>\n<p>#<\/p>\n<p># Set the DBHOST option to the server you wish to backup, leave the<\/p>\n<p># default to backup &#8220;this server&#8221;.(to backup multiple servers make<\/p>\n<p># copies of this file and set the options for that server)<\/p>\n<p>#<\/p>\n<p># Put in the list of DBNAMES(Databases)to be backed up. If you would like<\/p>\n<p># to backup ALL DBs on the server set DBNAMES=&#8221;all&#8221;.(if set to &#8220;all&#8221; then<\/p>\n<p># any new DBs will automatically be backed up without needing to modify<\/p>\n<p># this backup script when a new DB is created).<\/p>\n<p>#<\/p>\n<p># If the DB you want to backup has a space in the name replace the space<\/p>\n<p># with a % e.g. &#8220;data base&#8221; will become &#8220;data%base&#8221;<\/p>\n<p># NOTE: Spaces in DB names may not work correctly when SEPDIR=no.<\/p>\n<p>#<\/p>\n<p># You can change the backup storage location from \/backups to anything<\/p>\n<p># you like by using the BACKUPDIR setting..<\/p>\n<p>#<\/p>\n<p># The MAILLOG and MAILADDR options and pretty self explanitory, use<\/p>\n<p># these to have the backup log mailed to you at any email address or multiple<\/p>\n<p># email addresses in a space seperated list.<\/p>\n<p># (this will require that you are permitted to run the &#8220;mail&#8221; program<\/p>\n<p># on your server.)<\/p>\n<p>#<\/p>\n<p># Finally copy automysqlbackup.sh to anywhere on your server and make sure<\/p>\n<p># to set executable permission. You can also copy the script to<\/p>\n<p># \/etc\/cron.daily to have it execute automatically every night or simply<\/p>\n<p># place a symlink in \/etc\/cron.daily to the file if you wish to keep it<\/p>\n<p># somwhere else.<\/p>\n<p># NOTE:On Debian copy the file with no extention for it to be run<\/p>\n<p># by cron e.g just name the file &#8220;automysqlbackup&#8221;<\/p>\n<p>#<\/p>\n<p># Thats it..<\/p>\n<p>#<\/p>\n<p>#<\/p>\n<p># === Advanced options doc&#8217;s ===<\/p>\n<p>#<\/p>\n<p># The list of MDBNAMES is the DB&#8217;s to be backed up only monthly. You should<\/p>\n<p># always include &#8220;mysql&#8221; in this list to backup your user\/password<\/p>\n<p># information along with any other DBs that you only feel need to<\/p>\n<p># be backed up monthly. (if using a hosted server then you should<\/p>\n<p># probably remove &#8220;mysql&#8221; as your provider will be backing this up)<\/p>\n<p># NOTE: If DBNAMES=&#8221;all&#8221; then MDBNAMES has no effect as all DBs will be backed<\/p>\n<p># up anyway.<\/p>\n<p>#<\/p>\n<p># If you set DBNAMES=&#8221;all&#8221; you can configure the option DBEXCLUDE. Other<\/p>\n<p># wise this option will not be used.<\/p>\n<p># This option can be used if you want to backup all dbs, but you want<\/p>\n<p># exclude some of them. (eg. a db is to big).<\/p>\n<p>#<\/p>\n<p># Set CREATE_DATABASE to &#8220;yes&#8221; (the default) if you want your SQL-Dump to create<\/p>\n<p># a database with the same name as the original database when restoring.<\/p>\n<p># Saying &#8220;no&#8221; here will allow your to specify the database name you want to<\/p>\n<p># restore your dump into, making a copy of the database by using the dump<\/p>\n<p># created with automysqlbackup.<\/p>\n<p># NOTE: Not used if SEPDIR=no<\/p>\n<p>#<\/p>\n<p># The SEPDIR option allows you to choose to have all DBs backed up to<\/p>\n<p># a single file (fast restore of entire server in case of crash) or to<\/p>\n<p># seperate directories for each DB (each DB can be restored seperately<\/p>\n<p># in case of single DB corruption or loss).<\/p>\n<p>#<\/p>\n<p># To set the day of the week that you would like the weekly backup to happen<\/p>\n<p># set the DOWEEKLY setting, this can be a value from 1 to 7 where 1 is Monday,<\/p>\n<p># The default is 6 which means that weekly backups are done on a Saturday.<\/p>\n<p>#<\/p>\n<p># Use PREBACKUP and POSTBACKUP to specify Per and Post backup commands<\/p>\n<p># or scripts to perform tasks either before or after the backup process.<\/p>\n<p>#<\/p>\n<p>#<\/p>\n<p>#=====================================================================<\/p>\n<p># Backup Rotation..<\/p>\n<p>#=====================================================================<\/p>\n<p>#<\/p>\n<p># Daily Backups are rotated weekly..<\/p>\n<p># Weekly Backups are run by default on Saturday Morning when<\/p>\n<p># cron.daily scripts are run&#8230;Can be changed with DOWEEKLY setting..<\/p>\n<p># Weekly Backups are rotated on a 5 week cycle..<\/p>\n<p># Monthly Backups are run on the 1st of the month..<\/p>\n<p># Monthly Backups are NOT rotated automatically&#8230;<\/p>\n<p># It may be a good idea to copy Monthly backups offline or to another<\/p>\n<p># server..<\/p>\n<p>#<\/p>\n<p>#=====================================================================<\/p>\n<p># Please Note!!<\/p>\n<p>#=====================================================================<\/p>\n<p>#<\/p>\n<p># I take no resposibility for any data loss or corruption when using<\/p>\n<p># this script..<\/p>\n<p># This script will not help in the event of a hard drive crash. If a<\/p>\n<p># copy of the backup has not be stored offline or on another PC..<\/p>\n<p># You should copy your backups offline regularly for best protection.<\/p>\n<p>#<\/p>\n<p># Happy backing up&#8230;<\/p>\n<p>#<\/p>\n<p>#=====================================================================<\/p>\n<p># Restoring<\/p>\n<p>#=====================================================================<\/p>\n<p># Firstly you will need to uncompress the backup file.<\/p>\n<p># eg.<\/p>\n<p># ungzip file.gz<\/p>\n<p>#<\/p>\n<p># Next you will need to use the mysql client to restore the DB from the<\/p>\n<p># sql file.<\/p>\n<p># eg.<\/p>\n<p># mysql &#8211;user=username &#8211;pass=password &#8211;host=dbserver database &lt; \/path\/file.sql<\/p>\n<p># or<\/p>\n<p># mysql &#8211;user=username &#8211;pass=password &#8211;host=dbserver -e &quot;source \/path\/file.sql&quot; database<\/p>\n<p>#<\/p>\n<p># NOTE: Make sure you use &quot;&#8221; in the above command because<\/p>\n<p># you are piping the file.sql to mysql and not the other way around.<\/p>\n<p>#<\/p>\n<p># Lets hope you never have to use this.. \ud83d\ude42<\/p>\n<p>#<\/p>\n<p>#=====================================================================<\/p>\n<p># Change Log<\/p>\n<p>#=====================================================================<\/p>\n<p>#<\/p>\n<p># VER 1.9 &#8211; (2004-05-25)<\/p>\n<p>#    Small bug fix to handle spaces in LOGFILE path which contains spaces (reported by Thomas von Eyben)<\/p>\n<p>#    Updated docs to mention that Log email can be sent to multiple email addresses.<\/p>\n<p># VER 1.8 &#8211; (2004-05-01)<\/p>\n<p>#    Added option to make backups restorable to alternate database names<\/p>\n<p>#    meaning that a copy of the database can be created (Based on patch by Rene Hoffmann)<\/p>\n<p>#    Seperated options into standard and advanced.<\/p>\n<p>#    Removed &#8221; from single file dump DBMANES because it caused an error but<\/p>\n<p>#    this means that if DB&#8217;s have spaces in the name they will not dump when SEPDIR=no.<\/p>\n<p>#    Added -p option to mkdir commands to create multiple subdirs without error.<\/p>\n<p>#    Added disk usage and location to the bottom of the backup report.<\/p>\n<p># VER 1.7 &#8211; (2004-04-22)<\/p>\n<p>#    Fixed an issue where weelky backups would only work correctly if server<\/p>\n<p>#    locale was set to English (issue reported by Tom Ingberg)<\/p>\n<p>#    used &#8220;eval&#8221; for &#8220;rm&#8221; commands to try and resolve rotation issues.<\/p>\n<p>#    Changed name of status log so multiple scripts can be run at the same time.<\/p>\n<p># VER 1.6 &#8211; (2004-03-14)<\/p>\n<p>#   Added PREBACKUP and POSTBACKUP command functions. (patch by markpustjens)<\/p>\n<p>#   Added support for backing up DB&#8217;s with Spaces in the name.<\/p>\n<p>#   (patch by markpustjens)<\/p>\n<p># VER 1.5 &#8211; (2004-02-24)<\/p>\n<p>#   Added the ability to exclude DB&#8217;s when the &#8220;all&#8221; option is used.<\/p>\n<p>#   (Patch by kampftitan)<\/p>\n<p># VER 1.4 &#8211; (2004-02-02)<\/p>\n<p>#   Project moved to Sourceforge.net<\/p>\n<p># VER 1.3 &#8211; (2003-09-25)<\/p>\n<p>#   Added support for backing up &#8220;all&#8221; databases on the server without<\/p>\n<p>#    having to list each one seperately in the configuration.<\/p>\n<p>#   Added DB restore instructions.<\/p>\n<p># VER 1.2 &#8211; (2003-03-16)<\/p>\n<p>#   Added server name to the backup log so logs from multiple servers<\/p>\n<p>#   can be easily identified.<\/p>\n<p># VER 1.1 &#8211; (2003-03-13)<\/p>\n<p>#   Small Bug fix in monthly report. (Thanks Stoyanski)<\/p>\n<p>#   Added option to email log to any email address. (Inspired by Stoyanski)<\/p>\n<p>#   Changed Standard file name to .sh extention.<\/p>\n<p>#   Option are set using yes and no rather than 1 or 0.<\/p>\n<p># VER 1.0 &#8211; (2003-01-30)<\/p>\n<p>#   Added the ability to have all databases backup to a single dump<\/p>\n<p>#   file or seperate directory and file for each database.<\/p>\n<p>#   Output is better for log keeping.<\/p>\n<p># VER 0.6 &#8211; (2003-01-22)<\/p>\n<p>#   Bug fix for daily directory (Added in VER 0.5) rotation.<\/p>\n<p># VER 0.5 &#8211; (2003-01-20)<\/p>\n<p>#   Added &#8220;daily&#8221; directory for daily backups for neatness (suggestion by Jason)<\/p>\n<p>#   Added DBHOST option to allow backing up a remote server (Suggestion by Jason)<\/p>\n<p>#   Added &#8220;&#8211;quote-names&#8221; option to mysqldump command.<\/p>\n<p>#   Bug fix for handling the last and first of the year week rotation.<\/p>\n<p># VER 0.4 &#8211; (2002-11-06)<\/p>\n<p>#   Added the abaility for the script to create its own directory structure.<\/p>\n<p># VER 0.3 &#8211; (2002-10-01)<\/p>\n<p>#   Changed Naming of Weekly backups so they will show in order.<\/p>\n<p># VER 0.2 &#8211; (2002-09-27)<\/p>\n<p>#   Corrected weekly rotation logic to handle weeks 0 &#8211; 10<\/p>\n<p># VER 0.1 &#8211; (2002-09-21)<\/p>\n<p>#   Initial Release<\/p>\n<p>#<\/p>\n<p>#=====================================================================<\/p>\n<p>#=====================================================================<\/p>\n<p>#=====================================================================<\/p>\n<p>#<\/p>\n<p># Should not need to be modified from here down!!<\/p>\n<p>#<\/p>\n<p>#=====================================================================<\/p>\n<p>#=====================================================================<\/p>\n<p>#=====================================================================<\/p>\n<p>#PATH=\/usr\/local\/bin:\/usr\/bin:\/bin<\/p>\n<p>PATH=\/usr\/local\/bin:\/usr\/bin:\/bin:\/usr\/local\/mysql\/bin<\/p>\n<p>DATE=`date +%Y-%m-%d`\t\t\t# Datestamp e.g 2002-09-21<\/p>\n<p>DOW=`date +%A`\t\t\t\t# Day of the week e.g. Monday<\/p>\n<p>DNOW=`date +%u`\t\t\t\t# Day number of the week 1 to 7 where 1 represents Monday<\/p>\n<p>DOM=`date +%d`\t\t\t\t# Date of the Month e.g. 27<\/p>\n<p>M=`date +%B`\t\t\t\t# Month e.g January<\/p>\n<p>W=`date +%V`\t\t\t\t# Week Number e.g 37<\/p>\n<p>VER=1.9\t\t\t\t\t# Version Number<\/p>\n<p>LOGFILE=\/$DBHOST-`date +%N`.log\t\t# Logfile Name<\/p>\n<p>OPT=&#8221;&#8211;quote-names &#8211;opt&#8221;\t\t# OPT string for use with mysqldump ( see man mysqldump )<\/p>\n<p># Run command before we begin<\/p>\n<p>if [ &#8220;$PREBACKUP&#8221; ]<\/p>\n<p>then<\/p>\n<p>$PREBACKUP<\/p>\n<p>fi<\/p>\n<p># Create required directories<\/p>\n<p>if [ ! -e &#8220;$BACKUPDIR&#8221; ]\t\t# Check Backup Directory exists.<\/p>\n<p>then<\/p>\n<p>mkdir -p &#8220;$BACKUPDIR&#8221;<\/p>\n<p>fi<\/p>\n<p>if [ ! -e &#8220;$BACKUPDIR\/daily&#8221; ]\t\t# Check Daily Directory exists.<\/p>\n<p>then<\/p>\n<p>mkdir -p &#8220;$BACKUPDIR\/daily&#8221;<\/p>\n<p>fi<\/p>\n<p>if [ ! -e &#8220;$BACKUPDIR\/weekly&#8221; ]\t\t# Check Weekly Directory exists.<\/p>\n<p>then<\/p>\n<p>mkdir -p &#8220;$BACKUPDIR\/weekly&#8221;<\/p>\n<p>fi<\/p>\n<p>if [ ! -e &#8220;$BACKUPDIR\/monthly&#8221; ]\t# Check Monthly Directory exists.<\/p>\n<p>then<\/p>\n<p>mkdir -p &#8220;$BACKUPDIR\/monthly&#8221;<\/p>\n<p>fi<\/p>\n<p>if [ &#8220;$SEPDIR&#8221; = &#8220;yes&#8221; ]; then # Check if CREATE DATABSE should be included in Dump<\/p>\n<p>if [ &#8220;$CREATE_DATABASE&#8221; = &#8220;no&#8221; ]; then<\/p>\n<p>OPT=&#8221;$OPT &#8211;no-create-db&#8221;<\/p>\n<p>else<\/p>\n<p>OPT=&#8221;$OPT &#8211;databases&#8221;<\/p>\n<p>fi<\/p>\n<p>else<\/p>\n<p>OPT=&#8221;$OPT &#8211;databases&#8221;<\/p>\n<p>fi<\/p>\n<p># Hostname for LOG information<\/p>\n<p>if [ &#8220;$DBHOST&#8221; = &#8220;localhost&#8221; ]; then<\/p>\n<p>HOST=`hostname`<\/p>\n<p>else<\/p>\n<p>HOST=$DBHOST<\/p>\n<p>fi<\/p>\n<p># If backing up all DBs on the server<\/p>\n<p>if [ &#8220;$DBNAMES&#8221; = &#8220;all&#8221; ]; then<\/p>\n<p>DBNAMES=&#8221;`mysql &#8211;user=$USERNAME &#8211;password=$PASSWORD &#8211;host=$DBHOST &#8211;batch -N -e &#8220;show databases&#8221;| sed &#8216;s\/ \/%\/g&#8217;`&#8221;<\/p>\n<p># If DBs are excluded<\/p>\n<p>for exclude in $DBEXCLUDE<\/p>\n<p>do<\/p>\n<p>DBNAMES=`echo $DBNAMES | sed &#8220;s\/\\b$exclude\\b\/\/g&#8221;`<\/p>\n<p>done<\/p>\n<p>MDBNAMES=$DBNAMES<\/p>\n<p>fi<\/p>\n<p>echo ======================================================================&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>echo AutoMySQLBackup VER $VER&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>echo http:\/\/sourceforge.net\/projects\/automysqlbackup\/&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>echo &gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>echo Backup of Database Server &#8211; $HOST&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>echo ======================================================================&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p># Test is seperate DB backups are required<\/p>\n<p>if [ &#8220;$SEPDIR&#8221; = &#8220;yes&#8221; ]; then<\/p>\n<p>echo Backup Start Time `date`&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>echo ======================================================================&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p># Monthly Full Backup of all Databases<\/p>\n<p>if [ $DOM = &#8220;01&#8221; ]; then<\/p>\n<p>for MDB in $MDBNAMES<\/p>\n<p>do<\/p>\n<p># Prepare $DB for using<\/p>\n<p>MDB=&#8221;`echo $MDB | sed &#8216;s\/%\/ \/g&#8217;`&#8221;<\/p>\n<p>if [ ! -e &#8220;$BACKUPDIR\/monthly\/$MDB&#8221; ]\t\t# Check Monthly DB Directory exists.<\/p>\n<p>then<\/p>\n<p>mkdir -p &#8220;$BACKUPDIR\/monthly\/$MDB&#8221;<\/p>\n<p>fi<\/p>\n<p>echo Monthly Backup of $MDB&#8230;&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>mysqldump &#8211;user=$USERNAME &#8211;password=$PASSWORD &#8211;host=$DBHOST $OPT &#8220;$MDB&#8221; &gt; &#8220;$BACKUPDIR\/monthly\/$MDB\/$DATE.$M.$MDB.sql&#8221;<\/p>\n<p>gzip -f &#8220;$BACKUPDIR\/monthly\/$MDB\/$DATE.$M.$MDB.sql&#8221;<\/p>\n<p>echo&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>echo Backup Information for $BACKUPDIR\/monthly\/$MDB\/$DATE.$M.$MDB.sql.gz&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>gzip -l &#8220;$BACKUPDIR\/monthly\/$MDB\/$DATE.$M.$MDB.sql.gz&#8221; &gt;&gt; &#8220;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>echo &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>done<\/p>\n<p>fi<\/p>\n<p>for DB in $DBNAMES<\/p>\n<p>do<\/p>\n<p># Prepare $DB for using<\/p>\n<p>DB=&#8221;`echo $DB | sed &#8216;s\/%\/ \/g&#8217;`&#8221;<\/p>\n<p># Create Seperate directory for each DB<\/p>\n<p>if [ ! -e &#8220;$BACKUPDIR\/daily\/$DB&#8221; ]\t\t# Check Daily DB Directory exists.<\/p>\n<p>then<\/p>\n<p>mkdir -p &#8220;$BACKUPDIR\/daily\/$DB&#8221;<\/p>\n<p>fi<\/p>\n<p>if [ ! -e &#8220;$BACKUPDIR\/weekly\/$DB&#8221; ]\t\t# Check Weekly DB Directory exists.<\/p>\n<p>then<\/p>\n<p>mkdir -p &#8220;$BACKUPDIR\/weekly\/$DB&#8221;<\/p>\n<p>fi<\/p>\n<p># Weekly Backup<\/p>\n<p>if [ $DNOW = $DOWEEKLY ]; then<\/p>\n<p>echo Weekly Backup of Database \\( $DB \\)&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>echo Rotating 5 weeks Backups&#8230;&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>if [ &#8220;$W&#8221; -le 05 ];then<\/p>\n<p>REMW=`expr 48 + $W`<\/p>\n<p>elif [ &#8220;$W&#8221; -lt 15 ];then<\/p>\n<p>REMW=0`expr $W &#8211; 5`<\/p>\n<p>else<\/p>\n<p>REMW=`expr $W &#8211; 5`<\/p>\n<p>fi<\/p>\n<p>eval rm -fv &#8220;$BACKUPDIR\/weekly\/$DB\/week.$REMW.*&#8221; &gt;&gt; &#8220;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>echo&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>mysqldump &#8211;user=$USERNAME &#8211;password=$PASSWORD &#8211;host=$DBHOST $OPT &#8220;$DB&#8221; &gt; &#8220;$BACKUPDIR\/weekly\/$DB\/week.$W.$DATE.sql&#8221;<\/p>\n<p>gzip -f &#8220;$BACKUPDIR\/weekly\/$DB\/week.$W.$DATE.sql&#8221;<\/p>\n<p>echo Backup Information for $BACKUPDIR\/weekly\/$DB\/week.$W.$DATE.sql.gz&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>gzip -l &#8220;$BACKUPDIR\/weekly\/$DB\/week.$W.$DATE.sql.gz&#8221; &gt;&gt; &#8220;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>echo &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p># Daily Backup<\/p>\n<p>else<\/p>\n<p>echo Daily Backup of Database \\( $DB \\)&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>echo Rotating last weeks Backup&#8230;&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>eval rm -fv &#8220;$BACKUPDIR\/daily\/$DB\/*.$DOW.sql.gz&#8221; &gt;&gt; &#8220;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>echo&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>mysqldump &#8211;user=$USERNAME &#8211;password=$PASSWORD &#8211;host=$DBHOST $OPT &#8220;$DB&#8221; &gt; &#8220;$BACKUPDIR\/daily\/$DB\/$DATE.$DOW.sql&#8221;<\/p>\n<p>gzip -f &#8220;$BACKUPDIR\/daily\/$DB\/$DATE.$DOW.sql&#8221;<\/p>\n<p>echo Backup Information for $BACKUPDIR\/daily\/$DB\/$DATE.$DOW.sql.gz&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>gzip -l &#8220;$BACKUPDIR\/daily\/$DB\/$DATE.$DOW.sql.gz&#8221; &gt;&gt; &#8220;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>echo &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>fi<\/p>\n<p>done<\/p>\n<p>echo Backup End `date`&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>echo ======================================================================&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>else # One backup file for all DBs<\/p>\n<p>echo Backup Start `date`&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>echo ======================================================================&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p># Monthly Full Backup of all Databases<\/p>\n<p>if [ $DOM = &#8220;01&#8221; ]; then<\/p>\n<p>echo Monthly full Backup of \\( $MDBNAMES \\)&#8230;&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>mysqldump &#8211;user=$USERNAME &#8211;password=$PASSWORD &#8211;host=$DBHOST $OPT $MDBNAMES &gt; &#8220;$BACKUPDIR\/monthly\/$DATE.$M.all-databases.sql&#8221;<\/p>\n<p>gzip -f &#8220;$BACKUPDIR\/monthly\/$DATE.$M.all-databases.sql&#8221;<\/p>\n<p>echo&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>echo Backup Information for $BACKUPDIR\/monthly\/$DATE.$M.all-databases.sql.gz&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>gzip -l &#8220;$BACKUPDIR\/monthly\/$DATE.$M.all-databases.sql.gz&#8221; &gt;&gt; &#8220;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>echo &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>fi<\/p>\n<p># Weekly Backup<\/p>\n<p>if [ $DNOW = $DOWEEKLY ]; then<\/p>\n<p>echo Weekly Backup of Databases \\( $DBNAMES \\)&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>echo&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>echo Rotating 5 weeks Backups&#8230;&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>if [ &#8220;$W&#8221; -le 05 ];then<\/p>\n<p>REMW=`expr 48 + $W`<\/p>\n<p>elif [ &#8220;$W&#8221; -lt 15 ];then<\/p>\n<p>REMW=0`expr $W &#8211; 5`<\/p>\n<p>else<\/p>\n<p>REMW=`expr $W &#8211; 5`<\/p>\n<p>fi<\/p>\n<p>eval rm -fv &#8220;$BACKUPDIR\/weekly\/week.$REMW.*&#8221; &gt;&gt; &#8220;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>echo&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>mysqldump &#8211;user=$USERNAME &#8211;password=$PASSWORD &#8211;host=$DBHOST $OPT $DBNAMES &gt; &#8220;$BACKUPDIR\/weekly\/week.$W.$DATE.sql&#8221;<\/p>\n<p>gzip -f &#8220;$BACKUPDIR\/weekly\/week.$W.$DATE.sql&#8221;<\/p>\n<p>echo Backup Information for $BACKUPDIR\/weekly\/week.$W.$DATE.sql.gz&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>gzip -l &#8220;$BACKUPDIR\/weekly\/week.$W.$DATE.sql.gz&#8221; &gt;&gt; &#8220;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>echo &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p># Daily Backup<\/p>\n<p>else<\/p>\n<p>echo Daily Backup of Databases \\( $DBNAMES \\)&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>echo&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>echo Rotating last weeks Backup&#8230;&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>eval rm -fv &#8220;$BACKUPDIR\/daily\/*.$DOW.sql.gz&#8221; &gt;&gt; &#8220;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>echo&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>mysqldump &#8211;user=$USERNAME &#8211;password=$PASSWORD &#8211;host=$DBHOST $OPT $DBNAMES &gt; &#8220;$BACKUPDIR\/daily\/$DATE.$DOW.sql&#8221;<\/p>\n<p>gzip -f &#8220;$BACKUPDIR\/daily\/$DATE.$DOW.sql&#8221;<\/p>\n<p>echo Backup Information for $BACKUPDIR\/daily\/$DATE.$DOW.sql.gz&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>gzip -l &#8220;$BACKUPDIR\/daily\/$DATE.$DOW.sql.gz&#8221; &gt;&gt; &#8220;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>echo &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>fi<\/p>\n<p>echo Backup End Time `date`&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>echo ======================================================================&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>fi<\/p>\n<p>echo Total disk space used for backup storage..&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>echo Size &#8211; Location&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>echo `du -hs $BACKUPDIR`&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>echo&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>echo ======================================================================&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>echo If you find AutoMySQLBackup valuable please make a donation at&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>echo http:\/\/sourceforge.net\/project\/project_donations.php?group_id=101066&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>echo ======================================================================&gt;&gt;&#8221;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>if [ &#8220;$MAILLOG&#8221; = &#8220;yes&#8221; ]<\/p>\n<p>then<\/p>\n<p>cat &#8220;$BACKUPDIR\/$LOGFILE&#8221; | mail -s &#8220;MySQL Backup Log for $HOST &#8211; $DATE&#8221; $MAILADDR<\/p>\n<p>else<\/p>\n<p>cat &#8220;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p>fi<\/p>\n<p># Clean up Logfile<\/p>\n<p>eval rm -f &#8220;$BACKUPDIR\/$LOGFILE&#8221;<\/p>\n<p># Run command when we&#8217;re done<\/p>\n<p>if [ &#8220;$POSTBACKUP&#8221; ]<\/p>\n<p>then<\/p>\n<p>$POSTBACKUP<\/p>\n<p>fi<\/p>\n<p>exit 0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This one worked for me. It is now sitting in \/private\/etc\/periodic\/daily\/ with other daily scripts being run by cron. Had to add the path to the MYSQLDUMP utility in this line: PATH=\/usr\/local\/bin:\/usr\/bin:\/bin:\/usr\/local\/mysql\/bin Automatic MySQL Backup<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-205","post","type-post","status-publish","format-standard","hentry","category-tech"],"_links":{"self":[{"href":"https:\/\/www.fiveforks.com\/jeb\/wp-json\/wp\/v2\/posts\/205","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.fiveforks.com\/jeb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fiveforks.com\/jeb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fiveforks.com\/jeb\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fiveforks.com\/jeb\/wp-json\/wp\/v2\/comments?post=205"}],"version-history":[{"count":0,"href":"https:\/\/www.fiveforks.com\/jeb\/wp-json\/wp\/v2\/posts\/205\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.fiveforks.com\/jeb\/wp-json\/wp\/v2\/media?parent=205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fiveforks.com\/jeb\/wp-json\/wp\/v2\/categories?post=205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fiveforks.com\/jeb\/wp-json\/wp\/v2\/tags?post=205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}