Iremos gerar um relatório dinâmico por partição de acordo com o mês, exportando dados em csv. (MySQL).
Script:
cat /root/relatorio.sql
SET @final = DATE_SUB(last_day(sysdate()), interval 1 month);
SELECT CONCAT('p',DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 1 MONTH),'%Y%m'))
INTO @partitions;
SET @query =
CONCAT('
SELECT *
INTO OUTFILE ''/opt/csv/teste1_',@partitions,'.csv'' FIELDS TERMINATED BY '','' OPTIONALLY ENCLOSED BY ''"'' LINES TERMINATED BY ''\n''
from atm.averbacao partition
(',@partitions,') where av_data_emiss between DATE_SUB(ADDDATE(LAST_DAY(DATE_SUB(CURDATE(), INTERVAL 1 MONTH)), 1), interval 1 month) and DATE_SUB(last_day(sysdate()), interval 1 month) limit 10;');
PREPARE stmt FROM @query;
EXECUTE stmt;
ShellScriptCrontab:
# Crontab
05 15 * * * /usr/bin/mysql testdb < /root/relatorio.sql
ShellScript