-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgeoip-shell-backup.sh
244 lines (201 loc) · 6.36 KB
/
geoip-shell-backup.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/bin/sh
# shellcheck disable=SC2015,SC2034,SC2154,SC2086,SC1090,SC2120
# geoip-shell-backup.sh
# Copyright: antonk ([email protected])
# github.com/friendly-bits
#### Initial setup
p_name="geoip-shell"
. "/usr/bin/${p_name}-geoinit.sh" || exit 1
san_args "$@"
newifs "$delim"
set -- $_args
oldifs
#### USAGE
usage() {
cat <<EOF
Usage: $me <action> [-n] [-s] [-d] [-V] [-h]
Creates a backup of the current firewall state and current ipsets or restores them from backup.
Actions:
create-backup|restore : create a backup of, or restore $p_name config, ipsets and firewall rules
Options:
-n : Do not restore config and status files
-s : Only restore or create backup of config and status files
-d : Debug
-V : Version
-h : This help
EOF
}
#### PARSE ARGUMENTS
# check for valid action
action="$1"
case "$action" in
create-backup|restore) shift ;;
* ) unknownact
esac
# process the rest of the args
restore_conf=1 only_conf_status=
while getopts ":nsdVh" opt; do
case $opt in
n) restore_conf= ;;
s) only_conf_status=1 ;;
d) debugmode_arg=1 ;;
V) echo "$curr_ver"; exit 0 ;;
h) usage; exit 0 ;;
*) unknownopt
esac
done
shift $((OPTIND-1))
extra_args "$@"
is_root_ok
setdebug
debugentermsg
bk_failed() {
[ "$1" ] && echolog -err "$1"
rm_bk_tmp
die "$FAIL create backup of $p_name ipsets."
}
rm_bk_tmp() {
set +f
rm -rf "$bk_dir_new"
rm -f "$bk_failed_file" "$tmp_file" "$iplist_dir/"*.iplist
}
rstr_failed() {
rm_rstr_tmp
export main_config=
[ "$1" ] && echolog -err "$1"
[ "$2" = reset ] && {
echolog -err "*** Geoblocking is not working. Removing geoblocking firewall rules. ***"
rm_all_georules
}
die
}
rm_rstr_tmp() {
set +f
rm -f "$tmp_file" "$iplist_dir/"*.iplist
}
# detects archive type (if any) of file passed in 1st argument by its extension
# and sets the $extract_cmd variable accordingly
set_extract_cmd() {
set_extr_cmd() { checkutil "$1" && extract_cmd="$1 -cd" ||
die "backup archive type is '$1' but the $1 utility is not found."; }
case "$1" in
bz2 ) set_extr_cmd bzip2 ;;
xz ) set_extr_cmd xz ;;
gz ) set_extr_cmd gunzip ;;
* ) extract_cmd="cat" ;;
esac
}
# detects the best available archive type and sets $compr_cmd and $bk_ext accordingly
set_archive_type() {
arch_bzip2="bzip2 -zc@bz2"
arch_xz="xz -zc@xz"
arch_gzip="gzip -c@gz"
arch_cat="cat@"
for _util in bzip2 xz gzip cat; do
checkutil "$_util" && {
eval "compr_cmd=\"\${arch_$_util%@*}\"; bk_ext=\"\${arch_$_util#*@}\""
break
}
done
}
# checks for diff in new and old config and status files and makes a backup or restores if necessary
# 1 - restore|backup
cp_conf() {
unset src_f src_d dest_f dest_d
case "$1" in
restore) src_f=_bak; src_d="$bk_dir/"; cp_act=Restoring ;;
backup) dest_f=_bak; dest_d="$bk_dir_new/"; cp_act="Creating backup of" ;;
*) echolog -err "cp_conf: bad argument '$1'"; return 1
esac
for bak_f in status config; do
eval "cp_src=\"$src_d\$${bak_f}_file$src_f\" cp_dest=\"$dest_d\$${bak_f}_file$dest_f\""
[ "$cp_src" ] && [ "$cp_dest" ] || { echolog -err "cp_conf: $FAIL set \$cp_src or \$cp_dest"; return 1; }
[ -f "$cp_src" ] || continue
[ -f "$cp_dest" ] && compare_files "$cp_src" "$cp_dest" && {
debugprint "$cp_src is identical to $cp_dest"
continue
}
debugprint "Copying '$cp_src' to '$cp_dest'"
printf %s "$cp_act the $bak_f file... "
cp "$cp_src" "$cp_dest" || { echolog -err "$cp_act the $bak_f file failed."; return 1; }
OK
done
}
#### VARIABLES
getconfig families
[ ! "$inbound_iplists" ] && getconfig inbound_iplists
[ ! "$outbound_iplists" ] && getconfig outbound_iplists
bk_dir="$datadir/backup"
bk_dir_new="${bk_dir}.new"
config_file="$conf_file"
config_file_bak="${p_name}.conf.bak"
status_file="$datadir/status"
status_file_bak="status.bak"
bk_failed_file="/tmp/$p_name-backup-failed"
checkvars _fw_backend datadir
. "$_lib-$_fw_backend.sh" || die
#### MAIN
mk_lock
set +f
case "$action" in
create-backup)
trap 'trap - INT TERM HUP QUIT; rm_bk_tmp; die' INT TERM HUP QUIT
tmp_file="/tmp/${p_name}_backup.tmp"
[ "$only_conf_status" ] && {
bk_dir_new="$bk_dir"
mkdir -p "$bk_dir" && chmod -R 600 "$bk_dir" && chown -R root:root "$bk_dir" &&
cp_conf backup || die "$FAIL create backup of the config and status files."
die 0
}
set_archive_type
mkdir -p "$bk_dir_new" && chmod -R 600 "$bk_dir_new" && chown -R root:root "$bk_dir_new"
san_str iplists "$inbound_iplists $outbound_iplists" || die
printf_s "Creating backup of $p_name IP sets... "
rm -f "$bk_failed_file"
create_backup && OK
rm -f "$tmp_file"
setconfig "bk_ext=${bk_ext:-bak}" &&
cp_conf backup || bk_failed
rm -rf "$bk_dir"
mv "$bk_dir_new" "$bk_dir" || bk_failed
chmod -R 600 "$bk_dir" && chown -R root:root "$bk_dir" ||
echolog -err "$FAIL set permissions for the backup directory '$bk_dir'."
printf '%s\n\n' "Successfully created backup of $p_name state." ;;
restore)
trap 'trap - INT TERM HUP QUIT; rm_rstr_tmp; die' INT TERM HUP QUIT
if [ "$restore_conf" ]; then
bk_conf_file="$bk_dir/$config_file_bak"
and_config=" and config"
else
bk_conf_file="$config_file"
and_config=
fi
[ "$only_conf_status" ] && {
cp_conf restore || die "$FAIL restore the config and status files."
die 0
}
echolog "${_nl}Preparing to restore $p_name IP lists$and_config from backup..."
[ ! -s "$bk_conf_file" ] && rstr_failed "Config file '$bk_conf_file' is empty or doesn't exist."
getconfig inbound_iplists inbound_iplists "$bk_conf_file" &&
getconfig outbound_iplists outbound_iplists "$bk_conf_file" &&
getconfig bk_ext bk_ext "$bk_conf_file" || rstr_failed "$FAIL get backup config."
san_str iplists "$inbound_iplists $outbound_iplists" || die
if [ "$iplists" ]; then
get_counters
set_extract_cmd "$bk_ext"
extract_iplists
else
echolog "No IP lists registered - skipping iplist extraction."
fi
### Remove geoblocking iptables rules and ipsets
rm_all_georules || rstr_failed "$FAIL remove firewall rules and ipsets."
[ "$restore_conf" ] && { cp_conf restore || rstr_failed; }
export main_config=
[ "$_fw_backend" = ipt ] && { restore_ipsets || rstr_failed "$FAIL restore $p_name ipsets from backup." reset; }
call_script "$i_script-apply.sh" restore
apply_rv=$?
rm_rstr_tmp
[ "$apply_rv" != 0 ] && rstr_failed "$FAIL restore $p_name IP lists$and_config from backup." "reset"
[ "$restore_conf" ] && echolog "Successfully completed action 'restore'."
esac
die 0