xtrabackup热备份mysql

目录

xtrabackup热备份mysql

备份脚本

#!/bin/bash
function write_pid()
{
        if [[ -f ${pid} ]]
        then
                rm -f ${pid}
                echo -e "${now} 初始化pid时, 删除pid" >> ${log}
        else
                echo -e "all backup pid" > ${pid}
                echo -e "${now} 初始化pid时, 生成pid" >> ${log}
        fi
}

function del_pid()
{
        now=`date +"%Y-%m-%d_%H-%M-%S"`
        if [[ -f ${pid} ]]
        then
                rm -f ${pid}
                echo -e "${now} 备份完成, 删除pid" >> ${log}
                exit 1
        fi
}

function init()
{
        export PATH=$PATH:/usr/local/webserver/mysql/bin
        all_backup="/usr/local/webserver/percona-xtrabackup-2.0.0/bin/innobackupex"
        xtra_bin="/usr/local/webserver/percona-xtrabackup-2.0.0/bin/xtrabackup_55"
        mysql_user="root"
        mysql_password="passwd"
        mysql_host='127.0.0.1'
        base_dir='/data/mysql'
        backup_dir='/data/db_backup'
        now=`date +"%Y-%m-%d_%H-%M-%S"`
        pid="/opt/slave_db_backup.pid"
        log="/opt/slave_db_all_backup.log"

        if [[ -f /opt/slave_increment.pid ]]
        then
                now=`date +"%Y-%m-%d %H:%M:%S"`
                echo -e "${now} 正在进行增量备份" >> ${log}
                exit 1
        fi

        if [[ -f ${pid} ]]
        then
                now=`date +"%Y-%m-%d %H:%M:%S"`
                echo -e "${now} 上次完全备份未完成" >> ${log}
                exit 1
        fi
}

function db_backup()
{
        local port=$1
        ${all_backup} --user=${mysql_user} --password=${mysql_password} --slave-info --no-timestamp \
        --host=${mysql_host} --port=${port} --defaults-file=${base_dir}/${port}/my.cnf --safe-slave-backup \
        --ibbackup=${xtra_bin} ${backup_dir}/${port}/all/${now}

        ${all_backup} --apply-log --defaults-file=${base_dir}/${port}/my.cnf ${backup_dir}/${port}/all/${now}
        mkdir -p ${backup_dir}/${port}/all/

        tar czvf ${backup_dir}/${port}/all/dbbackup_${now}.tar.gz ${backup_dir}/${port}/all/${now}


        cd ${backup_dir}/${port}/all/
        if [[ ${backup_dir}/${port}/all == `pwd` ]]
        then
                rm -rf ${now}
                echo -e "${port}切换成功" >> ${log}
        fi
}


init
#生成pid
write_pid
#houdao
db_backup "3307"
db_backup "3308"
db_backup "3309"
#删除pid
del_pid


目录