MySQL Backup Script

Here is what I use to make a backup library of MySQL daily that will keep backups for 30 days.

mysql_backup.sh

#!/bin/sh

#
# Daily MySQL Backup
#

BACKUPDIR=/backup/mysql
BACKUPFILE=db-backup_`date +"%Y-%m-%d-%H"`.sql.gz
BACKUPDAYS=30

DBHOST=localhost
DBNAME="mysql db1 db2"
DBUSER=dbuser
DBPASS=dbpass

MYSQLDUMP=`which mysqldump`
GZIP=`which gzip`
FIND=`which find`

# latest daily backup
${MYSQLDUMP} --flush-logs -h${DBHOST} -u${DBUSER} -p${DBPASS} --databases ${DBNAME} | ${GZIP} -9 > ${BACKUPDIR}/${BACKUPFILE}
# uncomment below if you are running a master/slave and you want to create a new binlog
#${MYSQLDUMP} --flush-logs --master-data -h${DBHOST} -u${DBUSER} -p${DBPASS} --databases ${DBNAME} | ${GZIP} -9 > ${BACKUPDIR}/${BACKUPFILE}

# delete old backups
${FIND} ${BACKUPDIR} -mtime +${BACKUPDAYS} -delete -print

Reply

  • Allowed HTML tags: <b> <br> <p> <a> <strong> <cite> <em> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • You may use [img:xx] tags to display uploaded files or images inline.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <css>, <diff>, <drupal5>, <html>, <javascript>, <php>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

More information about formatting options