-
Notifications
You must be signed in to change notification settings - Fork 0
/
restic-run-all.sh
executable file
·155 lines (137 loc) · 3.3 KB
/
restic-run-all.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
#!/bin/bash
# Creates backups of ZFS datasets with restic
# Uses healthchecks.io for signaling
# Version
VERSION="v0.2"
# Path
PATH="/usr/sbin:/usr/bin:/sbin:/bin"
# /usr/local/bin
if [ -d /usr/local/bin ]; then PATH="/usr/local/bin:$PATH"; fi
# /usr/local/sbin
if [ -d /usr/local/sbin ]; then PATH="/usr/local/sbin:$PATH"; fi
# OmniOS Community Edition
if [ -d /opt/ooce/bin ]; then PATH="/opt/ooce/bin:$PATH"; fi
# pkgsrc (https://pkgsrc.smartos.org/)
if [ -d /opt/local/bin ]; then PATH="/opt/local/bin:$PATH"; fi
export PATH
# echo to stderr
echoerr() { printf "%s\n" "$*" >&2; }
fnUsage() {
echoerr "USAGE: $0 [-c <config-file>]" 1>&2;
echoerr " -c <config-file>: path to config file" 1>&2;
exit 1;
}
# command line arguments
# https://wiki.bash-hackers.org/howto/getopts_tutorial
#echo $*
CONFIGFILE=""
while getopts "hc:" OPT; do
case $OPT in
c)
CONFIGFILE="$OPTARG"
;;
h)
fnUsage
;;
*)
fnUsage
;;
esac
done
# mandatory arguments
if [ ! "$CONFIGFILE" ]; then
fnUsage;
fi
# Absolute path to this script, e.g. /home/user/bin/foo.sh
SCRIPT=$(readlink -f "$0")
# Name of the script
SCRIPTNAME=$(basename "$SCRIPT")
# Hostname
HOSTNAME=$(hostname)
# runtime measurement
declare -i iSTARTTIME=0
declare -i iENDTIME=0
declare -i iRUNTIME=0
declare -i iRUNTIMEH=0
declare -i iRUNTIMEM=0
declare -i iRUNTIMES=0
iSTARTTIME=$(date +%s)
if [ -f "$CONFIGFILE" ]; then
source "$CONFIGFILE"
else
echoerr "$CONFIGFILE not found!"
exit 1
fi
fnSendStart () {
if [ ! "$1" ]; then
local MESSAGE="Empty message."
else
local MESSAGE="$1"
fi
echo "$MESSAGE"
# Ping Healthchecks.io
if [ -n "$HC_URL" ]; then
echo -n "Connecting to healthchecks.io: "
curl -fsS --retry 3 --data-raw "$SCRIPTNAME $VERSION: $MESSAGE" "$HC_URL/start"
echo ""
fi
}
fnSendSuccess () {
if [ ! "$1" ]; then
local MESSAGE="Empty message."
else
local MESSAGE="$1"
fi
echo "$MESSAGE"
# Ping Healthchecks.io
if [ -n "$HC_URL" ]; then
echo -n "Connecting to healthchecks.io: "
curl -fsS --retry 3 --data-raw "$SCRIPTNAME $VERSION: $MESSAGE" "$HC_URL"
echo ""
fi
}
fnSendError () {
if [ ! "$1" ]; then
local MESSAGE="Undefined error!"
else
local MESSAGE="$1"
fi
echoerr "$MESSAGE"
# Ping Healthchecks.io
if [ -n "$HC_URL" ]; then
echo -n "Connecting to healthchecks.io: "
curl -fsS --retry 3 --data-raw "$SCRIPTNAME $VERSION: $MESSAGE" "$HC_URL/fail"
echo ""
fi
}
fnSendStart "Starting backups: $HOSTNAME"
# Find all .conf files but exclude .all.conf files
CONF_FILES=$(find "$SEARCH_DIR" -type f -name "*.conf" ! -name "*.all.conf")
# Check if any .conf files were found
if [ -z "$CONF_FILES" ]; then
fnSendError "No .conf files found."
exit 1
fi
declare -i iERROR_COUNT=0
ERROR_CONFS=""
# Loop through each .conf file
for CONF_FILE in $CONF_FILES; do
if ! "$RESTIC_DATASET_BACKUP" -c "$CONF_FILE"; then
iERROR_COUNT+=1
ERROR_CONFS="${CONF_FILE}, $ERROR_CONFS"
fi
echo ""
echo "---"
echo ""
done
# runtime measurement
iENDTIME=$(date +%s)
iRUNTIME=$iENDTIME-$iSTARTTIME
iRUNTIMEH=${iRUNTIME}/3600
iRUNTIMEM=(${iRUNTIME}%3600)/60
iRUNTIMES=${iRUNTIME}%60
if [ -n "$ERROR_CONFS" ]; then
fnSendError "Errors: $iERROR_COUNT; Configs: $ERROR_CONFS; Run time: $(printf "%02d:%02d:%02d" $iRUNTIMEH $iRUNTIMEM $iRUNTIMES)."
else
fnSendSuccess "OK! Run time: $(printf "%02d:%02d:%02d" $iRUNTIMEH $iRUNTIMEM $iRUNTIMES)."
fi