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