针对单主机多实例的MySQL完整备份脚本,根据自己的情况,自行修改即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
#!/bin/bash # # backup user USERNAME= PASSWORD= SOCKET= # create variables today=`date +'%Y-%m-%d-%H'` sevenday=`date -d '7 day ago' +'%Y-%m-%d-%H'` # create backup directory dir="3306" backup_dir=/backup for i in $dir;do mkdir $backup_dir/$i/binlog -p done # backup full data for i in $dir;do DB=`mysql -u"$USERNAME" -p"$PASSWORD" -e "show databases;" | egrep -v "(Database|information_schema|mysql|performance_schema|sys|test)"` for db in $DB;do mysqldump -U"$USERNAME" -p"$PASSWORD" --single-transaction --master-data=2 <span class="crayon-o">--</span><span class="crayon-v">flush</span><span class="crayon-o">-</span><span class="crayon-v">logs --databases </span>$db > $backup_dir/$i/$i-${db}-${today}.sql done sleep 3 done # backup binlog # rsync -vzrtopg /apps/mysql/kupai/data/master-bin* /backup/kupai/binlog # delete seven days before for i in $dir;do rm -rf $backup_dir/$i/$i-$i-${sevenday}.sql done # rsync to remote server for i in $dir;do rsync -avzc /backup/$i/$i-${today}.sql 10.106.33.2::backup/mysql/10.0.30.65 &> /dev/null done |
待完结。。