批量备份mysql数据库(shell编程)
时间:2021-07-01 10:21:17
帮助过:34人阅读
=/
mysqlbak
MYUSER=
root
MYPASS=
7960224654321
SOCKET=/
var/lib/mysql/
mysql.sock
MYCMD=
"mysql -u$MYUSER -p$MYPASS -S $SOCKET"
MYDUMP=
"mysqldump -u$MYUSER -p$MYPASS -S $SOCKET"
[ ! -d
"$DBPATH" ] &&
mkdir $DBPATH
for dbname
in `$MYCMD -e
"show databases;" | sed
‘1d‘| egrep -v
"mysql|schema|accesslog"`
do
mkdir -p $DBPATH/${dbname}_$(date +%
F)
if [ `$MYCMD -e
"show tables from $dbname;" | wc -l` -gt
0 ]
then
for table
in `$MYCMD -e
"show tables from $dbname;" | sed
‘1d‘`
do
$MYDUMP $dbname $table | gzip > $DBPATH/${dbname}_$(date +%F)/
${dbname}_${table}.sql.gz
done
else
echo " $dbname is null! no DBbak "
fi
done
批量备份mysql数据库(shell编程)
标签:mysqldump mysq amp code for else bin bash shell编程