Liunx備份mysql資料庫的shell指令碼

程式設計小白李智障發表於2020-12-03
#!/bin/bash

# 關閉java程式
javapid=`ps aux|grep java|grep -v grep|awk '{print $2}'`
for i in ${javapid}
  do
    kill -9 $i
  done
echo "------closed java pid------"

echo "------start backup mysql,input IP------"

# 備份資料庫
read -p "input IP: " ipAdd
echo "------your input IP: "$ipAdd"------"
read -p "yes[y],quit[enter]: " num
if [[ "$num" = y ]] || [[ "$num" = "Y" ]]
then
  echo "------starting backup------"
  mysql -h$ipAdd -usunbox -pabcd1234! -e "show databases;" >/dev/null 2>&1
  if [ $? -eq 0 ]
  then
    /usr/local/mysql/bin/mysqldump -h$ipAdd -usunbox -pabcd1234! store >/root/back.sql
  else
    echo "database IP error........"
    exit
  fi
fi

echo "------delete store------"
mysql -usunbox -pabcd1234! -e 'drop database store;'
echo "------create store------"
mysql -usunbox -pabcd1234! -e 'create database store character set utf8;'
echo "------restore mysql------"
mysql -u使用者 -p資料庫密碼 資料庫名 </root/back.sql
echo "------backup OK.------"

相關文章