#!/bin/bash
#MYSQL 全量备份，建议在salve从库上运行，并开启log_slave_updates = 1

if [ ! -d /backup ];then
	mkdir /backup;
fi
cd /backup
dateDIR=`date +"%y-%m-%d"`
mkdir $dateDIR/data -p
# database directory
path=/database/mysql

user=root
password=vqiuvqiu

for i in `/usr/local/mysql/bin/mysql -u$user -p$password -e "show databases" | grep -v "Database" | grep -v "performance_schema"`
do
mysqldump -u$user -p$password --default-character-set=utf8 -q --lock-all-tables --flush-logs -E -R --triggers -B $i | gzip >/backup/$dateDIR/data/${i}_${dateDIR}.sql.gz
done

binlog_rm=`tail -n 1 $path/mysql-bin.index | sed 's/.\///'`
/usr/local/mysql/bin/mysql -u$user -p$password -e "purge binary logs to '$binlog_rm'"
