#!/bin/bash
#?Date:2017-10
#?Description:backup?mysql?binary?logs
#?Version:0.1
db_user="root"
myisam_db="mysql"
db_passwd="123456"
host="localhost"
export?PATH="/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin"
backup_dir="/opt/backup/mysql/bin"
log_file="/opt/backup/mysql/bin/bin_log_file.log"
bin_index="/mydata/data/mysql-bin.index"
data_dir="/mydata/data"
mysql="$(which?mysql)"
$mysql?-u$db_user?-h$host?-p$db_passwd?-e?"flush?logs"
num=$(awk?‘END{print?NR}‘?$bin_index)
counter=0
start_time=$(date?+"%F?%H:%M:%S")
[?!?-d?$backup_dir?]?&&?mkdir?-p?$backup_dir
[?!?-f?$log_file?]?&&?touch?$log_file
echo?"Starting?backup?mysql?binary?logs?at?$start_time"?>>$log_file
for?file?in?`cat?$bin_index`
do
??base=`basename?$file`
??counter=$(expr?$counter?+?1)
??if?[?$counter?-eq?$num?];then
????echo?"Skip?$base"?>>$log_file
??else
????if?[?!?-f?$backup_dir/$base?];then
??????cp?-a?$data_dir/$base?$backup_dir?2>>$log_file
??????if?[?$??-eq?0?];then
????????echo?"Backup?$base?successfully"?>>$log_file
??????else
????????echo?"Backup?$base?failed"?>>$log_file
????????echo?"Backup?$base?to?$backup_dir?failed"|mail?-s?"MySQL?binary?logs?backup?failed"[email protected]localhost
??????fi
????fi
??fi
done
stop_time=$(date?+"%F?%H:%M:%S")
echo?"End?backup?mysql?binary?logs?at?$stop_time"?>>$log_file
echo?"?"?>>$log_file
exit?0