-
Notifications
You must be signed in to change notification settings - Fork 1
/
borgsphere.sh
executable file
·226 lines (175 loc) · 5.35 KB
/
borgsphere.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/bin/sh
#########################################################################################################
# ___________
# /-/_"/-/_/-/|
# /"-/-_"/-_//||
# /__________/|/|
# |"|_'='-]:+|/||
# |-+-|.|_'-"||//
# |[".[:!+-'=|// borgsphere version 0.1.2
# |='!+|-:]|-|/ https://github.com/fti7/borgsphere
# ----------
#
#################
BS_VERSION="0.1.2"
# Prefer manual installed Version of Borg
PATH="/usr/local/bin:$PATH"
#
# Helper Functions
#
send_mail()
{
local tmpfile=$(mktemp -t borgmail)
printf "Subject: $1\n" > "$tmpfile"
printf "To: $2\n" >> "$tmpfile"
printf "From: $3\n" >> "$tmpfile"
printf "\n" >> "$tmpfile"
if [ -f "$4" ]
then
cat "$4" >> "$tmpfile"
printf "\n" >> "$tmpfile"
else
printf "$4\n" >> "$tmpfile"
fi
sendmail -t < "$tmpfile"
[ $? -eq 0 ] && rm $tmpfile
echo "OK"
}
matches() {
input="$1"
pattern="$2"
echo "$input" | grep -q "$pattern"
}
#
# Main Code
#
if [ $# -eq 0 ]; then
echo "Usage: $0 jobfile [jobfile...]"
exit 1
fi
for cfg in "$@"
do
CFG_NAME=$(basename "$cfg")
# Set Defaults
BACKUP_ID="example"
BACKUP_DIRS="/root"
BACKUP_EXCLUDE="--exclude-caches"
BORG_REPOSITORY="/path/to/repo"
RETRIES_ON_ERROR=10
BORG_OPTS="--list --filter=AME --lock-wait=15 --compression lz4"
BORG_FILES_CACHE_TTL=2000
BORG_PRUNE_ENABLED=false
BORG_PRUNE_OPTS="--keep-hourly=24 --keep-daily=7 --keep-weekly=4 --keep-monthly=12"
MAIL_ENABLED=false
MAIL_FROM="[email protected]"
MAIL_TO="[email protected]"
MAIL_SUBJECT_OK="OK | BorgBackup $CFG_NAME"
MAIL_SUBJECT_WARN="WARN | BorgBackup $CFG_NAME"
MAIL_SUBJECT_ERROR="ERROR | BorgBackup $CFG_NAME"
MAIL_SUBJECT_DEDUPSIZE=true
HOOK_PRE=""
HOOK_POST_OK=""
HOOK_POST_ERROR=""
LOG_FILE="/tmp/borgsphere.$BACKUP_ID.$(date +'%Y-%m-%d-%H%M%S').log"
# Load Config
. $cfg
# Main Code
echo "config: $cfg | borgsphere $BS_VERSION" >> $LOG_FILE
echo "Backup Directories: $BACKUP_DIRS" >> $LOG_FILE
echo "Destination Repository: $BORG_REPOSITORY" >> $LOG_FILE
echo "------------------------------------------------------------------------------" >> $LOG_FILE
echo "" >> $LOG_FILE
if [ -z "$BACKUP_ID" ]
then
echo "BACKUP_ID not set... skipping"
continue
fi
# Pre Hook
if [ -n "$HOOK_PRE" ]
then
echo "## Exec Pre-Hook" >> $LOG_FILE
echo "cmd: $HOOK_PRE" >> $LOG_FILE
$HOOK_PRE >> $LOG_FILE 2>&1
echo "## End Pre-Hook rc=$?" >> $LOG_FILE
echo "" >> $LOG_FILE
fi
# Backup
[ -e ${BORG_PASSPHRASE} ] && export BORG_PASSPHRASE
export BORG_FILES_CACHE_TTL
for i in `seq 1 $RETRIES_ON_ERROR`;
do
echo "## Backup started $(date +'%H:%M:%S %Y-%m-%d') #$i" >> $LOG_FILE
BORG_CMD="borg create --verbose --stats --show-rc $BORG_OPTS $BORG_REPOSITORY::"{hostname}-${BACKUP_ID}-{now:%Y-%m-%d_%H:%M:%S}" $BACKUP_DIRS $BACKUP_EXCLUDE"
echo "cmd: $BORG_CMD" >> $LOG_FILE
$BORG_CMD >> $LOG_FILE 2>&1
BORG_RC=$?
echo "## Backup finished $(date +'%H:%M:%S %Y-%m-%d') rc=$BORG_RC" >> $LOG_FILE
if [ $BORG_RC -lt 2 ]
then
break
else
echo "Error - Retry $i/$RETRIES_ON_ERROR" >> $LOG_FILE
sleep 60
fi
done
echo "" >> $LOG_FILE
# Prune old Archives
if [ "$BORG_PRUNE_ENABLED" = "true" -a $BORG_RC -lt 2 ]
then
echo "## Prune Repository with options: $BORG_PRUNE_OPTS" >> $LOG_FILE
borg prune -v --list $BORG_REPOSITORY --prefix "{hostname}-${BACKUP_ID}-" $BORG_PRUNE_OPTS >> $LOG_FILE 2>&1
echo "OK" >> $LOG_FILE
echo "" >> $LOG_FILE
fi
echo "## List all Borg Archives in Repository" >> $LOG_FILE
borg list $BORG_REPOSITORY >> $LOG_FILE
echo "" >> $LOG_FILE
# Post Hook
if [ $BORG_RC -lt 2 ]
then
if [ -n "$HOOK_POST_OK" ]
then
echo "## Exec Post-Hook-OK" >> $LOG_FILE
echo "cmd: $HOOK_POST_OK" >> $LOG_FILE
$HOOK_POST_OK >> $LOG_FILE 2>&1
echo "## End Post-Hook-OK rc=$?" >> $LOG_FILE
echo "" >> $LOG_FILE
fi
else
if [ -n "$HOOK_POST_ERROR" ]
then
echo "## Exec Post-Hook-ERROR" >> $LOG_FILE
echo "cmd: $HOOK_POST_ERROR" >> $LOG_FILE
$HOOK_POST_ERROR >> $LOG_FILE 2>&1
echo "## End Post-Hook-ERROR rc=$?" >> $LOG_FILE
echo "" >> $LOG_FILE
fi
fi
if [ "$MAIL_ENABLED" = "true" ]
then
echo "## Send Mail Notification" >> $LOG_FILE
SUBJ=""
if [ $BORG_RC -eq 0 ]
then
SUBJ="$MAIL_SUBJECT_OK"
elif [ $BORG_RC -eq 1 ]
then
SUBJ="$MAIL_SUBJECT_WARN"
elif [ $BORG_RC -gt 1 ]
then
SUBJ="$MAIL_SUBJECT_ERROR"
fi
# Append Dedupsize?
if [ $BORG_RC -lt 2 -a "$MAIL_SUBJECT_DEDUPSIZE" = "true" ]
then
DEDUP_SIZE=$(grep "This archive" $LOG_FILE | head -n1 | awk '{print $7, $8}')
# Hide if just Metadata
if ! matches "$DEDUP_SIZE" "kB"
then
SUBJ="$SUBJ - $DEDUP_SIZE"
fi
fi
# Send Mail
send_mail "$SUBJ" "$MAIL_TO" "$MAIL_FROM" "$LOG_FILE" >> $LOG_FILE 2>&1
fi
done