mysqldump是一种逻辑备份工具 , 可以对数据库进行全量备份 , 和binlog增量备份共同使用可以进行数据库备份 , 基于此写了一个备份的脚本
#!/bin/bash
all_path="/opt/mysql_bakup/all" # MySQL全量备份目录
add_path="/opt/mysql_bakup/add" # MySQL增量备份目录
old_all_path="/opt/mysql_bakup/old_all" # MySQL旧全量备份目录
tar_date="$(date +%F)" # 打包时间
date_name="$(date +%F_%T)" # 备份时间
db_name="test" # 数据库名称
user_name="root" # 数据库用户名称
user_passwd="root" # 数据库用户密码
mysql_operate="mysql -u"${user_name}" -p"${user_passwd}" "
data_path="/data/mysql"
log_path="${data_path}/log" # MySQL日志存放位置
# 0.开启binlog功能
binlog_status=$($mysql_operate -e "show variables like 'log_bin'" | awk 'NR==2{print $2}')
if [ $binlog_status == "OFF" ] ; then
cat >>/etc/my.cnf << EOF
log-bin=$data_path/log/mysql_bin # 二进制日志binlog文件位置和名称
server-id=1 #MySQL服务器的唯一标识符,用于主从同步
EOF
systemctl restart mysqld
if [ $? -eq 0 ] ; then
echo "开启binlog日志功能"
else
echo "开启binlog日志功能失败"
exit 10
fi
fi
# 1.如果没有备份目录,就创建
create_dir(){
if [ ! -d ${all_path} ] ; then
mkdir -p ${all_path}
echo "创建全量备份目录${all_path}结束"
fi
if [ ! -d ${add_path} ] ; then
mkdir -p ${add_path}
echo "创建增量备份目录${add_path}结束"
fi
if [ ! -d ${old_all_path} ] ; then
mkdir -p ${old_all_path}
echo "创建打tar包的旧全量备份目录${old_all_path}结束"
fi
}
# 2.全量备份
all_backup(){
# 1.将之前的全量包做打包处理
if [ $(ls ${all_path} ) ]; then
# 打tar包的同时删除源文件
tar czf ${old_all_path}/${tar_date}.tar.gz -C ${all_path} . && find /opt/mysql_bakup/all -mindepth 1 -delete
if [ $? -eq 0 ] ; then
echo "之前的全量备份打包到${old_all_path}/${tar_date}.tar.gz"
else
echo "这种打包也能出问题?离谱...."
exit 1
fi
fi
# 2.将数据库的数据备份
mysqldump -u"${user_name}" -p"${user_passwd}" --single-transaction --flush-logs -B ${db_name} > ${all_path}/${db_name}_db_${date_name}.sql
if [ $? -eq 0 ] ; then
echo "全量备份成功,位置为${all_path}/${db_name}_db_${date_name}.sql"
else
echo "就一句话的全量备份也能出问题啊?啊?...."
exit 2
fi
}
# 3.增量备份
add_backup(){
# 1.获取当前最新的binlog文件编号
new_num=$(cat ${log_path}/mysql_bin.index | tail -1 | cut -d "." -f 2)
# 2.获取目录中最新的binlog文件编号
old_num=$(find ${add_path} -mindepth 1 | tail -1 | cut -d "." -f 2)
if [ -z $old_num ] ; then
old_num=0
fi
# 3.获取还差几个文件,加上1个刷新次数
num=$((new_num-old_num+1))
# 4.刷新binlog日志
${mysql_operate} -e "flush logs"
# 2.备份相差个数的binlog文件,
log_name=($(tail -${num} ${log_path}/mysql_bin.index))
log_length=${#log_name[@]}
for i in ${log_name[*]} ; do
# 运行到最新的一行跳出
if [[ $i == ${log_name[log_length-1]} ]] ; then
echo "最后一次直接跳出"
break
fi
cp ${i} ${add_path}
if [ $? -eq 0 ] ; then
echo "增量备份成功,位置为${add_path}${i}"
else
echo "增量备份失败"
exit 3
fi
done
}
# 4.还原数据库
back_resore(){
# 1.临时关闭binlog功能
${mysql_operate} -e "set sql_log_bin=0"
echo "临时关闭binlog功能"
# 2.将全量备份恢复至数据库
all_bak="$(find ${all_path} -mindepth 1)" # 找到全量备份
${mysql_operate} ${db_name} < ${all_bak}
if [ $? -eq 0 ] ; then
echo "全量备份恢复成功"
else
echo "全量备份恢复失败"
exit 4
fi
# 3.将增量备份按照顺序恢复至数据库
add_bak="$(find ${add_path} -mindepth 1)" # 找到增量备份
for i in ${add_bak[*]} ; do
mysqlbinlog ${i} | ${mysql_operate}
if [ $? -eq 0 ] ; then
echo "增量备份恢复成功,${i}"
else
echo "增量备份恢复失败,${i}"
exit 5
fi
done
# 4.开启binlog功能
${mysql_operate} -e "set sql_log_bin=1"
echo "开启binlog功能"
}
create_dir
options=("all" "add" "restore")
case $1 in
${options[0]})
all_backup
;;
${options[1]})
add_backup
;;
${options[2]})
back_resore
;;
*)
echo "请使用以下格式进行备份: "
echo "<Usage> : sh $0 {全量备份 : all | 增量备份 : add | 还原备份 : restore}"
exit 1
;;
esac
mysql -uroot -proot -e "use test; select * from testlog"
标签:binlog,name,备份,echo,mysqldump,mysql,path
From: https://www.cnblogs.com/ramiro/p/18061678