1:mysql開啓log_bin功能
vim /etc/my.cnf
log-bin=/data/mysql/datamysql
#!/usr/bin/bash
#define:MySQL full backup and incremental backup
#date:2017.02.09
#author:sql
basename="/data/scripts/mysql_back"
mysql_bak_dir="/data/mysql_bak"
get_log_dir="/data/mysql/data"
today=`date +"%Y%m%d"`數據庫
###數據庫信息
mysql_ip="127.0.0.1"
mysql_user="root"
mysql_passwd="root"
mysql_path="/bin"vim
logdir="/data/log/mysql" #日誌路徑
log=$logdir/log"$today".log #日誌文件
is_font=1 #終端是否打印日誌: 1打印 0不打印
is_log=1 #是否記錄日誌: 1記錄 0不記錄
[[ -d $logdir ]]||mkdir -p $logdir #確保日誌目錄存在bash
datef(){
date "+%Y-%m-%d %H:%M:%S"
}日誌
print_log(){
if [[ $is_log -eq 1 ]];then
[[ -d $logdir ]] || mkdir -p $logdir
echo -e "[ $(datef) ] $1" >> $log
fi
if [[ $is_font -eq 1 ]];then
echo -e "[ $(datef) ] $1"
fi
}ip
####日期判斷
Date=`date|awk '{print $1}'`rem
full_bakck() {
print_log "mysql 全量備份"
print_log "獲取當前最新binlog"
log_bin_name=`ls -lt $get_log_dir/log.*|head -n 1|awk '{print $9}'`
while true
do
$mysql_path/mysqldump -h $mysql_ip -u$mysql_user -p$mysql_passwd --single-transaction --all-databases --flush-logs > $mysql_bak_dir/backup_sunday_1_PM"$today".sql
if [[ $? -eq 0 ]];then
print_log "mysql全量備份成功,備份名:backup_sunday_1_PM"$today".sql,二進制日誌:$log_bin_name" get
break
fi
done
}io
incremental_back() {
print_log "mysql 增量備份"
log_bin_name=`ls -lt $get_log_dir/log.*|head -n 1|awk '{print $9}'`
$mysql_path/mysqladmin -h $mysql_ip -u$mysql_user -p$mysql_passwd flush-logs
print_log "mysql 增量備份成功,二進制日誌:$log_bin_name"
}
case $Date in Sun) full_bakck ;; Mon|Tue|Wed|Thu|Fri|Sat) incremental_back ;; *) echo "Usage 獲取的日期格式有問題,請檢查" ;; esac