jenkins調用shell執行資料庫升級及備份

來源:互聯網
上載者:User

標籤:

###this is deploysql.sh###

#!/bin/bash
LANG="en_US.UTF-8"
date=`date +‘%Y%m%d_%H%M%S‘`
user=root
password=yourpassword
cd /root/SQLtest/
svn up
if [ $? = 0 ];then
    get_env=`cat /root/SQLtest/upgrade.sql |grep ^#ENV=|sed ‘s/#ENV=//g;s/.$//‘`
    get_host=`cat /root/SQLtest/upgrade.sql |grep ^#HOST=|sed ‘s/#HOST=//g;s/.$//‘`
    host=${get_host}
  if [ -n "${get_env"}] && [ -n "${get_host}"];then
    echo -e "正在擷取 ${get_env} ip:${get_host}需要備份的表...\n"
    backup_tables=`cat /root/SQLtest/upgrade.sql |grep ^#backup_tables=|sed ‘s/#backup_tables=//g;s/.$//‘`
    OLD_IFS="$IFS"
    IFS=","
    tables=($backup_tables)
    IFS="$OLD_IFS"
    if [ -n "$tables" ];then
      echo -e "您需要備份的表有:${tables[*]}\n"
      for table in ${tables[@]}
      do
    echo -e "mysqldump -uroot -pdbpasswd ${table%.*} ${table#*.} > ${table%.*}_${table#*.}_${date}.sql\n"
      done
      if [ $? -eq 0 ]
      then
        echo -e "備份成功!\n"
        echo -e "正在執行升級SQL...\n"
        mysql -u$user -p$password -h $host <<EOF
        source /root/SQLtest/upgrade.sql;
EOF
      else
        echo -e "備份失敗!\n"
      fi
    else        
      echo -e "警告:您沒有輸入任何需要備份的表!但是升級SQL依然執行...\n"
      echo -e "正在執行升級SQL...\n"
      mysql -u$user -p$password -h $host <<EOF
      source /root/SQLtest/upgrade.sql;
EOF
    fi
  else
    echo -e "ENV or HOST not found!\n"
  fi
else
    echo -e "svn update failed!\n"
fi

 


#this is testENV.sql#

################################聲明主機(營運維護)##############################################

#ENV=准生產環境
#HOST=192.168.1.1.

################################END###################################################




#################################聲明需要備份的表,建議對升級有操作的表進行備份,可以為空白,即不備份直接升級(開發維護)######################################

#請在下一行“backup_tables=”後輸入需要備份的表,格式:db.table多個之間用“,”分隔

#backup_tables=mysql.user,mysql.host

##################################END##################################################





################################提供升級操作SQL(開發維護)############################################
use mysql;
show tables;
################################END#####################################################

jenkins調用shell執行資料庫升級及備份

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.