I. Script description 1, need to back up 2 libraries (mysqltest1 mysqltest) 2, open 4 process 3, you can choose to do backup uncompressed and backup compression 4, you can reject the unwanted database 5, backup user permissions in the database grant select,reload,super,lock tables,create view,show view on *.* to ' Backup ' @ ' localhost ' identified by ' backup '; flush privileges;6, scheduled Task 00 00 * * * cd /opt/dbbak && bash mysqlback.sh mysqlbackupconf >> /opt/dbbak/dbbackup.log 2>&17, will delete three days ago Binlog8, MySQL version 5.5 of 9, debian7.4 (3.2.54-2 x86_ 64) Second, the specific script is as follows cat mysqlback.sh#!/bin/bash#--------------------------------------------------#Author: Jimmygong#email:[email protected] #FileName:mysqlback.sh#function: #Version:1.0 #Created : 2015-06-29#--------------------------------------------------If [[ $# -ne 1 ]]then echo -e "usage:$0 mysqlbackupconf" exit 1else config=$1 if [[ ! -f $config ]] then echo -e "usage:$0 mysqlbackupconf" exit 1 fifisource $configdbname = "" currdate=$ (date +%y%m%d) haveinnodb=0localip=$ (ifconfig eth0|awk '/inet addr:/' |awk -F: ' {print $2} ' |awk -f ' ' ' {print $1} ') function purgebinlog () { purdate= ' date ' +%f %t ' --date= ' 3 day ago ' pur= ' purge master logs before ' $purdate ' " echo " $pur "|mysql -u" $ Dbuser " -p" $dbpass "}function redirectlog () { logfile= $logdir/${currdate} _${localip}_log mkdir -p $logdir exec 1>$ Logfile exEc 2> $logfile}function checkdestdir () { destdir= $destdir/$currdate if [[ ! -d $destdir ]] then mkdir -p $destdir fi}function runcommand () { comm=$1 mysql -u "$dbuser" -p "$ Dbpass " -sNe " $comm "}function generateschema () { i=0 result= ' runcommand ' show databases ' for db in $ result do rv= ' echo $exclude |grep -w -i $db ' if [[ -n ' $rv ] ] then continue; fi dbname[i]= $db let i++ done}function guessengine () { innodb= ' runcommand ' show engines ' |grep -i Innodb|grep -i yes ' if [[ ! -z "$innodb" ]]    THEN        HAVEINNODB=1    FI} function dobackupsql () { db=$1 destname= $destdir/$ db.sql if [[ $haveinnodb -eq 1 ]] then dumpcomm= "Mysqldump --opt -u$dbuser -p$dbpass --single-transaction --databases " else dumpcomm= "mysqldump --Opt -u$dbuser -p$dbpass --databases " fi $dumpcomm $DB > $destname}function dobackupgzip () { db=$1 destname= $destdir/$db .sql.gz if [[ $haveinnodb -eq 1 ]] then dumpcomm= "mysqldump --opt -u$dbuser -p$dbpass --single-transaction --databases " Else dumpcomm= "mysqldump --opt -u$dbuser -p$ Dbpass --databases " fi $dumpcomm $db |gzip > $destname}function backup () { actioncommand=$1 if [[ $commpress -eq 1 ]] then actioncommanD= "Dobackupgzip" else actioncommand= " Dobackupsql " fi echo -en " ' Date ' \tbackup\t$db\n " $actioncommand $db}function backupalldb () { count=0 for db in ${dbname[@]} do backup $db & let count+=1 [[ $ ((count%$ Processnum)) -eq 0 ]] && wait done wait echo "All backup done"} purgebinlogredirectlogcheckdestdirgenerateschemaguessengineruncommand "Flush logs" Backupalldbexit 0 Three, specific configuration information cat mysqlbackupconfdbuser= "Backup" dbpass= "Backup" exclUde= ' Mysql information_schema performance_schema ' destdir=/opt/dbbaklogdir=/opt/dbbak/logcommpress= 1processnum=4 Four, after the backup Results 1, [email protected]:~# ll /opt/dbbak/20150629/-rw-r--r-- 1 root root 4443602410 Jun 29 19:18 mysqltest1.sql.gz-rw-r--r-- 1 root ROOT 4443601959 JUN 29 19:19 MYSQLTEST.SQL.GZ2, [Email protected]:~# ll /opt/dbbak/log/-rw-r--r-- 1 root root 109 jun 29 19:19 20150629_ 10.131.172.202_log
This article is from the "7928217" blog, please be sure to keep this source http://7938217.blog.51cto.com/7928217/1669168
MySQL Backup single instance (a) shell