]>
Commit | Line | Data |
---|---|---|
c7b7a70d | 1 | #!/bin/bash |
70df8302 MT |
2 | ############################################################################### |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
c1a5a49b | 5 | # Copyright (C) 2007-2022 IPFire Team <info@ipfire.org> # |
70df8302 MT |
6 | # # |
7 | # This program is free software: you can redistribute it and/or modify # | |
8 | # it under the terms of the GNU General Public License as published by # | |
9 | # the Free Software Foundation, either version 3 of the License, or # | |
10 | # (at your option) any later version. # | |
11 | # # | |
12 | # This program is distributed in the hope that it will be useful, # | |
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # | |
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | |
15 | # GNU General Public License for more details. # | |
16 | # # | |
17 | # You should have received a copy of the GNU General Public License # | |
18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. # | |
19 | # # | |
20 | ############################################################################### | |
cf29614f | 21 | |
13f6473a MT |
22 | shopt -s nullglob |
23 | ||
c7b7a70d MT |
24 | NOW="$(date "+%Y-%m-%d-%H:%M")" |
25 | ||
26 | list_addons() { | |
27 | local file | |
28 | for file in /var/ipfire/backup/addons/includes/*; do | |
29 | if [ -f "${file}" ]; then | |
30 | basename "${file}" | |
31 | fi | |
32 | done | |
33 | ||
34 | return 0 | |
8e8bbd9d | 35 | } |
c7b7a70d MT |
36 | |
37 | process_includes() { | |
38 | local include | |
c7b7a70d MT |
39 | for include in $@; do |
40 | local file | |
41 | while read -r file; do | |
d5d3748b MT |
42 | # Skip any empty line (which will include /) |
43 | [ -n "${file}" ] || continue | |
44 | ||
13f6473a | 45 | for file in /${file}; do |
b275771f MT |
46 | if [ -e "${file}" ]; then |
47 | echo "${file}" | |
48 | fi | |
4f10c0b3 | 49 | done |
c7b7a70d MT |
50 | done < "${include}" |
51 | done | sort -u | |
5ad5a6bc | 52 | } |
c7b7a70d MT |
53 | |
54 | make_backup() { | |
55 | local filename="${1}" | |
56 | shift | |
57 | ||
58 | # Backup all addons first | |
59 | local addon | |
60 | for addon in $(list_addons); do | |
61 | make_addon_backup "${addon}" | |
62 | done | |
63 | ||
e5f3e039 | 64 | # Backup using global exclude/include definitions |
c7e0d73e | 65 | tar cvfz "${filename}" -C / \ |
c7b7a70d MT |
66 | --exclude-from="/var/ipfire/backup/exclude" \ |
67 | --exclude-from="/var/ipfire/backup/exclude.user" \ | |
51ed815f | 68 | $(process_includes "/var/ipfire/backup/include") \ |
e5f3e039 | 69 | $(process_includes "/var/ipfire/backup/include.user") \ |
c7b7a70d MT |
70 | "$@" |
71 | ||
72 | return 0 | |
cf29614f | 73 | } |
c7b7a70d MT |
74 | |
75 | restore_backup() { | |
76 | local filename="${1}" | |
77 | ||
fc717041 | 78 | # Extract backup |
3f8e70f6 MT |
79 | if ! tar xvzpf "${filename}" -C / \ |
80 | --exclude-from="/var/ipfire/backup/exclude" \ | |
81 | --exclude-from="/var/ipfire/backup/exclude.user"; then | |
fc717041 MT |
82 | echo "Could not extract backup" >&2 |
83 | return 1 | |
84 | fi | |
c7b7a70d | 85 | |
28797d48 TF |
86 | # Restart syslogd, httpd and suricata in case we've just loaded old logs |
87 | apachectl -k graceful | |
88 | /bin/kill -HUP `cat /var/run/suricata.pid 2> /dev/null` 2> /dev/null | |
89 | /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null | |
90 | ||
5562f26f AF |
91 | # remove wrong vnstat tag file |
92 | rm -f /var/log/vnstat/tag | |
93 | ||
34b7b986 AF |
94 | # create dhcpcd user |
95 | groupadd -g 52 dhcpcd | |
96 | useradd -c 'dhcpcd privsep user' \ | |
97 | -d /run/dhcpcd/chroot \ | |
98 | -g dhcpcd \ | |
99 | -s /bin/false \ | |
100 | -u 52 dhcpcd | |
101 | ||
c7b7a70d MT |
102 | # Run converters |
103 | ||
104 | # Outgoing Firewall | |
105 | if [ -d "/var/ipfire/outgoing" ]; then | |
106 | # Reset files | |
107 | local file | |
108 | for file in /var/ipfire/firewall/{config,outgoing} \ | |
109 | /var/ipfire/fwhosts/custom{hosts,groups,networks}; do | |
110 | : > "${file}" | |
111 | chown nobody:nobody "${file}" | |
112 | done | |
113 | ||
114 | # Run converter | |
115 | convert-outgoingfw | |
116 | ||
117 | # Remove old configuration | |
118 | rm -rf "/var/ipfire/outgoing" | |
119 | fi | |
120 | ||
121 | # External Access | |
122 | if [ -d "/var/ipfire/xtaccess" ]; then | |
123 | : > /var/ipfire/firewall/config | |
124 | chown nobody:nobody "/var/ipfire/firewall/config" | |
125 | ||
126 | # Run converter | |
127 | convert-xtaccess | |
128 | ||
129 | # Remove old configuration | |
130 | rm -rf "/var/ipfire/xtaccess" | |
131 | fi | |
132 | ||
133 | # DMZ Holes | |
134 | if [ -d "/var/ipfire/dmzholes" ] || [ -d "/var/ipfire/portfw" ]; then | |
135 | : > /var/ipfire/firewall/config | |
136 | chown nobody:nobody "/var/ipfire/firewall/config" | |
137 | ||
138 | # Run converter | |
139 | convert-dmz | |
140 | ||
141 | # Remove old configuration | |
142 | rm -rf "/var/ipfire/dmzholes" | |
143 | fi | |
144 | ||
145 | # Port Forwardings | |
146 | if [ -d "/var/ipfire/portfw" ]; then | |
147 | # Run converter | |
148 | convert-portfw | |
149 | ||
150 | # Remove old configuration | |
151 | rm -rf "/var/ipfire/portfw" | |
152 | fi | |
153 | ||
f1d982cc SS |
154 | # Convert location |
155 | convert-to-location | |
156 | ||
c7b7a70d MT |
157 | # Reload firewall |
158 | firewallctrl | |
159 | ||
8c273724 SS |
160 | # Snort to suricata converter. |
161 | if [ -d "/var/ipfire/snort" ]; then | |
162 | # Run converter | |
163 | convert-snort | |
164 | ||
165 | # Remove old configuration directory. | |
166 | rm -rf "/var/ipfire/snort" | |
167 | fi | |
168 | ||
4aa1382e SS |
169 | # IDS multiple providers converter. |
170 | if [ -e "/var/ipfire/suricata/rules-settings" ]; then | |
171 | # Run the converter | |
172 | convert-ids-multiple-providers | |
173 | fi | |
174 | ||
b3dbe9ef SS |
175 | # IDS backend converter. |
176 | if [ -e "/var/ipfire/suricata/oinkmaster.conf" ]; then | |
177 | # Run the converter | |
178 | convert-ids-backend-files | |
179 | fi | |
180 | ||
ecbf6676 MT |
181 | # Convert DNS settings |
182 | convert-dns-settings | |
183 | ||
6df8a22b AF |
184 | # move nobeeps if exist |
185 | [ -e "/var/ipfire/ppp/nobeeps" ] && mv /var/ipfire/ppp/nobeeps /var/ipfire/red/nobeeps | |
186 | ||
233baacd MT |
187 | # Update OpenVPN CRL |
188 | /etc/fcron.daily/openvpn-crl-updater | |
189 | ||
c7b7a70d | 190 | return 0 |
901a50cf | 191 | } |
c7b7a70d MT |
192 | |
193 | find_logfiles() { | |
c1a5a49b | 194 | local filelist=( /var/log/logwatch/* /var/log/messages* /var/log/*.log /var/log/**/*.log ) |
c7b7a70d MT |
195 | |
196 | echo "${filelist[@]}" | |
b90a7e56 | 197 | } |
c7b7a70d MT |
198 | |
199 | make_addon_backup() { | |
200 | local name="${1}" | |
201 | shift | |
202 | ||
203 | if [ ! -f "/var/ipfire/backup/addons/includes/${name}" ]; then | |
204 | echo "${name} does not have any backup includes" >&2 | |
205 | return 1 | |
206 | fi | |
207 | ||
208 | local filename="/var/ipfire/backup/addons/backup/${name}.ipf" | |
209 | ||
210 | tar cvzf "${filename}" \ | |
211 | $(process_includes "/var/ipfire/backup/addons/includes/${name}") | |
8e8bbd9d | 212 | } |
c7b7a70d MT |
213 | |
214 | restore_addon_backup() { | |
215 | local name="${1}" | |
216 | ||
217 | if [ -d "/tmp/${name}.ipf" ]; then | |
218 | mv "/tmp/${name}.ipf" "/var/ipfire/backup/addons/backup/${name}.ipf" | |
219 | fi | |
220 | ||
fc717041 MT |
221 | # Extract backup |
222 | if ! tar xvzpf "/var/ipfire/backup/addons/backup/${name}.ipf" -C /; then | |
223 | echo "Could not extract backup" >&2 | |
224 | return 1 | |
225 | fi | |
a609bfb0 | 226 | } |
8e8bbd9d | 227 | |
c7b7a70d MT |
228 | main() { |
229 | local command="${1}" | |
230 | shift | |
231 | ||
c7b7a70d MT |
232 | case "${command}" in |
233 | include) | |
175f5c06 MT |
234 | local filename="${1}" |
235 | ||
236 | if [ -z "${filename}" ]; then | |
51ed815f | 237 | filename="/var/ipfire/backup/${NOW}.ipf" |
175f5c06 MT |
238 | fi |
239 | ||
c7b7a70d MT |
240 | make_backup "${filename}" $(find_logfiles) |
241 | ;; | |
242 | ||
243 | exclude) | |
175f5c06 MT |
244 | local filename="${1}" |
245 | ||
246 | if [ -z "${filename}" ]; then | |
51ed815f | 247 | filename="/var/ipfire/backup/${NOW}.ipf" |
175f5c06 MT |
248 | fi |
249 | ||
c7b7a70d MT |
250 | make_backup "${filename}" |
251 | ;; | |
252 | ||
253 | restore) | |
175f5c06 MT |
254 | local filename="${1}" |
255 | ||
256 | if [ -z "${filename}" ]; then | |
257 | filename="/tmp/restore.ipf" | |
258 | fi | |
259 | ||
d9db9160 | 260 | restore_backup "${filename}" |
c7b7a70d MT |
261 | ;; |
262 | ||
263 | addonbackup) | |
264 | make_addon_backup "$@" | |
265 | ;; | |
cf29614f | 266 | |
c7b7a70d MT |
267 | restoreaddon) |
268 | restore_addon_backup "${1/.ipf/}" | |
269 | ;; | |
84578512 | 270 | |
c7b7a70d | 271 | iso) |
175f5c06 MT |
272 | # Desired backup filename |
273 | local filename="/var/ipfire/backup/${NOW}.ipf" | |
274 | ||
c7b7a70d | 275 | if make_backup "${filename}"; then |
45a5df5a | 276 | /usr/local/bin/backupiso "${NOW}" |
c7b7a70d MT |
277 | fi |
278 | ;; | |
279 | ||
280 | makedirs) | |
281 | mkdir -p /var/ipfire/backup/addons/{backup,includes} | |
282 | ;; | |
283 | ||
4f10c0b3 MT |
284 | list) |
285 | process_includes "/var/ipfire/backup/include" "/var/ipfire/backup/include.user" | |
286 | ;; | |
287 | ||
c7b7a70d MT |
288 | /var/ipfire/backup/*.ipf|/var/ipfire/backup/addons/backup/*.ipf|/var/tmp/backupiso/*.iso) |
289 | unlink "${command}" | |
290 | ;; | |
291 | ||
292 | *) | |
293 | echo "${0}: [include|exclude|restore|addonbackup <addon>|restoreaddon <addon>|iso]" >&2 | |
294 | return 2 | |
295 | ;; | |
296 | esac | |
297 | ||
298 | return $? | |
cf29614f | 299 | } |
c7b7a70d MT |
300 | |
301 | main "$@" || exit $? |