龍巖易富通網(wǎng)絡(luò)科技有限公司

龍巖小程序開發(fā),龍巖分銷系統(tǒng)

shell自動(dòng)備份mysql數(shù)據(jù)庫

2016.01.14 | 1788閱讀 | 0條評(píng)論 | shell腳本


#!/bin/bash


#1.數(shù)據(jù)庫信息定義

echo  "Enter mysql password:"

read  PWD

mysql_host="localhost"

mysql_user="root"

mysql_passwd=$PWD


#sql備份目錄

back_dir="/home/bakdb"



if [ ! -d $back_dir ]; then

mkdir -p $back_dir

fi


#備份的數(shù)據(jù)庫數(shù)組

db_arr=$(echo "show databases;" | mysql -u$mysql_user -p$mysql_passwd -h$mysql_host)



#當(dāng)前日期

nowdate=`date +%Y-%m-%d`;



#2.進(jìn)入到備份目錄

cd $back_dir

mkdir $nowdate

cd $nowdate



#3.循環(huán)備份

for dbname in ${db_arr}

do

   if [ "$dbname" = "information_schema" ] || [ "$dbname" = "Database" ] || [ "$dbname" = "test" ] || [ "$dbname" = "performance_schema" ] || [ "$dbname" = "mysql" ]; then

       echo "no"  

    else

       sqlfile=$dbname".sql"

       a=`mysqldump -u$mysql_user -p$mysql_passwd -h$mysql_host $dbname >$sqlfile`

       echo $dbname

   fi

done


贊 (

發(fā)表評(píng)論