]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - config/backup/backup.pl
Merge branch 'master-IDSv3' into temp-c164-development
[people/pmueller/ipfire-2.x.git] / config / backup / backup.pl
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007-2014 IPFire Team <info@ipfire.org> #
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 ###############################################################################
21
22 NOW="$(date "+%Y-%m-%d-%H:%M")"
23
24 list_addons() {
25 local file
26 for file in /var/ipfire/backup/addons/includes/*; do
27 if [ -f "${file}" ]; then
28 basename "${file}"
29 fi
30 done
31
32 return 0
33 }
34
35 process_includes() {
36 local include
37
38 for include in $@; do
39 local file
40 while read -r file; do
41 for file in ${file}; do
42 if [ -e "${file}" ]; then
43 echo "${file}"
44 fi
45 done
46 done < "${include}"
47 done | sort -u
48 }
49
50 make_backup() {
51 local filename="${1}"
52 shift
53
54 # Backup all addons first
55 local addon
56 for addon in $(list_addons); do
57 make_addon_backup "${addon}"
58 done
59
60 # Backup using global exclude/include definitions
61 tar cvfz "${filename}" \
62 --exclude-from="/var/ipfire/backup/exclude" \
63 --exclude-from="/var/ipfire/backup/exclude.user" \
64 $(process_includes "/var/ipfire/backup/include") \
65 $(process_includes "/var/ipfire/backup/include.user") \
66 "$@"
67
68 return 0
69 }
70
71 restore_backup() {
72 local filename="${1}"
73
74 tar xvzpf "${filename}" -C /
75
76 # Restart syslogd, httpd and suricata in case we've just loaded old logs
77 apachectl -k graceful
78 /bin/kill -HUP `cat /var/run/suricata.pid 2> /dev/null` 2> /dev/null
79 /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null
80
81 # remove wrong vnstat tag file
82 rm -f /var/log/vnstat/tag
83
84 # create dhcpcd user
85 groupadd -g 52 dhcpcd
86 useradd -c 'dhcpcd privsep user' \
87 -d /run/dhcpcd/chroot \
88 -g dhcpcd \
89 -s /bin/false \
90 -u 52 dhcpcd
91
92 # Run converters
93
94 # Outgoing Firewall
95 if [ -d "/var/ipfire/outgoing" ]; then
96 # Reset files
97 local file
98 for file in /var/ipfire/firewall/{config,outgoing} \
99 /var/ipfire/fwhosts/custom{hosts,groups,networks}; do
100 : > "${file}"
101 chown nobody:nobody "${file}"
102 done
103
104 # Run converter
105 convert-outgoingfw
106
107 # Remove old configuration
108 rm -rf "/var/ipfire/outgoing"
109 fi
110
111 # External Access
112 if [ -d "/var/ipfire/xtaccess" ]; then
113 : > /var/ipfire/firewall/config
114 chown nobody:nobody "/var/ipfire/firewall/config"
115
116 # Run converter
117 convert-xtaccess
118
119 # Remove old configuration
120 rm -rf "/var/ipfire/xtaccess"
121 fi
122
123 # DMZ Holes
124 if [ -d "/var/ipfire/dmzholes" ] || [ -d "/var/ipfire/portfw" ]; then
125 : > /var/ipfire/firewall/config
126 chown nobody:nobody "/var/ipfire/firewall/config"
127
128 # Run converter
129 convert-dmz
130
131 # Remove old configuration
132 rm -rf "/var/ipfire/dmzholes"
133 fi
134
135 # Port Forwardings
136 if [ -d "/var/ipfire/portfw" ]; then
137 # Run converter
138 convert-portfw
139
140 # Remove old configuration
141 rm -rf "/var/ipfire/portfw"
142 fi
143
144 # Convert location
145 convert-to-location
146
147 # Reload firewall
148 firewallctrl
149
150 # Convert old OpenVPN CCD files (CN change, Core Update 75)
151 convert-ovpn
152
153 # Snort to suricata converter.
154 if [ -d "/var/ipfire/snort" ]; then
155 # Run converter
156 convert-snort
157
158 # Remove old configuration directory.
159 rm -rf "/var/ipfire/snort"
160 fi
161
162 # IDS multiple providers converter.
163 if [ -e "/var/ipfire/suricata/rules-settings" ]; then
164 # Run the converter
165 convert-ids-multiple-providers
166 fi
167
168 # Convert DNS settings
169 convert-dns-settings
170
171 # move nobeeps if exist
172 [ -e "/var/ipfire/ppp/nobeeps" ] && mv /var/ipfire/ppp/nobeeps /var/ipfire/red/nobeeps
173
174 return 0
175 }
176
177 find_logfiles() {
178 local filelist=( /var/log/messages* /var/log/*.log /var/log/**/*.log )
179
180 echo "${filelist[@]}"
181 }
182
183 make_addon_backup() {
184 local name="${1}"
185 shift
186
187 if [ ! -f "/var/ipfire/backup/addons/includes/${name}" ]; then
188 echo "${name} does not have any backup includes" >&2
189 return 1
190 fi
191
192 local filename="/var/ipfire/backup/addons/backup/${name}.ipf"
193
194 tar cvzf "${filename}" \
195 $(process_includes "/var/ipfire/backup/addons/includes/${name}")
196 }
197
198 restore_addon_backup() {
199 local name="${1}"
200
201 if [ -d "/tmp/${name}.ipf" ]; then
202 mv "/tmp/${name}.ipf" "/var/ipfire/backup/addons/backup/${name}.ipf"
203 fi
204
205 tar xvzpf "/var/ipfire/backup/addons/backup/${name}.ipf" -C /
206 }
207
208 main() {
209 local command="${1}"
210 shift
211
212 case "${command}" in
213 include)
214 local filename="${1}"
215
216 if [ -z "${filename}" ]; then
217 filename="/var/ipfire/backup/${NOW}.ipf"
218 fi
219
220 make_backup "${filename}" $(find_logfiles)
221 ;;
222
223 exclude)
224 local filename="${1}"
225
226 if [ -z "${filename}" ]; then
227 filename="/var/ipfire/backup/${NOW}.ipf"
228 fi
229
230 make_backup "${filename}"
231 ;;
232
233 restore)
234 local filename="${1}"
235
236 if [ -z "${filename}" ]; then
237 filename="/tmp/restore.ipf"
238 fi
239
240 restore_backup "${filename}"
241 ;;
242
243 addonbackup)
244 make_addon_backup "$@"
245 ;;
246
247 restoreaddon)
248 restore_addon_backup "${1/.ipf/}"
249 ;;
250
251 iso)
252 # Desired backup filename
253 local filename="/var/ipfire/backup/${NOW}.ipf"
254
255 if make_backup "${filename}"; then
256 /usr/local/bin/backupiso "${NOW}"
257 fi
258 ;;
259
260 makedirs)
261 mkdir -p /var/ipfire/backup/addons/{backup,includes}
262 ;;
263
264 list)
265 process_includes "/var/ipfire/backup/include" "/var/ipfire/backup/include.user"
266 ;;
267
268 /var/ipfire/backup/*.ipf|/var/ipfire/backup/addons/backup/*.ipf|/var/tmp/backupiso/*.iso)
269 unlink "${command}"
270 ;;
271
272 *)
273 echo "${0}: [include|exclude|restore|addonbackup <addon>|restoreaddon <addon>|iso]" >&2
274 return 2
275 ;;
276 esac
277
278 return $?
279 }
280
281 main "$@" || exit $?