-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail.sh
143 lines (121 loc) · 3.91 KB
/
email.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/bin/bash
#
# Copyright (C) 2013 Dan Fruehauf <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# TODO hardcoded
declare -r DEFAULT_EMAIL_HOST=localhost
declare -i -r DEFAULT_EMAIL_PORT=25
# notifies via email
# $1 - backup model name
# $2 - backup retval
# $3 - temporary file holding the names of the failed backups
# $4 - email address
# $5 - email host (using localhost if not specified)
# $6 - email port (using 25 if not specified)
backup() {
local backup_model=$1; shift
local -i backup_retval=$1; shift
local failed_backups_tmp_file=$1; shift
local email_address="$1"; shift
local email_host=$1; shift
local -i email_port=$1; shift
[ x"$email_host" = x ] && email_host=$DEFAULT_EMAIL_HOST
[ $email_port -eq 0 ] && email_port=$DEFAULT_EMAIL_PORT
local email_subject=""
local tmp_file_email_content=`mktemp`
if [ $backup_retval -gt 0 ]; then
# if the backup failed
email_subject="Backup failed for '$backup_model'"
cat > $tmp_file_email_content <<EOF
Sorry mate, unfortunately your backup failed.
Backup model: '$backup_model'
EOF
echo -n "Backup modules failed: '" >> $tmp_file_email_content
cat $failed_backups_tmp_file | xargs >> $tmp_file_email_content
else
# if the backup succeeded
email_subject="Backup succeeded for '$backup_model'"
cat > $tmp_file_email_content <<EOF
Success! Backup succeeded!
Backup model: '$backup_model'
EOF
fi
# and... send the email
_send_email $email_host $email_port \
backup@`hostname` $email_address \
"$email_subject" $tmp_file_email_content
# remove temporary file
rm -f $tmp_file_email_content
}
# notifies via email
# $1 - backup model name
# $2 - backup retval
# $3 - temporary file holding the names of the failed backups
# $4 - email address
# $5 - email host (using localhost if not specified)
# $6 - email port (using 25 if not specified)
restore() {
local backup_model=$1; shift
local -i backup_retval=$1; shift
local failed_backups_tmp_file=$1; shift
local email_address="$1"; shift
local email_host=$1; shift
local -i email_port=$1; shift
[ x"$email_host" = x ] && email_host=$DEFAULT_EMAIL_HOST
[ $email_port -eq 0 ] && email_port=$DEFAULT_EMAIL_PORT
local email_subject=""
local tmp_file_email_content=`mktemp`
if [ $backup_retval -gt 0 ]; then
# if the backup failed
local email_subject="Restore failed for '$backup_model'"
cat > $tmp_file_email_content <<EOF
Sorry mate, unfortunately your backup failed.
Backup model: '$backup_model'
EOF
echo -n "Backup modules failed: '" >> $tmp_file_email_content
cat $failed_backups_tmp_file | xargs >> $tmp_file_email_content
else
# if the backup succeeded
cat > $tmp_file_email_content <<EOF
Success! Restore succeeded!
Backup model: '$backup_model'
EOF
fi
# and... send the email
_send_email $email_host $email_port \
backup@`hostname` $email_address \
"$email_subject" $tmp_file_email_content
# remove temporary file
rm -f $tmp_file_email_content
}
# send an email using nc
# $1 - host
# $2 - port
# $3 - from
# $4 - to
# $5 - subject
# $6 - content file
_send_email() {
local host=$1; shift
local -i port=$1; shift
local from=$1; shift
local to=$1; shift
local subject="$1"; shift
local email_content_file="$1"; shift
(export smtp=$host:$port; cat $email_content_file | \
mail -s "$subject" -r $from $to)
}