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 Script

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

MySQL Backup Script with MyDumper

Similar script using the super fast mysql backup script mydumper by Doomas Mituzas which will backup each table to its own compressed file using multiple threads:

#!/bin/sh

#
# Daily MySQL Backup
#

BACKUPDIR=/backup/mysql/
BACKUPFOLDER=`date +"%Y-%m-%d-%H"`
BACKUPDAYS=30

MYSQLDUMP=`which mysqldump`
MYDUMPER=/usr/local/bin/mydumper
FIND=`which find`
MKDIR=`which mkdir`

# latest daily backup
${MKDIR} ${BACKUPDIR}${BACKUPFOLDER}
${MYSQLDUMP} --no-data > ${BACKUPDIR}${BACKUPFOLDER}/_structure.sql
${MYDUMPER} -t 4 -o ${BACKUPDIR}${BACKUPFOLDER} -c

# delete old backups
${FIND} ${BACKUPDIR} -mtime +${BACKUPDAYS} -print -exec rm {} \;

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <b> <i> <strong> <cite> <em> <code> <pre> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <css>, <diff>, <drupal5>, <html>, <javascript>, <php>. The supported tag styles are: <foo>, [foo]. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.