]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - config/backup/backup.pl
Restart logging after restoring backup
[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 # Run converters
80
81 # Outgoing Firewall
82 if [ -d "/var/ipfire/outgoing" ]; then
83 # Reset files
84 local file
85 for file in /var/ipfire/firewall/{config,outgoing} \
86 /var/ipfire/fwhosts/custom{hosts,groups,networks}; do
87 : > "${file}"
88 chown nobody:nobody "${file}"
89 done
90
91 # Run converter
92 convert-outgoingfw
93
94 # Remove old configuration
95 rm -rf "/var/ipfire/outgoing"
96 fi
97
98 # External Access
99 if [ -d "/var/ipfire/xtaccess" ]; then
100 : > /var/ipfire/firewall/config
101 chown nobody:nobody "/var/ipfire/firewall/config"
102
103 # Run converter
104 convert-xtaccess
105
106 # Remove old configuration
107 rm -rf "/var/ipfire/xtaccess"
108 fi
109
110 # DMZ Holes
111 if [ -d "/var/ipfire/dmzholes" ] || [ -d "/var/ipfire/portfw" ]; then
112 : > /var/ipfire/firewall/config
113 chown nobody:nobody "/var/ipfire/firewall/config"
114
115 # Run converter
116 convert-dmz
117
118 # Remove old configuration
119 rm -rf "/var/ipfire/dmzholes"
120 fi
121
122 # Port Forwardings
123 if [ -d "/var/ipfire/portfw" ]; then
124 # Run converter
125 convert-portfw
126
127 # Remove old configuration
128 rm -rf "/var/ipfire/portfw"
129 fi
130
131 # Reload firewall
132 firewallctrl
133
134 # Convert old OpenVPN CCD files (CN change, Core Update 75)
135 convert-ovpn
136
137 # Snort to suricata converter.
138 if [ -d "/var/ipfire/snort" ]; then
139 # Run converter
140 convert-snort
141
142 # Remove old configuration directory.
143 rm -rf "/var/ipfire/snort"
144 fi
145
146 return 0
147 }
148
149 find_logfiles() {
150 local filelist=( /var/log/messages* /var/log/*.log /var/log/**/*.log )
151
152 echo "${filelist[@]}"
153 }
154
155 make_addon_backup() {
156 local name="${1}"
157 shift
158
159 if [ ! -f "/var/ipfire/backup/addons/includes/${name}" ]; then
160 echo "${name} does not have any backup includes" >&2
161 return 1
162 fi
163
164 local filename="/var/ipfire/backup/addons/backup/${name}.ipf"
165
166 tar cvzf "${filename}" \
167 $(process_includes "/var/ipfire/backup/addons/includes/${name}")
168 }
169
170 restore_addon_backup() {
171 local name="${1}"
172
173 if [ -d "/tmp/${name}.ipf" ]; then
174 mv "/tmp/${name}.ipf" "/var/ipfire/backup/addons/backup/${name}.ipf"
175 fi
176
177 tar xvzpf "/var/ipfire/backup/addons/backup/${name}.ipf" -C /
178 }
179
180 main() {
181 local command="${1}"
182 shift
183
184 case "${command}" in
185 include)
186 local filename="${1}"
187
188 if [ -z "${filename}" ]; then
189 filename="/var/ipfire/backup/${NOW}.ipf"
190 fi
191
192 make_backup "${filename}" $(find_logfiles)
193 ;;
194
195 exclude)
196 local filename="${1}"
197
198 if [ -z "${filename}" ]; then
199 filename="/var/ipfire/backup/${NOW}.ipf"
200 fi
201
202 make_backup "${filename}"
203 ;;
204
205 restore)
206 local filename="${1}"
207
208 if [ -z "${filename}" ]; then
209 filename="/tmp/restore.ipf"
210 fi
211
212 restore_backup "/tmp/restore.ipf"
213 ;;
214
215 addonbackup)
216 make_addon_backup "$@"
217 ;;
218
219 restoreaddon)
220 restore_addon_backup "${1/.ipf/}"
221 ;;
222
223 iso)
224 # Desired backup filename
225 local filename="/var/ipfire/backup/${NOW}.ipf"
226
227 if make_backup "${filename}"; then
228 /usr/local/bin/backupiso "${NOW}" &
229 fi
230 ;;
231
232 makedirs)
233 mkdir -p /var/ipfire/backup/addons/{backup,includes}
234 ;;
235
236 list)
237 process_includes "/var/ipfire/backup/include" "/var/ipfire/backup/include.user"
238 ;;
239
240 /var/ipfire/backup/*.ipf|/var/ipfire/backup/addons/backup/*.ipf|/var/tmp/backupiso/*.iso)
241 unlink "${command}"
242 ;;
243
244 *)
245 echo "${0}: [include|exclude|restore|addonbackup <addon>|restoreaddon <addon>|iso]" >&2
246 return 2
247 ;;
248 esac
249
250 return $?
251 }
252
253 main "$@" || exit $?