龍巖易富通網絡科技有限公司

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

shell自動備份mysql數據庫

2016.01.14 | 1792閱讀 | 0條評論 | shell腳本


#!/bin/bash


#1.數據庫信息定義

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


#備份的數據庫數組

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



#當前日期

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



#2.進入到備份目錄

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ā)表評論