mysql 二进制日志增量备份sell脚本

#function:MySQL增量备份脚本
#version:1.0.0
#author:leiyuanxin
#date:2018/10/26
#-----------------------------------
#!/bin/sh
exprot LANG=en_US.UTF-8
#设置时间
DATE=`date +%Y%m%d`
#设置信息
USER=root
PASSWORD=Wuzhan@1095618365
#设置路径
cd /
/usr/bin/mkdir -p mysql_bak/daily
/usr/bin/mkdir -p mysql_bak/logs
BakDir=/mysql_bak/daily
BinDir=/var/lib/mysql
LogFile=/mysql_bak/logs/Daily_$DATE.log
BinlogFile=/var/lib/mysql/relay-bin.index

#刷新日志
/usr/bin/mysqladmin -u$USER -p$PASSWORD flush-logs
Counter=`wc -l $BinlogFile | awk '{print $1}'`
NextNum=0
start_time=`date +'%Y-%m-%d %H:%M:%S'`
echo `date +"%Y年%m月%d日 %H:%M:%S"` $Next1 Bakup start! >> $LogFile

#这个for循环用于比对$Counter,$NextNum这两个值来确定文件是不是存在或最新的
for file in `cat $BinlogFile`
do
  base=`basename $file`
  #basename用于截取relay-bin.00000*文件名,去掉./relay-bin.00000前面的./
  NextNum=`expr $NextNum + 1`
  if [ $NextNum -eq $Counter ]
  then
    echo $base skip! >> $LogFile
  else
    dest=$BakDir/$base
    if(test -e $dest)
    #test -e用于检测目标文件是否存在,存在就写exist!到$LogFile去
    then
      echo $base exist! >> $LogFile
    else
      cp $BinDir/$base $BakDir
      echo $base copying >> $LogFile
   fi
  fi
done
echo `date +"%Y年%m月%d日 %H:%M:%S"` $Next2 Bakup succ! >> $LogFile
end_time=`date +'%Y-%m-%d %H:%M:%S'`
start_seconds=$(date --date="$start_time" +%s);
end_seconds=$(date --date="$end_time" +%s);
echo "本次备份运行时间: "$((end_seconds-start_seconds))"s" >> $LogFile

参考文章:https://www.jb51.net/article/123786.htm

你可能感兴趣的:(入门,初级)