批量备份mysql数据库(shell编程)

#!/bin/bash

DBPATH=/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

 

posted @ 2017-07-01 15:35  奶奶灰与爷爷白  阅读(349)  评论(0编辑  收藏  举报