mysql 增量备份脚本

根据网上脚本修改而成

mysqlFullBackup.sh

#!/bin/bash

scriptsDir=/opt/shells
mysqlDir=/opt/app/mysql
user=root
userPWD=
dataBackupDir=/opt/db-backup
eMailFile=$dataBackupDir/email.txt
[email protected]
logFile=$dataBackupDir/mysqlbackup.log
DATE=`date "+%Y%m%d"`
echo "" > $eMailFile
echo $(date +"%y%m%d %H:%M:%S") >> $eMailFile
cd $dataBackupDir
dumpFile=dongao-all$DATE.sql

GZDumpFile=dongao-all$DATE.sql.tar.gz
cd $dataBackupDir

$mysqlDir/bin/mysqldump -u$user -p$userPWD --skip-opt --master-data=2 --flush-logs --single-transaction --add-drop-table --create-options --quick --extended-insert=true --default-character-set=utf8 --disable-keys --triggers -R --databases test  discuz supesite > $dumpFile


if [[ $? == 0 ]]; then

  gzip $dumpFile >> $eMailFile 2>&1

  echo "BackupFileName:$GZDumpFile" >> $eMailFile

  echo "DataBase Backup Success!" >> $eMailFile
fi



#  cd $dataBackupDir/daily

#  rm -f *


find /opt/db-backup -name "dongao*.gz" -mtime +10 -type f | xargs rm -rf



echo "--------------------------------------------------------" >> $logFile

cat $eMailFile >> $logFile


cat $eMailFile | mail -s "MySQL Full Backup" $eMail

 

mysqlDailyBackup.sh

 

#!/bin/bash
#
scriptsDir=/opt/shells
mysqlDir=/opt/app/mysql
dataDir=$mysqlDir/var
user=root
userPWD=
dataBackupDir=/opt/db-backup
dailyBackupDir=$dataBackupDir/daily
eMailFile=$dataBackupDir/email.txt
[email protected]
logFile=$dataBackupDir/mysqlbackup.log
HOSTNAME=`uname -n`
echo "" > $eMailFile
echo $(date +"%y%m%d %H:%M:%S") >> $eMailFile
$mysqlDir/bin/mysqladmin -u$user -p$userPWD flush-logs
cd $dataDir
fileList=`cat mysql-bin.index`
iCounter=0
for file in $fileList
do
  iCounter=`expr $iCounter + 1`
done
nextNum=0
iFile=0
for file in $fileList
do
  binLogName=`basename $file`
  nextNum=`expr $nextNum + 1`
# 跳过最后一个二进制日志(数据库当前使用的二进制日志文件)
  if [[ $nextNum == $iCounter ]]; then
    echo "Skip lastest!" > /dev/null
  else
    dest=$dailyBackupDir/$binLogName
# 跳过已经备份的二进制日志文件
    if [[ -e $dest ]]; then
      echo "Skip exist $binLogName!" > /dev/null
    else
# 备份日志文件到备份目录
      rsync -a $binLogName $dailyBackupDir
      if [[ $? == 0 ]]; then
        iFile=`expr $iFile + 1`
        echo "$binLogName Backup Success!" >> $eMailFile
      fi
    fi
  fi
done
chmod -R +r $dailyBackupDir
if [[ $iFile == 0 ]];then
  echo "No Binlog Backup!" >> $eMailFile
else
  echo "Backup $iFile File(s)." >> $eMailFile
  echo "Backup MySQL Binlog OK!" >> $eMailFile
fi
cat $eMailFile | mail -s "MySQL Daily Backup" $eMail
echo "--------------------------------------------------------" >> $logFile
cat $eMailFile >> $logFile
 

你可能感兴趣的:(sql,mysql,脚本,F#,bash)