-
Notifications
You must be signed in to change notification settings - Fork 0
/
unattended-snapraid.sh
executable file
·135 lines (101 loc) · 2.88 KB
/
unattended-snapraid.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
#!/bin/bash
#
# unattended-snapraid.sh
# © COPYRIGHT 2019 by T.Magerl <dev@muab.org>
# LICENCE: CC BY-NC-SA 4.0 (see licence-file included)
#
self_config="$(dirname $0)/unattended-snapraid.conf"
if [[ ! -f "$self_config" ]]; then
log_path=""
verify_percentage=3
auto_remove_logs=true
notice="'$self_config' not found\nedit and rename 'unattended-snapraid.conf-SAMPLE'.\nusing default settings.\n\n"
else
. "$(dirname $0)"/unattended-snapraid.conf
fi
if pidof -o %PPID -x "$0">/dev/null; then exit 0; fi
snapraid=$(which snapraid)
if [[ $snapraid == "" ]]; then
echo -e "snapraid not found\nbye"
exit 1
fi
if [[ -z $1 ]] || [[ ! -f "$1" ]]; then
echo -e "unattended-snapraid script\n\nusage:\n\n unattended-snapraid.sh \"/foo/bar/snapraid.conf\" [opt: (integer) percent to verify]\n"
exit 1
fi
config="$1"
if [[ -z $2 ]]; then
scheduledcheck=$verify_percentage
fi
DATUM=`date +%y%m%d`
if [[ "$log_path" == "" ]] || [[ ! -d "$log_path" ]]; then
log_path="$(dirname $config)"
fi
if [[ $auto_remove_logs ]] && [[ $( ls -t "$log_path/$(basename $config)"_??????.log | awk 'NR>7' ) != "" ]]; then
rm $( ls -t "$log_path/$(basename $config)"_??????.log | awk 'NR>7' )
fi
log_file="$log_path/$(basename $config)_$DATUM.log"
debug_file="$log_path/$(basename $config)_$DATUM.tmp"
echo -e "### snapraid results for '$config', created by unattended-snapraid\n" > "$log_file"
if [[ $notice ]]; then
echo -e "$notice" >> "$log_file"
fi
action() {
if [[ $r =~ "Nothing to do" ]]; then
return 1
else
return 0
fi
}
important() {
if [[ $r =~ "Nothing to do" ]]; then
return 1
elif [[ $r =~ "No rehash is in progress or needed." ]] && \
[[ $r =~ "No error detected." ]]; then
return 1
else
return 0
fi
}
log() {
echo -e "\n$@" >> "$log_file"
echo -e "\nunattended-snapraid '$config': $@"
}
snap() {
r=$($snapraid -c "$config" $@)
exit_code=$?
if [[ ! $exit_code == 0 ]]; then
log "\nsnapraid (not unattended-snapraid) failed execution:\n\n$r\n\nplease test snapraid manually"
exit 2
fi
if important; then
log "$r"
fi
}
log "syncing changes"
snap sync >>/dev/null
if action; then
log "scrubbing recent sync"
snap -p new scrub >>/dev/null
else
log "no changes found, scrubbing bad blocks"
snap -p bad scrub >>/dev/null
if ! action; then
log "no bad blocks found, scheduled scrubbing ($scheduledcheck%)"
fi
fi
r=$($snapraid -c "$config" touch)
sync
snap status 2>/dev/null \
| grep -v "|" \
| grep -v "last scrub" \
| grep -v "Loading state" \
| grep -v "Self test" \
| grep -v "of memory"
if [[ $(cat "$log_file") =~ "snapraid -e fix" ]]; then
log "trying to fix found errors"
snap -e fix
fi
snap smart 2>/dev/null
echo "unattended-snapraid: results saved to '$log_file'"
exit 0