1. 程式人生 > >數據庫指定庫表中的字段進行備份,並以表格的形式定時發送郵件到指定郵箱 ?

數據庫指定庫表中的字段進行備份,並以表格的形式定時發送郵件到指定郵箱 ?

路徑 user pass 騰訊 .sql 自動生成 cron 信息 騰訊企業

備份數據進行定時郵件發送
mkdir -p /opt/module/shell/
mkdir -p /opt/module/shell/sql/revenue.sql
mkdir -p /opt/module/shell/csv/revenue
#MailTool.jar 放在指定目錄下 便於發送郵件使用 可自動生成表格 
vim /opt/module/shell/daily_text.sh
# !/bin/bash
#數據庫指定庫.表中的字段進行備份,並以表格的形式發送郵件到指定郵箱
#定義變量
dat=`date -d ‘1 days ago‘ +%Y-%m`
basepath=/opt/module/shell
sql_revenue=${basepath}/sql/revenue.sql
csv_revenue=${basepath}/csv/revenue/${dat}.csv
filename=`date +%Y-%m`
# html_revenue=${basepath}/html/revenue/${dat}.html
#current=`date -d last-day "+%Y-%m-%d"`
#timeStamp=`date -d "$current" +%s`
# 將current轉換為時間戳,精確到毫秒
#currentTimeStamp=$((timeStamp*1000))
#current2=`date "+%Y-%m-%d"`
#time2Stamp=`date -d "$current2" +%s`
# 將current轉換為時間戳,精確到毫秒
#current2TimeStamp=$((time2Stamp*1000))

#具體的sql語句 備份某個庫裏的某個表中的指定字段
sql_revenue_total="select date,tb_card_no,goodsId,outItemSKu,money from itoyo_2018.o_jujibao_record Order by date"

#數據庫的登陸ip,端口,用戶,密碼
mysql=/usr/bin/mysql
host=數據庫ip
port=3306
username=數據庫指定用戶名
password=密碼

#將備份的內容導出到定義好的變量文件中
$mysql -h$host -P$port -u$username  -p$password  -e "$sql_revenue_total" > $csv_revenue
total=`$mysql -h$host -P$port -u$username -p$password -e "$sql_revenue_total" | tail -n+2`
echo -e "$total" >> $csv_revenue
sed -i ‘s/\t/","/g‘ $csv_revenue
sed -i ‘s/^/"/g‘    $csv_revenue
sed -i ‘s/$/"/g‘    $csv_revenue
sed -i ‘2,3d‘ $csv_revenue

#過濾出備份文件中當月的信息到指定文件
cat $csv_revenue | grep -E "(date|$filename)" |sed ‘$d‘ > $csv_revenue.bak
#編輯郵件內容及格式
confile=${basepath}/conf.properties
rm -f $confile
    #這裏我用的是騰訊企業郵箱,exmail.qq.com  郵箱端口為465
echo  "mailhost=smtp.exmail.qq.com"                                       > $confile
echo  "mailport=465"                                                      >> $confile
echo  "username=郵箱地址"                              >> $confile
echo  "password=郵箱密碼"                                                 >> $confile
echo  "from=發件人郵箱"                                  >> $confile
echo  "to=收件人郵箱" >> $confile
echo  "#cc="                                                              >> $confile
echo  "#bcc="                                                             >> $confile
echo  "subject=標題 $dat"                                           >> $confile
echo  "content=<p>本月數據<br><table>#table_revenue#</table><br></p> " >> $confile
echo  "table_revenue=$csv_revenue.bak"                                    >> $confile
echo  "file=$csv_revenue.bak"                                             >> $confile
#發送郵件  並-mail指定郵件內容  要執行計劃任務 必須寫絕對路徑
/data/tools/jdk1.7.0_67/bin/java -jar /opt/module/shell/MailTool.jar -mail $confile

定時計劃任務

#該daily_text.sh腳本必須給執行權限
crontab -e
00 18 30 * * . /opt/module/shell/daily_text.sh

數據庫指定庫表中的字段進行備份,並以表格的形式定時發送郵件到指定郵箱 ?