千家信息网

mysql备份每个库下面每个表

发表于:2025-01-31 作者:千家信息网编辑
千家信息网最后更新 2025年01月31日,#!/bin/bashMYUSER="root"MYPASS="123456"HOST="localhost"BACKUPDIR="/backup_mysql"DATE=`date -I`RETENT
千家信息网最后更新 2025年01月31日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

0