求开源脚本工具--执行sql生成excel并自动发送给指定接收人

求这样的开源脚本工具? 命令行中只需指定sql和邮件接收人

sendReport -sql 'select * from user where id in (...)' -mailTo 'aaa@foo.com,bbb@foo.com'

会自动将查询结果保存到一个Excel文件中 然后发给指定接收人。

阅读 2.6k
1 个回答

试验了一个简单的脚本,使用 MySQLmailgun

#!/bin/bash

REPORT_FILE=/tmp/report.csv
sudo rm -f $REPORT_FILE
mysql -uroot -proot --execute "$1 into outfile '$REPORT_FILE' fields terminated by ','" yizaoyiwan_dev
curl https://api.mailgun.net/v3/sandbox698c6abd28c14f05a4a4ae76fec18271.mailgun.org/messages \
    -s --user 'api:key-xxxxxxx' \
    -F from='yourname@gmail.com' \
    -F to="$2" \
    -F subject='New report here' \
    -F text='Here is the new report, check it.' \
    -F attachment=@$REPORT_FILE \

使用方法

sh send-report 'select * from posts' someone@example.com

如果没有服务器上 outfile 的权限,那可以这样

mysql -h db_host -uroot -proot db_name --execute "SELECT * FROM headlines" --silent --raw > /path/to/report.csv

其它部分相同(假定你用的是 linux 或者 osx),这部分的参考资料: http://code.openark.org/blog/...

logo
Microsoft
子站问答
访问
宣传栏