vim all_db_bak.sh
#!/bin/bash
#MySQL 全库备份
PATH="/usr/local/mysql/bin:$PATH"
BAK1=/bak1
BAK2=/bak2
TIME=$(date +"%F:%H:%M:%S")
USER=root
PASS=123456
if [ ! -d $BAK1 -o ! -d $BAK2 ];then
mkdir $BAK1
mkdir $BAK2
mysqldump -u$USER -p$PASS --all-databases > /$BAK1/all_${TIME}.sql
cp $BAK1/all_${TIME}.sql $BAK2
echo -e "\033[32mMySQL数据已全库备份到 ${BAK2} 目录\033[0m"
else
mysqldump -u$USER -p$PASS --all-databases > /$BAK1/all_${TIME}.sql
cp $BAK1/all_${TIME}.sql $BAK2
echo -e "\033[32mMySQL数据已全库备份到 ${BAK2} 目录\033[0m"
fi
[mysqld]
log-bin = "/home/mysql/logbin.log"
binlog-format = ROW
log-bin-index = "/home/mysql/logindex"
binlog_cache_size=32m
max_binlog_cache_size=512m
max_binlog_size=512m
mkdir -p /home/mysql/backup/daily
vim binlogbak.sh
#!/bin/bash
PATH="/usr/local/mysql/bin:$PATH"
export LANG=en_US.UTF-8
BakDir=/home/mysql/backup/daily
BinDir=/home/mysql
LogFile=/home/mysql/backup/binlog.log
BinFile=/home/mysql/logindex.index
#注意修改密码
mysqladmin -uroot -p123456 flush-logs
#这个是用于产生新的 mysql-bin.00000* 文件
Counter=`wc -l $BinFile |awk '{print $1}'`
NextNum=0
#这个 for 循环用于比对 $Counter,$NextNum 这两个值来确定文件是不是存在或最新的
for file in `cat $BinFile`
do
base=`basename $file`
#basename用于截取mysql-bin.00000*文件名,去掉./mysql-bin.000005前面的./
NextNum=`expr $NextNum + 1`
if [ $NextNum -eq $Counter ]
then
echo $base skip! >> $LogFile
else
dest=$BakDir/$base
if(test -e $dest)
#test -e用于检测目标文件是否存在,存在就写exist!到$LogFile
then
echo $base exist! >> $LogFile
else
cp $BinDir/$base $BakDir
echo $base copying >> $LogFile
fi
fi
done
echo `date +"%Y年%m月%d日 %H:%M:%S"` Bakup succ! >> $LogFile
vim /etc/crontab
#每个星期日凌晨 3:00 执行完全备份脚本
0 3 * * 0 /home/mysql/all_db_bak.sh >/dev/null 2>&1
#周一到周六凌晨3:00做增量备份
0 3 * * 1-6 /home/mysql/binlogbak.sh >/dev/null 2>&1
crontab /etc/crontab #使上述定时任务生效
crontab -l #查看定时任务
/binlogbak.sh >/dev/null 2>&1
crontab /etc/crontab /#使上述定时任务生效
crontab -l /#查看定时任务
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/weixin_53560205/article/details/121043422
内容来源于网络,如有侵权,请联系作者删除!