-
Notifications
You must be signed in to change notification settings - Fork 0
/
parallel-zrb.sh
executable file
·167 lines (123 loc) · 3.54 KB
/
parallel-zrb.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
#!/bin/bash
#set -x
# default variables
export PATH="/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
export QUIET_NOTIFICATIONS=1
export INTERACTIVE_SESSION=0
export BACKUP_DATASET="tank/zrb"
export GLOBAL_CONFIG_DIR="/etc/zrb"
export GLOBAL_EXCLUDE_RULE="$GLOBAL_CONFIG_DIR/exclude"
export GLOBAL_EXPIRE_RULE="$GLOBAL_CONFIG_DIR/expire"
export FREQ_LIST="daily"
export PARALLEL_JOBS="4"
export LOCK_FILE="/var/run/${SCRIPT_BASENAME}.lock"
export VAULTS_FILE="/tmp/vaults.txt"
export SCRIPT_BASENAME="${0##*/}"
f_check_switch_param() {
if ( ! echo "$1" | grep -q "^[A-Za-z0-9]" ); then
f_say "$C_RED Missing argument!"
exit 1
fi
}
f_date() {
local text="$1"
echo -n "$text"
date +"%Y-%m-%d %H:%M %Z"
}
_f_declare_f_say() {
if [[ $- == *i* ]]; then
export QUIET_NOTIFICATIONS=0
export INTERACTIVE_SESSION=1
fi
if [[ $INTERACTIVE_SESSION -eq 1 ]]
then
f_say() { echo -ne "$1"; echo -e "$C_NOCOLOR"; }
export C_GREEN="\e[1;32m"
export C_RED="\e[1;31m"
export C_BLUE="\e[1;34m"
export C_PURPLE="\e[1;35m"
export C_CYAN="\e[1;36m"
export C_NOCOLOR="\e[0m"
else
# do nothing
f_say() { echo -ne "$1"; true; }
fi
export -f f_say
}
f_process_args() {
while [ "$#" -gt "0" ]; do
case "$1" in
-f|--freq)
PARAM=$2
f_check_switch_param "$PARAM"
FREQ_LIST="$PARAM"
shift 2
;;
-j|--jobs)
PARAM=$2
f_check_switch_param "$PARAM"
PARALLEL_JOBS="$PARAM"
shift 2
;;
-h|--help)
f_usage
;;
esac
done
}
f_usage() {
echo "Usage:"
echo " $0 verbose output"
echo " $0 -f <freq> hourly,[daily],weekly,monthly (comma separated list)"
echo " $0 -j <num> number of parallel jobs"
echo
exit 1
}
f_list_vaults() {
if ( ! zfs list -H -s name -o name "$BACKUP_DATASET" > /dev/null ) ; then
f_say "$C_RED Unable to access the ZFS dataset: '$BACKUP_DATASET'"
exit 1
fi
local dataset
for dataset in $( zfs list -r -H -s name -o name "$BACKUP_DATASET" | grep -v "${BACKUP_DATASET}$" ); do
zfs list -H -s name -o name -r "$dataset" | grep -q "${dataset}/" || echo "$dataset";
done | sed "s@^${BACKUP_DATASET}/@@"
}
f_lock_create() {
local pid_now=$$
if pid_locked=$(cat "$LOCK_FILE" 2>/dev/null);
then
if ( ps --no-headers -o args -p "$pid_locked" | grep -q "${SCRIPT_BASENAME}" );
then
f_say "$C_RED $0 is already running!"
exit 1
else
f_say "$C_PURPLE Stale pidfile exists...removing."
f_lock_remove
fi
fi
echo "$pid_now" > "$LOCK_FILE"
}
f_lock_remove() {
rm -f "$LOCK_FILE"
}
f_read_global_config() {
if [ -f "$GLOBAL_CONFIG_DIR/BACKUP_DATASET" ];
then
BACKUP_DATASET=$(cat "$GLOBAL_CONFIG_DIR/BACKUP_DATASET")
fi
}
f_run_parallel_jobs() {
VAULTS_FILE=$1
f_date "BEGIN: "
# shellcheck disable=SC1083
parallel -j "$PARALLEL_JOBS" -a "$VAULTS_FILE" zrb.sh -e yes -f "$FREQ_LIST" -v {1}
f_date "FINISH: "
}
_f_declare_f_say
f_process_args "$@"
f_read_global_config
f_lock_create
f_list_vaults | tee "$VAULTS_FILE" > /dev/null
f_run_parallel_jobs "$VAULTS_FILE"
f_lock_remove