利用mysqldump实现mysql数据库全量备份和增量备份脚本

mysqldump实现全量备份,增量备份脚本,

增量备份,其实就是复制日志

全备脚本:
#! /bin/bash
# mysql 全量备份脚本,建议在slave上运行,并开启log_slave_updates=1
mkdir /backup
cd /backup
datadir=`date +”%y-%m-%d”`
mkdir -p $datadir/data
path=/usr/local/mysql/data
for i in `mysql -uroot -p123456 -e “show databases” |grep -v “Database”`
do
mysqldump -uroot -p123456 –defaultes-character-set=utf8 -q –lock-all-tables –flush-logs -E -R –triggers -B $i |gzip > /backup/$datadir/data/${i}_${datadir}.sql.gz
#mysqldump -uroot -ptiancity –opt –single-transaction –flush-logs -E -R -C –triggers $i |gzip > “/backup/$datadir/data/${i}_${datadir}.sql.gz”
done
binlog_rm=`tail -n 1 /app/mysql/data/mysql-bin.index |sed ‘s/\/app\/mysql\/data\///’`
mysql -uroot -p123456 -e “purge binary logs to ‘$binlog_rm'”

增备脚本:
#! /bin/bash
# mysql 增量备份脚本,其实就是复制二进制日志,
cd /backup
datadir=`date +”%y-%m-%d”`
mkdir -p $datadir/data
path=/usr/local/mysql/data
mysqladmin -uroot -p12334 flush-logs
# mysql -uroot -p12334 -e “flush logs”
binlog_cp=`head -n -1 $path/mysql-bin.index |sed ‘s/\/app\/mysql\/data\///’`
for i in $binlog_cp
do
cp $path/$i /backup/$datadir/data/
done
binlog_rm=`tail -n 1 /app/mysql/data/mysql-bin.index |sed ‘s/\/app\/mysql\/data\///’`
mysql -uroot -p12334 -e “purge binary logs to ‘$binlog_rm'”

Author: Michael

关注数据分析,挖掘数据金矿。

Leave a Reply

Your email address will not be published. Required fields are marked *