备份数据进行定时邮件发送
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
原文地址:http://blog.51cto.com/13922718/2163390
时间: 2024-10-08 10:29:40