MySQL full backup and incremental backup scripts

1. Incremental backup

  1 mkdir -p /home/mysql/backup/daily

script

  1 cd /home/mysql
   2 vi binlogbak.sh
   3  
  4 #!/bin/bash
   5 export LANG=en_US.UTF-8
   6 BakDir=/home/mysql/ backup /daily
   7 BinDir=/home/mysql
   8 LogFile=/ home/mysql/ backup /binlog.log
   9 BinFile=/home/mysql/logindex. index 
10 mysqladmin -uroot -proot123 flush-logs
 11 #This is used to generate a new mysql-bin.00000* file
 12 Counter=`wc -l $BinFile |awk ' {print $1} '`
 13 NextNum=0
 14 #This for loop is used to compare the two values ​​of $Counter and $NextNum to determine whether the file exists or is up-to-date.
15  for  file  in `cat $BinFile`
 16 do
 17      base=`basename $ file `
 18      #basename is used to intercept mysql-bin.00000* file name, remove ./
 19      NextNum=` in front of ./mysql-bin.000005 expr $NextNum + 1`
 20      if [ $NextNum -eq $Counter ]
 21      then 
22          echo $base skip! >> $LogFile
 23      else 
24          dest=$BakDir/$base
 25          if (test -e $dest)
 26          #test -e is used to detect whether the target file exists, and if it exists, write exist! to $LogFile.
27          then 
28             echo $base exist! >> $LogFile
 29         else
 30             cp $BinDir/$base $BakDir
 31             echo $base copying >> $LogFile
 32         fi
 33     fi
 34 done
 35 echo `date +"%Y年%m月%d日 %H:%M:%S"` Bakup succ! >> $LogFile


2. Full backup

  1 vi databak.sh
  2 
  3 #!/bin/bash
  4 export LANG=en_US.UTF-8
  5 BakDir=/home/mysql/backup
  6 LogFile=/home/mysql/backup/bak.log
  7 Date=`date +%Y%m%d`
  8 Begin=`date +"%Y年%m月%d日 %H:%M:%S"`
  9 cd $BakDir
 10 DumpFile=$Date.sql
 11 GZDumpFile=$Date.sql.tgz
 12 mysqldump -uroot -proot123 --all-databases --flush-logs --delete-master-logs --single-transaction > $DumpFile 
13 tar -czvf $GZDumpFile $DumpFile
 14 rm $DumpFile
 15  
16  count =$(ls -l *.tgz | wc -l)
 17  if [ $ count -ge 5 ]
 18  then 
19  file =$(ls -l *.tgz |awk ' {print $9} '|awk ' NR==1 ')
 20 rm -f $ file 
21 fi
 22 #Only keep the database content of the past four weeks
 23  
24  Last =` date +" %Y year%m month%d day%H:%M:%S "`
 25echo start: $ Begin end: $ Last $GZDumpFile succ >> $LogFile
 26 cd $BakDir/daily
 27 rm -f *
 28 


3. Schedule tasks

  1 vi /etc/crontab
   2  
  3 #Execute a full backup script every Sunday at 3:00 am
   4 0 3 * * 0 /home/mysql/databak.sh >/dev/ null 2>&1
   5 #Monday to Saturday at 3 am :00 do incremental backup
   6 0 3 * * 1-6 /home/mysql/binlogbak.sh >/dev/ null 2>&1


4. Scheduled tasks take effect

  1 crontab /etc/crontab
  2 
  3 
  4 crontab -l

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326953034&siteId=291194637