1. 程式人生 > >mysql備份每個庫下面每個表

mysql備份每個庫下面每個表

mysql備份每個庫下面每個表

#!/bin/bash
MYUSER="root"
MYPASS="123456"
HOST="localhost"
BACKUPDIR="/backup_mysql"
DATE=`date -I`
RETENTION=20
#MYSQLDATA="/var/lib/mysql"
[ ! -d $BACKUPDIR/$DATE ] && mkdir -p $BACKUPDIR/$DATE
DBS=`mysql -u$MYUSER -p$MYPASS -Bse "show databases"|grep -v "information_schema" |grep -v "test"`

for db_name in $DBS
do
mkdir -p $BACKUPDIR/$DATE/$db_name
tables=`mysql -u$MYUSER -p$MYPASS -Bse "use $db_name;show tables"`
for table_name in $tables
do
mysqldump -u$MYUSER -p$MYPASS -h$HOST --single-transaction --master-data=2 $db_name $table_name >$BACKUPDIR/$DATE/$db_name/$table_name.sql
done
done

mysql備份每個庫下面每個表