mysqldump+binlog备份脚本
admin
2023-04-16 13:02:15
0

## 注意 各种目录创建及相关参数设置

## 设置 /root/dba/binlog_control.txt,记录binlog文件时间,获取时间命令:stat -c %y mysql_mmm_01-bin.000013

## 设置 /root/dba/fullbak_file.txt,记录全备目录,第一次执行时,需设置一个目录来存放备份前产生的binlog,之后每次全备会自动更新该文件

---------------------------full_backup:mysql_full_backup.sh----------------------------------------

#!/bin/bash

#mysql param

user="root"

passwd="123456"

socket="/data/mysql/run/mysql.sock"

port=3306

bk_options="--single-transaction --flush-logs -u$user -p$passwd --master-data=2 --all-databases --socket=$socket --port=$port"

#log file

dtime=`date +%Y%m%d%H%M%S`

log_base="/root/dba/logs"

log_file="$log_base/full_backup_${dtime}.log"

#control file

contr_file="/root/dba/binlog_control.txt"

fullbak_file="/root/dba/fullbak_file.txt"

#full backup dir

bk_dir_suffix=`date +%Y%m%d`

full_dir="/backup/mysql_bak/mysql_$bk_dir_suffix"

[ -d ${full_dir} ] || mkdir -p ${full_dir}

echo "mkdir full backup dir : $full_dir" >> $log_file

#full backup file

full_bk_file="mysql_full_$bk_dir_suffix.sql"

#mysqldump

cd $full_dir

mysqldump $bk_options > $full_bk_file

echo "full backup file  : $full_bk_file" >> $log_file

#backup flush logs--binlog to previous full backup

/root/dba/mysql_binlog_backup_new.sh

#update fullbak_file control file

echo $full_dir > $fullbak_file



---------------------------binlog_backup:mysql_binlog_backup_new.sh-------------------------------------------------------

#!/bin/bash

#mysql

user="root"

passwd="123456"

socket="/data/mysql/run/mysql.sock"

port=3306

#log file

dtime=`date +%Y%m%d%H%M%S`

log_base="/root/dba/logs"

log_file="$log_base/binlog_backup_${dtime}.log"

#control file

contr_file="/root/dba/binlog_control.txt"

fullbak_file="/root/dba/fullbak_file.txt"

#binlog path

binlog_path="/data/mysql/binlog/"

#binlog file format

binlog_format="mysql_mmm_01-bin.*[0-9]*"

#binlog backup dir

dest_dir=`cat $fullbak_file`

#current binlog

curr_binlog=`mysql -u$user -p$passwd --socket=$socket --port=$port -e 'show master status\G'|grep "File"|awk '{print $2}'`

echo "Current binlog      : $curr_binlog">>$log_file

#current binglog file time

currbinlog_time=`stat -c %y $binlog_path$curr_binlog`

echo "Current binlog time : $currbinlog_time">>$log_file

#lastest backup binlog file time

previous_time=`cat $contr_file`

echo "Previous time       : $previous_time">>$log_file

# ! -name "*$curr_binlog*" exclude current binglog

bk_count=`find -L $binlog_path -name "$binlog_format" ! -name "*$curr_binlog*" -newermt "$previous_time" ! -newermt "$currbinlog_time"|wc -l`

echo "backup files count  : $bk_count">>$log_file

if [ $bk_count -gt 0 ];then

find -L $binlog_path -name "$binlog_format" ! -name "*$curr_binlog*" -newermt "$previous_time" ! -newermt "$currbinlog_time"|xargs ls -t>>$log_file

#find the newest bakup file

last_file=`find -L $binlog_path -name "$binlog_format" ! -name "*$curr_binlog*" -newermt "$previous_time" ! -newermt "$currbinlog_time"|xargs ls -t|head -n 1`

#newest backup file time

last_file_time=`stat -c %y $last_file`

echo $last_file_time > $contr_file

#cp files

find -L $binlog_path -name "$binlog_format" ! -name "*$curr_binlog*" -newermt "$previous_time" ! -newermt "$currbinlog_time" -exec cp {} $dest_dir \;

fi

find $log_base -mtime +15 -exec rm -f {} \;


相关内容

热门资讯

凤凰连线:中美新一轮经贸磋商,... 中美双方将在韩国举行第七轮经贸磋商。美方的阵容和日程安排如何?在这轮磋商中有哪些关切?凤凰卫视驻韩国...
知情人士:阿联酋秘密打击伊朗,... 据参考消息援引美国《华尔街日报》网站5月11日报道,多名知情人士透露,阿联酋已对伊朗发动军事打击,令...
美防长称美伊停火协议依然有效 △赫格塞思(资料图)当地时间5月12日,美国国防部长赫格塞思表示,他们针对伊朗问题的所有情况都制定了...
特朗普二度来华,五大博弈看点,... 就在5月11日,外交部官宣了一则重磅消息:应中国邀请,美国总统特朗普将于5月13日至15日开启访华行...
科学家预测:“哥斯拉级”厄尔尼... 科学家近日发出警告,太平洋上空正在形成一种被称为“哥斯拉”级的罕见厄尔尼诺气候模式。有科学家预测,这...
200亿美金估值的可灵,值母公... 文 | 影子备忘录 要说今年科技圈最火的赛道,AI短剧绝对排得上号。一部AI仿真人短剧,3人团队、...
多名中国公民在越南乘机遗失财物... 近日,多名中国公民反映在越乘机过程中财物遗失,中国驻胡志明市总领馆提醒中国公民注意:一、强化防范意识...
电视机尺寸一览表70寸长宽 电视机尺寸一览表70寸长宽:一般液晶电视显示屏都是16比9,70寸就是说客它对角线长70寸是106....
东莞一社区推出生育奖励方案,二... 极目新闻记者 柳之萌近日,广东东莞万江街道谷涌社区出台生育奖励方案,对符合条件的二孩家庭一次性奖励1...
80寸液晶电视尺寸长宽多少厘米 80寸液晶电视尺寸长宽多少厘米:80寸的液晶电视大概为181x111厘米,即长度大概在181厘米,宽...