#!/bin/bash

set -o errexit
set -o nounset
set -o pipefail
lastmonth=$(date -d "1 month ago" +%Y-%m)

sh /data/drainer/archive_pb/archive_pb.sh >> /data/drainer/archive_pb/archive.log 2>&1

# configurations for mail.
FROM_ADDRESS="tidb@bankofbeijing.com.cn"
# For multiple addresses, use comma to seperate it.
TO_ADDRESS="tidb-alert@pingcap.com,wangxinyu@bankofbeijing.com.cn,liuyang@bankofbeijing.com.cn"
#TO_ADDRESS="tianshuang.qin@pingcap.com"
SUBJECT="archive PB incremental backup of $lastmonth"
#BODY="This is the message body and contains the message"
#SMTP_SERVER="smtp://smtp.mailtrap.io:25"
#SMTP_AUTH="login"
#SMTP_USER="3877fca4ffe7ce"
#SMTP_PASSWORD="9e434b999f6bc9"
SMTP_SERVER="smtp://mail.bj.bob:25"
SMTP_AUTH="login"
SMTP_USER="tidb@bankofbeijing.com.cn"
SMTP_PASSWORD="tidbtidb"

tail -12 /data/drainer/archive_pb/archive.log  | \
mailx -v -r "$FROM_ADDRESS" \
-s "$SUBJECT" \
-S smtp="$SMTP_SERVER" \
-S smtp-auth="$SMTP_AUTH" \
-S smtp-auth-user="$SMTP_USER" \
-S smtp-auth-password="$SMTP_PASSWORD" \
"$TO_ADDRESS"

sh /data/drainer/archive_pb/ftp.sh >> /data/drainer/archive_pb/archive.log 2>&1

tail -10 /data/drainer/archive_pb/archive.log  | \
mailx -v -r "$FROM_ADDRESS" \
-s "$SUBJECT" \
-S smtp="$SMTP_SERVER" \
-S smtp-auth="$SMTP_AUTH" \
-S smtp-auth-user="$SMTP_USER" \
-S smtp-auth-password="$SMTP_PASSWORD" \
"$TO_ADDRESS"