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