首页 > 代码库 > shell备份数据库

shell备份数据库

/bin/bash

BAKPATH=/opt/mysql

MYUSER=root

MYPASSWD=123456

SOCKET=/data/3308/mysql.sock

MYCMD="mysql -u$MYUSER -p$MYPASSWD -S $SOCKET"

MYSQLDUMP=mysqldump -u$MYUSER -P$MYPASSWD -S $SOCKET  -x -F -R

[ ! -d $BAKPATH ] && mkdir -p $BAKPATH

DBLIST=`$MYCMD -e "show databases; | sed 1d" |egrep -Evi "mysql|info" `

for dbname in DBLIST

do

        TLIST=`$MYCMD -e "show tables from $dbname;" | sed 1d`

        for tname in $TLIST

        do

                mkdir -p $BAKPATH/$dbname

                $MYSQLDUMP $dbname $tname |gzip >/$BAKPATH/$dbname/${tname}_$(date +%F).sql.gz

        done

        $MYSQLDUMP  $dbname |gzip > /$BAKPATH/${dbname}_$()$(date +%F).sql.gz

done


本文出自 “linux运维” 博客,转载请与作者联系!

shell备份数据库