shell编写mysql全备和增备脚本_使用shell实现mysql自动全备、增备&日志备份

数据库热备脚本:

vim backup.sh

#!/bin/sh

time=`date "+%Y%m%d_%H%M%S"`

host=`hostname`

week=`date +%w`

monitor="/home/mysql/monitor/mysql_hotbackup_status.txt" ##zabbix监控文件

time_start=`date +%s`

n=0 ###要周几做全备,周日是0,周一到周六依次为1~6

function check_status()

{

status=`cat ${1}|grep "innobackupex: completed OK"|wc -l`

if [ $status -eq 1 ]

then

echo 0 > ${monitor}

else

echo 1 > ${monitor}

fi

}

if [ $week -eq $n ]

then

/bin/sh /home/mysql/shell/mysql_xtrabackup.sh full >> /home/mysql/log/xtrabackup_log/${host}_${time}_full.log 2>&1

check_status /home/mysql/log/xtrabackup_log/${host}_${time}_full.log;

else

/bin/sh /home/mysql/shell/mysql_xtrabackup.sh incr >> /home/mysql/log/xtrabackup_log/${host}_${time}_incr.log 2>&1

check_status /home/mysql/log/xtrabackup_log/${host}_${time}_incr.log;

fi

time_end=`date +%s`

times=$((${time_end}-${time_start}))

if [ $week -eq $n ]

then

echo "it takes ${times} seconds to backup the full backup!!!" >> /home/my

sql/log/xtrabackup_log/${host}_${time}_full.log

else

echo "it takes ${times} seconds to backup the incr backup!!!" >> /home/mysql/log/xtrabackup_log/${host}_${time}_incr.log

fi

###数据库备份策略###

###周日全备,周一~周六增量备份###

vim mysql_xtrabackup.sh

#!/bin/bash

if [ $# -ne 1 ]

then

echo "usage: `basename $0` [full|incr]"

exit 1

fi

name=`hostname`               # the name for bakcup

conf_file=/etc/my.cnf           # the mysql config file

bakuser="backup"

bakpass="backup"

bakdir=/data/mysql/mysql3306/tmp/   # the directory to backup

baklog=/home/mysql/log

host=localhost

innobackupex=/usr/bin/innobackupex

remotedir="/mysqlbackup/databak/${name}"

time=`date "+%Y%m%d_%H%M%S"`              # don't modify

yesterday=`date  +"%Y%m%d" -d  "-1 days"`

chkpointdir=/home/mysql/chkpoint/

full() {

ssh root@mysql02 "mkdir ${remotedir}/${name}_${time}_full/"

innobackupex --defaults-file=${conf_file} --user=${bakuser} --password=${bakpass} --host=${host} --slave-info --stream=xbstream --compress --extra-lsndir=${chkpointdir} --parallel=16 --throttle=120 ${bakdir}|ssh root@mysql02 \ "xbstream -x -C ${remotedir}/${name}_${time}_full/"

scp /etc/my.cnf mysql02:${remotedir}/${name}_${time}_full/

}

incr() {

ssh root@mysql02 "mkdir /${remotedir}/${name}_${time}_incr/"

lsn=`cat ${chkpointdir}/xtrabackup_checkpoints|grep to_lsn|awk '{print $NF}'`

innobackupex --defaults-file=${conf_file} --user=${bakuser} --password=${bakpass} --host=${host} --slave-info --stream=xbstream --compress --extra-lsndir=${chkpointdir} --parallel=16 --throttle=120 --incremental --incremental-lsn=${lsn}  ${bakdir}|ssh root@mysql02 \ "xbstream -x -C ${remotedir}/${name}_${time}_incr/"

scp /etc/my.cnf mysql02:${remotedir}/${name}_${time}_incr/

}

case "$1" in

full)

full

;;

incr)

incr

;;

*)

echo "Usage: $0 full|incr"

esac

需求场景:

备份策略:周日全备,周一到周六增备。

备份路径:异地备份,压缩备份。

前提条件:mysql服务器同备份机mysql02配置了ssh互信。

数据库日志备份脚本:

vim binlog_mysql01.sh

#!/bin/sh

BACKUP_BIN="/usr/bin/mysqlbinlog"

LOCAL_BACKUP_DIR="/mysqlbackup/logbak/mysql01/"

BACKUP_LOG="/home/mysql/log/binlog_log"

REMOTE_HOST="192.168.1.1"

REMOTE_PORT="3306"

REMOTE_USER="backup"

REMOTE_PASS="backup"

FIRST_BINLOG="mysql-bin.000001"

#time to wait before reconnecting after failure

SLEEP_SECONDS=10

##create local_backup_dir if necessary

mkdir -p ${LOCAL_BACKUP_DIR}

cd ${LOCAL_BACKUP_DIR}

## 运行while循环,连接断开后等待指定时间,重新连接

while :

do

if [ `ls -A "${LOCAL_BACKUP_DIR}" |wc -l` -eq 0 ];then

LAST_FILE=${FIRST_BINLOG}

else

LAST_FILE=`ls -l ${LOCAL_BACKUP_DIR} | grep -v backuplog |tail -n 1 |awk '{print $9}'`

fi

${BACKUP_BIN} --raw --read-from-remote-server --stop-never --host=${REMOTE_HOST} --port=${REMOTE_PORT} --user=${REMOTE_USER} --password=${REMOTE_PASS} ${LAST_FILE}

echo "`date +"%Y/%m/%d %H:%M:%S"` mysqlbinlog停止,返回代码:$?" | tee -a ${BACKUP_LOG}

echo "${SLEEP_SECONDS}秒后再次连接并继续备份" | tee -a ${BACKUP_LOG}

sleep ${SLEEP_SECONDS}

done

nohup sh binlog_mysql01.sh 方式启动脚本,实时备份binlog数据。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值