三.添加脚本
全量备份
创建备份目录
mkdir -p /root/bin
mkdir -p /bak/mysql-xback
编写脚本,要指定备份命令的账号和密码
vim /root/bin/mybak-all.sh
#!/bin/bash
#全量备份,只备份一次
#指定备份目录
backup_dir="/bak/mysql-xback"
#检查
[[
-d ${backup_dir}
]]
|| mkdir -p ${backup_dir}
if
[[
-d ${backup_dir}/all-backup ]];then
echo "全备份已存在"
exit 1
fi
#命令,需要设置
innobackupex --defaults-file=/etc/my.cnf --user=back --password='123456'
--no-timestamp ${backup_dir}/all-backup &>
/tmp/mysql-backup.log
tail -n 1
/tmp/mysql-backup.log | grep 'completed OK!'
if
[[ $?
-eq 0
]];then
echo "all-backup"
>
/tmp/mysql-backup.txt
else
echo "备份失败"
exit 1
fi
增量备份
编写脚本,要指定备份目录
vim /root/bin/mybak-section.sh
#!/bin/bash
#增量备份
#备份目录
backup_dir="/bak/mysql-xback"
#新旧备份
old_dir=`cat /tmp/mysql-backup.txt`
new_dir=`date %F-%H-%M-%S`
#检查
if
[[
!
-d ${backup_dir}/all-backup ]];then
echo "还未全量备份"
exit 1
fi
#命令
/usr/bin/innobackupex --user=back --password='123456'
--no-timestamp --incremental --incremental-basedir=${backup_dir}/${old_dir} ${backup_dir}/${new_dir}
&>
/tmp/mysql-backup.log
tail -n 1
/tmp/mysql-backup.log | grep 'completed OK!'
if
[[ $?
-eq 0
]];then
echo "${new_dir}"
>
/tmp/mysql-backup.txt
else
echo "备份失败"
exit 1
fi
binlog
创建备份目录
mkdir -p /bak/mysql-binback
用于单点,备份binlog,要指定备份目录位置和其它变量
vim /root/bin/mybak-binlog.sh
#!/bin/bash
#
# 注意:执行脚本前修改脚本中的变量
# 功能:cp方式增量备份
#
# 适用:centos6
# 语言:中文
#
#使用:./xx.sh -uroot -p'123456',将第一次增量备份后的binlog文件名写到/tmp/binlog-section中,若都没有,自动填写mysql-bin.000001
#过程:增量先刷新binlog日志,再查询/tmp/binlog-section中记录的上一次备份中最新的binlog日志的值
# cp中间的binlog日志,并进行压缩。再将备份中最新的binlog日志写入。
#恢复:先进行全量恢复,再根据全量备份附带的time-binlog.txt中的记录逐个恢复。当前最新的Binlog日志要去掉有问题的语句,例如drop等。
#[变量]
#mysql这个命令所在绝对路径
my_sql="/usr/local/mysql/bin/mysql"
#mysqldump命令所在绝对路径
bak_sql="/usr/local/mysql/bin/mysqldump"
#binlog日志所在目录
binlog_dir=/usr/local/mysql/data
#mysql-bin.index文件所在位置
binlog_index=${binlog_dir}/mysql-bin.index
#备份到哪个目录
bak_dir=/bak/mysql-binback
#这个脚本的日志输出到哪个文件
log_dir=/tmp/mybak-binlog.log
#保存的天数,4周就是28天
save_day=10
#[自动变量]
#当前年
date_nian=`date %Y-`
begin_time=`date %F-%H-%M-%S`
#所有天数的数组
save_day_zu=($(for i in
`seq 1 ${save_day}`;do date -d -${i}days " %F";done))
#开始
/usr/bin/echo >> ${log_dir}
/usr/bin/echo "time:$(date %F-%H-%M-%S) info:开始增量备份"
>> ${log_dir}
#检查
${my_sql} $*
-e "show databases;"
&>
/tmp/info_error.txt
if
[[ $?
-ne 0
]];then
-
/usr/bin/echo "time:$(date %F-%H-%M-%S) info:登陆命令错误"
>> ${log_dir}
-
/usr/bin/cat /tmp/info_error.txt #如果错误则显示错误信息
exit 1
fi
#移动到目录
cd ${bak_dir}
bak_time=`date %F-%H-%M`
bak_timetwo=`date %F`
#刷新
${my_sql} $*
-e "flush logs"
if
[[ $?
-ne 0
]];then
-
/usr/bin/echo "time:$(date %F-%H-%M-%S) error:刷新binlog失败"
>> ${log_dir}
exit 1
fi
#获取开头和结尾binlog名字
last_bin=`cat /tmp/binlog-section`
next_bin=`tail -n 1 ${binlog_dir}/mysql-bin.index`
echo ${last_bin}
|grep 'mysql-bin'
&>
/dev/null
if
[[ $?
-ne 0
]];then
echo "mysql-bin.000001"
>
/tmp/binlog-section #不存在则默认第一个
last_bin=`cat /tmp/binlog-section`
fi
#截取需要备份的binlog行数
a=`/usr/bin/sort ${binlog_dir}/mysql-bin.index | uniq | grep -n ${last_bin}
| awk -F':'
'{print $1}'`
b=`/usr/bin/sort ${binlog_dir}/mysql-bin.index | uniq | grep -n ${next_bin}
| awk -F':'
'{print $1}'`
let b--
#输出最新节点
/usr/bin/echo "${next_bin}"
>
/tmp/binlog-section
#创建文件
rm -rf mybak-section-${bak_time}
/usr/bin/mkdir mybak-section-${bak_time}
for i in
`sed -n "${a},${b}p" ${binlog_dir}/mysql-bin.index | awk -F'./'
'{print $2}'`
do
-
if
[[
!
-f ${binlog_dir}/${i}
]];then
-
/usr/bin/echo "time:$(date %F-%H-%M-%S) error:binlog文件${i} 不存在"
>> ${log_dir}
exit 1
fi
cp -rf ${binlog_dir}/${i} mybak-section-${bak_time}/
-
if
[[
!
-f mybak-section-${bak_time}/${i}
]];then
-
/usr/bin/echo "time:$(date %F-%H-%M-%S) error:binlog文件${i} 备份失败"
>> ${log_dir}
exit 1
fi
done
#压缩
if
[[
-f mybak-section-${bak_time}.tar.gz ]];then
-
/usr/bin/echo "time:$(date %F-%H-%M-%S) info:压缩包mybak-section-${bak_time}.tar.gz 已存在"
>> ${log_dir}
-
/usr/bin/rm -irf mybak-section-${bak_time}.tar.gz
fi
/usr/bin/tar -cf mybak-section-${bak_time}.tar.gz mybak-section-${bak_time}
if
[[ $?
-ne 0
]];then
-
/usr/bin/echo "time:$(date %F-%H-%M-%S) error:压缩失败"
>> ${log_dir}
exit 1
fi
#删除binlog文件夹
/usr/bin/rm -irf mybak-section-${bak_time}
if
[[ $?
-ne 0
]];then
-
/usr/bin/echo "time:$(date %F-%H-%M-%S) info:删除sql文件失败"
>> ${log_dir}
exit 1
fi
#整理压缩的日志文件
for i in
`ls | grep "^mybak-section.*tar.gz$"`
-
do
echo $i | grep ${date_nian}
&>
/dev/null
-
if
[[ $?
-eq 0
]];then
a=`echo ${i%%.tar.gz}`
b=`echo ${a:(-16)}`
#当前日志年月日
c=`echo ${b%-*}`
d=`echo ${c%-*}`
-
#看是否在数组中,不在其中,并且不是当前时间,则删除。
echo ${save_day_zu[*]}
|grep -w $d &>
/dev/null
-
if
[[ $?
-ne 0
]];then
-
[[
"$d"
!=
"$bak_timetwo"
]]
&& rm -rf $i
fi
-
else
-
#不是当月的,其他类型压缩包,跳过
-
continue
fi
done
#结束
last_time=`date %F-%H-%M-%S`
/usr/bin/echo "begin_time:${begin_time} last_time:${last_time}"
>> ${log_dir}
/usr/bin/echo "time:$(date %F-%H-%M-%S) info:增量备份完成"
>> ${log_dir}
/usr/bin/echo >> ${log_dir}
用于主从,备份relay-bin,要指定备份目录位置和其它变量
vim /root/bin/mybak-binlog.sh
#!/bin/bash
#
# 注意:执行脚本前修改脚本中的变量
# 功能:cp方式增量备份
#
# 适用:centos6
# 语言:中文
#
#使用:./xx.sh -uroot -p'123456'
#[变量]
#mysql这个命令所在绝对路径
my_sql="/usr/local/mysql/bin/mysql"
#mysqldump命令所在绝对路径
bak_sql="/usr/local/mysql/bin/mysqldump"
#binlog日志所在目录
binlog_dir=/usr/local/mysql/data
#mysql-bin.index文件所在位置
binlog_index=${binlog_dir}/mysql-bin.index
#备份到哪个目录
bak_dir=/bak/mysql-binback
#这个脚本的日志输出到哪个文件
log_dir=/tmp/mybak-binlog.log
#保存的天数,4周就是28天
save_day=10
#[自动变量]
#当前年
date_nian=`date %Y-`
begin_time=`date %F-%H-%M-%S`
#所有天数的数组
save_day_zu=($(for i in
`seq 1 ${save_day}`;do date -d -${i}days " %F";done))
#开始
/usr/bin/echo >> ${log_dir}
/usr/bin/echo "time:$(date %F-%H-%M-%S) info:开始增量备份"
>> ${log_dir}
#检查
${my_sql} $*
-e "show databases;"
&>
/tmp/info_error.txt
if
[[ $?
-ne 0
]];then
-
/usr/bin/echo "time:$(date %F-%H-%M-%S) info:登陆命令错误"
>> ${log_dir}
-
/usr/bin/cat /tmp/info_error.txt #如果错误则显示错误信息
exit 1
fi
#移动到目录
cd ${bak_dir}
bak_time=`date %F-%H-%M`
bak_timetwo=`date %F`
#创建文件
rm -rf mybak-section-${bak_time}
/usr/bin/mkdir mybak-section-${bak_time}
for i in
`ls ${binlog_dir}| grep relay-bin`
do
cp -rf ${binlog_dir}/${i} mybak-section-${bak_time}/
-
if
[[
!
-f mybak-section-${bak_time}/${i}
]];then
-
/usr/bin/echo "time:$(date %F-%H-%M-%S) error:binlog文件${i} 备份失败"
>> ${log_dir}
exit 1
fi
done
#压缩
if
[[
-f mybak-section-${bak_time}.tar.gz ]];then
-
/usr/bin/echo "time:$(date %F-%H-%M-%S) info:压缩包mybak-section-${bak_time}.tar.gz 已存在"
>> ${log_dir}
-
/usr/bin/rm -irf mybak-section-${bak_time}.tar.gz
fi
/usr/bin/tar -cf mybak-section-${bak_time}.tar.gz mybak-section-${bak_time}
if
[[ $?
-ne 0
]];then
-
/usr/bin/echo "time:$(date %F-%H-%M-%S) error:压缩失败"
>> ${log_dir}
exit 1
fi
#删除binlog文件夹
/usr/bin/rm -irf mybak-section-${bak_time}
if
[[ $?
-ne 0
]];then
-
/usr/bin/echo "time:$(date %F-%H-%M-%S) info:删除sql文件失败"
>> ${log_dir}
exit 1
fi
#整理压缩的日志文件
for i in
`ls | grep "^mybak-section.*tar.gz$"`
-
do
echo $i | grep ${date_nian}
&>
/dev/null
-
if
[[ $?
-eq 0
]];then
a=`echo ${i%%.tar.gz}`
b=`echo ${a:(-16)}`
#当前日志年月日
c=`echo ${b%-*}`
d=`echo ${c%-*}`
-
#看是否在数组中,不在其中,并且不是当前时间,则删除。
echo ${save_day_zu[*]}
|grep -w $d &>
/dev/null
-
if
[[ $?
-ne 0
]];then
-
[[
"$d"
!=
"$bak_timetwo"
]]
&& rm -rf $i
fi
-
else
-
#不是当月的,其他类型压缩包,跳过
-
continue
fi
done
#结束
last_time=`date %F-%H-%M-%S`
/usr/bin/echo "begin_time:${begin_time} last_time:${last_time}"
>> ${log_dir}
/usr/bin/echo "time:$(date %F-%H-%M-%S) info:增量备份完成"
>> ${log_dir}
/usr/bin/echo >> ${log_dir}
重写备份
创建备份目录
mkdir -p /bak/xback
编写脚本,要指定备份目录位置
vim /root/bin/mybak-rewrite.sh
#!/bin/bash
#xbak备份脚本
#每周六执行一次
#10 4 * * 6 /bin/bash /root/bin/mybak-rewrite.sh
#清理并备份
[[
-d /bak/xback ]]
|| mkdir -p /bak/xback
cd /bak/xback
rm -rf *.tar.gz
[[
-d bak/mysql-xback ]]
|| echo "bak-dir not found"
cd /bak/mysql-xback
tar -cf XtraBackup.tar.gz *
mv XtraBackup.tar.gz /bak/xback
rm -rf /bak/mysql-xback/*
#全备份一次
bash /root/bin/mybak-all.sh
备份周期
添加权限
chmod x /root/bin/*
先进行一次innobackupex全量备份,后面的增量均在全量的基础上备份。
bash /root/bin/mybak-all.sh
每天2点进行一次innobackupex增量备份 binlog日志备份 每周4点将之前的innobackupex备份打包,并启动新的全量备份 crontab -e
代码语言:javascript复制30 2 * * * /bin/bash /root/bin/mybak-binlog.sh -uback -p'123456'
40 2 * * * /bin/bash /root/bin/mybak-section.sh10
4 * * 6 /bin/bash /root/bin/mybak-rewrite.sh