]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - config/backup/backup.pl
DNS: Add converter to migrate settings
[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 # Convert DNS settings
147 convert-dns-settings
148
149 return 0
150 }
151
152 find_logfiles() {
153 local filelist=( /var/log/messages* /var/log/*.log /var/log/**/*.log )
154
155 echo "${filelist[@]}"
156 }
157
158 make_addon_backup() {
159 local name="${1}"
160 shift
161
162 if [ ! -f "/var/ipfire/backup/addons/includes/${name}" ]; then
163 echo "${name} does not have any backup includes" >&2
164 return 1
165 fi
166
167 local filename="/var/ipfire/backup/addons/backup/${name}.ipf"
168
169 tar cvzf "${filename}" \
170 $(process_includes "/var/ipfire/backup/addons/includes/${name}")
171 }
172
173 restore_addon_backup() {
174 local name="${1}"
175
176 if [ -d "/tmp/${name}.ipf" ]; then
177 mv "/tmp/${name}.ipf" "/var/ipfire/backup/addons/backup/${name}.ipf"
178 fi
179
180 tar xvzpf "/var/ipfire/backup/addons/backup/${name}.ipf" -C /
181 }
182
183 main() {
184 local command="${1}"
185 shift
186
187 case "${command}" in
188 include)
189 local filename="${1}"
190
191 if [ -z "${filename}" ]; then
192 filename="/var/ipfire/backup/${NOW}.ipf"
193 fi
194
195 make_backup "${filename}" $(find_logfiles)
196 ;;
197
198 exclude)
199 local filename="${1}"
200
201 if [ -z "${filename}" ]; then
202 filename="/var/ipfire/backup/${NOW}.ipf"
203 fi
204
205 make_backup "${filename}"
206 ;;
207
208 restore)
209 local filename="${1}"
210
211 if [ -z "${filename}" ]; then
212 filename="/tmp/restore.ipf"
213 fi
214
215 restore_backup "/tmp/restore.ipf"
216 ;;
217
218 addonbackup)
219 make_addon_backup "$@"
220 ;;
221
222 restoreaddon)
223 restore_addon_backup "${1/.ipf/}"
224 ;;
225
226 iso)
227 # Desired backup filename
228 local filename="/var/ipfire/backup/${NOW}.ipf"
229
230 if make_backup "${filename}"; then
231 /usr/local/bin/backupiso "${NOW}" &
232 fi
233 ;;
234
235 makedirs)
236 mkdir -p /var/ipfire/backup/addons/{backup,includes}
237 ;;
238
239 list)
240 process_includes "/var/ipfire/backup/include" "/var/ipfire/backup/include.user"
241 ;;
242
243 /var/ipfire/backup/*.ipf|/var/ipfire/backup/addons/backup/*.ipf|/var/tmp/backupiso/*.iso)
244 unlink "${command}"
245 ;;
246
247 *)
248 echo "${0}: [include|exclude|restore|addonbackup <addon>|restoreaddon <addon>|iso]" >&2
249 return 2
250 ;;
251 esac
252
253 return $?
254 }
255
256 main "$@" || exit $?