mysql 备份脚本

[root@mysql]#cat mysql_full_bak.sh

#!/bin/sh

date=`date +%Y-%m-%d`

olddate=`date +%Y-%m-%d -d "-14 days"`

data_dir=/opt/mysql/var

bak_dir=/data_bak

dbname=t_test

user=root

pass=test

mkdir -p /data_bak/$dbname/$date

mysqldump --opt -uroot -ptest $dbname | gzip > $bak_dir/$dbname/$date/$dbname-$date.sql.gz

test -d $bak_dir/$dbname/$olddate && rm -f $bak_dir/$dbname/$olddate

mysqladmin -uroot -ptest flush-logs

rm_binlog=`tail -n1 $data_dir/mysql-bin.index | cut -d "/" -f2`

mysql -uroot -ptest -e "purge binary logs to '$rm_binlog'"

[root@mysql]#cat mysql_daily_bak.sh

#!/bin/sh

date=`date +%Y-%m-%d`

data_dir=/opt/mysql/var

bak_dir=/data_bak

dbname=t_test

user=root

pass=test

mkdir -p /data_bak/$date

mysqladmin flush-logs

n=`wc -l $data_dir/mysql-bin.index | cut -d " " -f1`

i=1

for file in `cat $data_dir/mysql-bin.index | cut -d "/" -f2`

do

if [ "$i" != "$n" ];then

cp $data_dir/$file $bak_dir/$date

fi

((++i))

done

rm_binlog=`tail -n1 $data_dir/mysql-bin.index | cut -d "/" -f2`

mysql -uroot -ptest -e "purge binary logs to '$rm_binlog'"