设为首页 收藏本站
查看: 1207|回复: 0

使用shell实现mysql自动全备、增备&日志备份

[复制链接]

尚未签到

发表于 2018-8-27 07:34:34 | 显示全部楼层 |阅读模式
数据库热备脚本:  
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=2 ###要周几做全备,周日是0,周一到周六依次为1~6
  

  

  
function init_dir()
  
{
  
    chkdir="/home/mysql/chkpoint"
  
    logdir="/home/mysql/log/xtrabackup_log"
  
    monitordir="/home/mysql/monitor"
  
    shelldir="/home/mysql/shell"
  
    remotedir="/prlife/backup/mysqlbackup/databak/${host}"
  
    if [ ! -d $chkdir ]
  
    then
  
        mkdir -p $chkdir
  
    fi
  
    if [ ! -d $logdir ]
  
    then
  
        mkdir -p $logdir
  
    fi
  
    if [ ! -d $monitordir ]
  
    then
  
        mkdir -p $monitordir
  
    fi
  
    if [ ! -d $shelldir ]
  
    then
  
        mkdir -p $shelldir
  
    fi
  
    if [ ! -d $remotedir ]
  
    then
  
        mkdir -p $remotedir
  
    fi
  
    chown -R mysql:mysql $remotedir $chkdir $logdir $monitordir $shelldir
  
}
  
function init_check_file()
  
{
  
    if [ ! -d $monitor ]
  
    then
  
        rm -fr $monitor
  
    fi
  
}
  
function check_status()
  
{
  
    status=`cat ${1}|grep "completed OK"|wc -l`
  
    if [ $status -eq 2 ]
  
    then
  
        ##success backup status
  
        echo 0 > ${monitor}
  
    else
  
        ##fail backup status
  
        echo 1 > ${monitor}
  
    fi
  
}
  

  
###check dir is exist or not####
  
init_dir
  
###according to backup policy to run xtrabackup###
  
if [ $week -eq $n ]
  
then
  
    init_check_file
  
    /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
  
    init_check_file
  
    /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
  
###get the backup log and spend time of backup###
  
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/mysql/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="root"
  
bakpass="xxx"
  
bakdir="/tmp"   # the directory to backup
  
baklog="/home/mysql/log"
  
host="localhost"
  
innobackupex="innobackupex"
  
remotedir="/prlife/backup/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() {
  

  
##创建日备份目录
  
mkdir ${remotedir}/${name}_${time}_full/
  

  
##调用xtrabackup工具实现热备
  
${innobackupex} --defaults-file=${conf_file} --user=${bakuser} --password=${bakpass} --host=${host} --slave-info --stream=xbstream --compress --extra-lsndir=${chkpointdir} --parallel=4 --throttle=120 ${bakdir} |xbstream -x -C ${remotedir}/${name}_${time}_full/
  

  
##备份配置文件
  
cp ${conf_file} ${remotedir}/${name}_${time}_full/
  

  
}
  

  
incr() {
  

  
##创建日备份目录
  
mkdir /${remotedir}/${name}_${time}_incr/
  

  
##获取上一次的备份位置点
  
lsn=`cat ${chkpointdir}/xtrabackup_checkpoints|grep to_lsn|awk '{print $NF}'`
  

  
##调用xtrabackup工具实现热备
  
innobackupex --defaults-file=${conf_file} --user=${bakuser} --password=${bakpass} --host=${host} --slave-info --stream=xbstream --compress --extra-lsndir=${chkpointdir} --parallel=4 --throttle=120 --incremental --incremental-lsn=${lsn}  ${bakdir} |xbstream -x -C ${remotedir}/${name}_${time}_incr/
  

  
##备份配置文件
  
cp ${conf_file} ${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数据。



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-556989-1-1.html 上篇帖子: shell memcache 安装脚本 下篇帖子: Shell获取IP地址的多种方法
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表