首页 > 代码库 > 批量备份mysql数据库(shell编程)

批量备份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

 

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