-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup-restore-mailer.sh
executable file
·74 lines (63 loc) · 2.07 KB
/
backup-restore-mailer.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/bash
LOGDIR="$1"
LOGFILE="$LOGDIR/backup-restore.log"
if [ ! -d "$LOGDIR" ] || [ ! -f "$LOGFILE" ];then
echo "Usage: backup-restore-mailer.sh <log dir>"
exit 1
fi
LOGDIR="$LOGDIR"
MJ_APIKEY_PUBLIC=""
MJ_APIKEY_PRIVATE=""
SENDER_EMAIL="[email protected]"
SENDER_NAME="Sender name"
RECIPIENT_EMAIL="[email protected]"
RECIPIENT_NAME="Recipient name"
SUBJECT="Example subject"
TEXT="Only html"
date=$(date +"%Y-%m-%d")
CSS="
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
text-align: left;
padding: 8px;
}
tr:nth-child(even){background-color: #f2f2f2}
th {
background-color: #4CAF50;
color: white;
}
</style>
"
HTML="<!DOCTYPE html><html>
<head>$CSS</head>
<body><h2>Backup-Restore $date</h2></br><table border='1'><tr><th>Name</th><th>Start</th><th>Finnish</th><th>Tag</th><th>Status</th></tr>"
HTML+=$(awk -F',' -v pattern="^$date" '$2 ~ pattern { sub(/^.*T/,"" , $2);sub(/^.*T/,"" , $3); print "<tr><td>" $1 "</td><td> " $2 " </td><td> " $3 "</td><td>" $4 "</td><td>" $5 "</td></tr>" }' "$LOGFILE" )
HTML+="</table><br /><br />For more information and troubleshooting, see <a href='https://github.com/dud380/mysql-backup-restore'>MySQL-Backup-Restore</a>.</body></html>"
# remove all carriage returns. It will break json otherwise
HTML=$(echo $HTML|tr -d '\n')
# TODO - check json if failed
curl -s -X POST --user "$MJ_APIKEY_PUBLIC:$MJ_APIKEY_PRIVATE" https://api.mailjet.com/v3.1/send \
-H 'Content-Type: application/json' \
-d "{
\"Messages\":[
{
\"From\": {
\"Email\": \"$SENDER_EMAIL\",
\"Name\": \"$SENDER_NAME\"
},
\"To\": [
{
\"Email\": \"$RECIPIENT_EMAIL\",
\"Name\": \"$RECIPIENT_NAME\"
}
],
\"Subject\": \"$SUBJECT\",
\"TextPart\": \"$TEXT\",
\"HTMLPart\": \"$HTML\"
}
]
}" > /dev/null