1:mysql开启log_bin功能
vim /etc/my.cnf log-bin=/data/mysql/data#!/usr/bin/bash
#define:MySQL full backup and incremental backup #date:2017.02.09 #author: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"logdir="/data/log/mysql" #日志路径
log=$logdir/log"$today".log #日志文件 is_font=1 #终端是否打印日志: 1打印 0不打印 is_log=1 #是否记录日志: 1记录 0不记录 [[ -d $logdir ]]||mkdir -p $logdir #确保日志目录存在 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 } ####日期判断 Date=`date|awk '{print $1}'`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"break
fi done }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