mysql全量备份、增量备份。开启mysql的logbin日志功能。在/etc/my.cnf文件中加入以下代码:
mysql全量备份、增量备份。开启mysql的logbin日志功能。在/etc/my.cnf文件中加入以下代码:
[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
重启mysql即可。其中路径 /home/mysql的用户和组要改成mysql。
2、增量备份
在/home/mysql/目录下建立以下目录:
增量备份脚本
cd /home/mysql
vi binlogbak.sh
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 -proot123 flush-logs
#这个是用于产生新的mysql-bin.00000*文件
Counter=wc -l $BinFile |awk '{print $1}'
NextNum=0
#这个for循环用于比对 C o u n t e r , Counter, Counter,NextNum这两个值来确定文件是不是存在或最新的。
for file in cat $BinFile
do
base=basename $file
#basename用于截取mysql-bin.00000*文件名,去掉./mysql-bin.000005前面的./
NextNum=expr $NextNum + 1
if [ N e x t N u m < / s p a n > − e q < s p a n c l a s s = " h l j s − v a r i a b l e " > NextNum -eq NextNum</span>−eq<spanclass="hljs−variable">Counter ]
then
echo KaTeX parse error: Expected 'EOF', got '&' at position 19: …e skip! &̲gt;> LogFile
else
dest= B a k D i r < / s p a n > / < s p a n c l a s s = " h l j s − v a r i a b l e " > BakDir/ BakDir</span>/<spanclass="hljs−variable">base
if(test -e $dest)
#test -e用于检测目标文件是否存在,存在就写exist!到$LogFile去。
then
echo KaTeX parse error: Expected 'EOF', got '&' at position 20: … exist! &̲gt;> LogFile
else
cp B i n D i r < / s p a n > / < s p a n c l a s s = " h l j s − v a r i a b l e " > BinDir/ BinDir</span>/<spanclass="hljs−variable">base $BakDir
echo KaTeX parse error: Expected 'EOF', got '&' at position 21: …/span> copying &̲gt;> LogFile
fi
fi
done
echo date +"%Y年%m月%d日 %H:%M:%S"
Bakup succ! >> $LogFile
?赋予binlogbak.sh执行权限
chmod a+x /home/mysql/binlogbak.sh
3、全量备份
vi databak.sh
export LANG=en_US.UTF-8
BakDir=/home/mysql/backup
LogFile=/home/mysql/backup/bak.log
Date=date +%Y%m%d
Begin=date +"%Y年%m月%d日 %H:%M:%S"
cd $BakDir
DumpFile=$Date.sql
GZDumpFile=$Date.sql.tgz
mysqldump -uroot -proot123 --all-databases --flush-logs --delete-master-logs --single-transaction > $DumpFile
tar -czvf G Z D u m p F i l e < / s p a n > < s p a n c l a s s = " h l j s − v a r i a b l e " > GZDumpFile GZDumpFile</span><spanclass="hljs−variable">DumpFile
rm $DumpFile
count=$(ls -l *.tgz |wc -l)
if [ $count -ge 5 ]
then
file=$(ls -l *.tgz |awk ‘{print $9}’|awk ‘NR==1’)
rm -f $file
fi
#只保留过去四周的数据库内容
Last=date +"%Y年%m月%d日 %H:%M:%S"
echo 开始: B e g i n < / s p a n > 结 束 : < s p a n c l a s s = " h l j s − v a r i a b l e " > Begin 结束: Begin</span>结束:<spanclass="hljs−variable">Last KaTeX parse error: Expected 'EOF', got '&' at position 24: …le succ &̲gt;> LogFile
cd $BakDir/daily
rm -f *
赋予databak.sh 执行权限
chmod a+x /home/mysql/databak.sh
?4、开启定时任务
vi /etc/crontab
#每个星期日凌晨3:00执行完全备份脚本
0 3 * * 0 /home/mysql/databak.sh >/dev/null 2>&1
#周一到周六凌晨3:00做增量备份
0 3 * * 1-6 /home/mysql/binlogbak.sh >/dev/null 2>&1
使上述定时任务生效
crontab /etc/crontab
查看定时任务
crontab -l
完成。