通過shell匯出資料庫查詢結果到excel中,並通過郵件發送到郵箱

來源:互聯網
上載者:User

標籤:student   err   rom   linu   CM   編碼   username   mail.py   詳細   

通過shell匯出資料庫查詢結果到excel中,並通過郵件發送到郵箱#!/bin/bash#通過shell查詢資料資訊並儲存在excel中,並記錄日誌#Data:2018-06-14#Name:Zhang#資料庫連接地址DBServer='192.168.1.1'#資料庫使用者名稱DBUserName='zhang'#資料庫密碼DBPasswd='zhang'############################################################選擇資料庫Use_Cmd="use zhang"#查詢資料庫資訊Sql語Select_Cmd="SELECT * FROM STUDENTS;"############################################################後台支付資訊儲存路徑Payment_DataDir='/opt/select_back'#日誌儲存路徑LogDir=/opt/select_back/logs#資料匯出時間backtime=`date +%Y%m%d%H%M`#儲存檔案名稱DataName="支付訂單資訊"############################################################郵件收件者#Email_receiver_people="[email protected]"#郵件主題#Email_Subject="詳細資料$backtime"##########################################################MKDIR='/bin/mkdir'#########################################################echo "##################判斷備份路徑 #############################"test ! -d $Payment_DataDir && $MKDIR -p $Payment_DataDirtest ! -w $Payment_DataDir && echo "Error: $Payment_DataDir is un-writeable." && exit 0test ! -d $LogDir && $MKDIR -p  $LogDirtest ! -w $LogDir && echo "Error: $LogDir is un-writeable." && exit 0echo "######################備份開始 #############################"echo "" >> $LogDir/$DataName.logecho -e "\033[44;32m-------------------------分割線-----------------------\033[0m \n" >> $LogDir/$DataName.logecho "匯出時間為$backtime,匯出${DataName}檔案開始" >> $LogDir/$DataName.log/usr/local/mysql/bin/mysql -u$DBUserName -h$DBServer -p$DBPasswd  -e "$Use_Cmd;$Select_Cmd" > $Payment_DataDir/$DataName-$backtime.xls#sed -i 's/^/`&/g' $Payment_DataDir/$DataName-$backtime.xls#使用iconv轉換編碼格式,把linux的utf8轉換成windows的gbkiconv -f "utf-8" -t "gbk" $Payment_DataDir/$DataName-$backtime.xls > $Payment_DataDir/Orders.xls#因第一列資料超過18位,excel中使用了科學計數法,所以把第一列加了“`”轉換成文字格式設定sed -i 's/^/`&/g' $Payment_DataDir/Orders.xlsif [ "$?" == 0 ];then#把匯出結果通過郵件指令碼,發送到郵箱/opt/py3/bin/python3 /opt/select_back/select_mail.pyecho "匯出時間為$backtime,匯出$DataName檔案結束!!!" >> $LogDir/$DataName.logecho "Mysql-$DataName資料匯出成功!!!" >> $LogDir/$DataName.logelseecho "匯出時間為$backtime,匯出$DataName檔案結束!!!" >> $LogDir/$DataName.logecho "Mysql-$DataName資料導失敗!!!" >> $LogDir/$DataName.logfiecho "######################匯出資料結束,時間為:$backtime#####"

註:如以上有錯誤或者有不足的地方請指正

通過shell匯出資料庫查詢結果到excel中,並通過郵件發送到郵箱

相關文章

聯繫我們

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