mysqlDatabase Backup與恢複指令碼

來源:互聯網
上載者:User

標籤:Database Backup   database   mysql   xxxxx   media   

#!/bin/bash

#mysql database backup and restore the script


# define config variable

host=localhost

user=root

passwd=xxxxx

#this variable for grant

mainHostIp="127.0.0.1"

base_path=/opt/disk-webData/mysqlBackup

# base_path=/media/sf_website/yii/disk-webData/mysqlBackup



### backup

backupdb() {


  bak_path=$base_path/$(date +%Y%m%d)/data

  log_path=$base_path/$(date +%Y%m%d)/log


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

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


  dblist=($(mysql -u$user -p$passwd -e "show databases;"|awk -F "|" ‘{print $1}‘|grep -Ev "Database|mysql|information_schema|performance_schema"))


  i=1

  for dbname in "${dblist[@]}"

  do

    echo -e "\n$i --> Backup database $dbname ..."

    file=$bak_path/"$dbname.sql"

    mysqldump -h$host -u$user -p$passwd -B $dbname > $file

    if [ -f $file ];then

      success="$i --> $dbname backup success!"

      echo $success;

      echo $success >> $log_path/backupLog.log

      echo "GRANT ALL PRIVILEGES ON $dbname.* TO [email protected];" >> $log_path/grant.log

      echo "GRANT ALL PRIVILEGES ON $dbname.* TO [email protected]$mainHostIp;" >> $log_path/grant.log

    fi

    let i++

  done


}



#restore

expr 1 + $1 &>/dev/null

if [ $? -eq 0 ];then


  bak_path="$base_path"/$1/data

  log_path="$base_path"/$1/log


  #restore

  cd $bak_path

  dblist=($(ls))

  i=1

  for dbname in "${dblist[@]}"

  do

    echo -e "\n$i --> Restore database $dbname ..."

    mysql -h$host -u$user -p$passwd < $dbname

    success="$i --> $dbname restore success!"

    echo $success

    echo $success >> $log_path/restoreLog.log

    let i++

  done


  #grant

  # if [ -f $log_path/grant.log ];then

    echo "Please ensure that [admin] user exist @[localhost & 127.0.0.1 & mainHostIp]";

    sleep 15s

    ii=1

    cat $log_path/grant.log | while read line

    do

      echo "$ii --> $line"

      mysql -h$host -u$user -p$passwd -e "$line"

      let ii++

    done

  # fi



elif [ "$1" == backup ];then


  backupdb


else


  cd $base_path

  dblist=($(ls))

  i=1;

  echo "Backup history ..."

  for dbname in "${dblist[@]}"

  do

    echo "$i --> $dbname"

    let i++

  done


fi


本文出自 “11002498” 部落格,請務必保留此出處http://11012498.blog.51cto.com/11002498/1883947

mysqlDatabase Backup與恢複指令碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.