Mysql备份脚本-Xtrabackup

Mysql备份脚本-Xtrabackup

    #!/bin/sh
    #mysql_backup
    begintime=`date +"%Y-%m-%d_%H-%M-%S"`
    format_time=`date +"%Y-%m-%d_%H-%M-%S"`
    tar_time=`date +%F --date="-1 day"`
    currentHour=$(date +%H)
    port=3306
    backdir=/data/backup
    tardir='/data/mysql_backup'
    file_cnf=/etc/my.cnf
    user_name=root
    passwd='123456'
    sock=/data/mysqldata/mysql.sock
    out_log=$backdir/xtraback_log_$format_time
    time_cost=$backdir/xtraback_time.txt

    #判断数据备份的压缩目录是否存在
    if [ ! -d "$tardir" ];then
        mkdir -p $tardir
    fi

    #判断备份目录是否存在
    if [ ! -d "$backdir" ]; then
        mkdir -p $backdir
    else
    #判断昨日是否备份,有则打包压缩后删除之前备份的目录
        if [ -d "$backdir/rec2" ];then
            tar zcf ${tardir}/mysql_bckup_$tar_time.tar.gz -C $backdir .
            sleep 3
            rm -fr ${backdir}/*
         fi
    fi

    #凌晨3时完全备份
    if [ $currentHour -eq 03 ] && [ ! -d "$backdir/full" ];then
        echo "#####start full backup at $begintime to directory full" >>$time_cost
        /usr/bin/innobackupex --defaults-file=$file_cnf --no-lock --no-timestamp --user=$user_name --password=$passwd --port=$port --socket=$sock $backdir/full &> ${out_log}-full.log
        sleep 3
    #中午14时与晚上19时进行增量备份
    elif [ $currentHour -eq 14 ] && [ ! -d "$backdir/rec1" ];then
        echo "#####start 1 incremental backup at $begintime to directory rec1" >>$time_cost
        /usr/bin/innobackupex --defaults-file=$file_cnf --no-lock --no-timestamp --user=$user_name --password=$passwd --port=$port --socket=$sock --incremental --incremental-basedir=$backdir/full $backdir/rec1 &> $out_log-rec1
        sleep 3

    elif [ $currentHour -eq 19 ] && [ ! -d "$backdir/rec2" ];then
        echo "#####start 3 incremental backup at $begintime to directory rec2" >>$time_cost
        /usr/bin/innobackupex --defaults-file=$file_cnf --no-lock --no-timestamp  --user=$user_name --password=$passwd --port=$port --socket=$sock --incremental --incremental-basedir=$backdir/rec1 $backdir/rec2 &> $out_log-rec2
        sleep 3

    fi