]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - config/backup/backup.pl
DNS: Add converter to migrate settings
[people/pmueller/ipfire-2.x.git] / config / backup / backup.pl
CommitLineData
c7b7a70d 1#!/bin/bash
70df8302
MT
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
2b4593b2 5# Copyright (C) 2007-2014 IPFire Team <info@ipfire.org> #
70df8302
MT
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###############################################################################
cf29614f 21
c7b7a70d
MT
22NOW="$(date "+%Y-%m-%d-%H:%M")"
23
24list_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
8e8bbd9d 33}
c7b7a70d
MT
34
35process_includes() {
36 local include
37
38 for include in $@; do
39 local file
40 while read -r file; do
4f10c0b3 41 for file in ${file}; do
c7b7a70d
MT
42 if [ -e "${file}" ]; then
43 echo "${file}"
44 fi
4f10c0b3 45 done
c7b7a70d
MT
46 done < "${include}"
47 done | sort -u
5ad5a6bc 48}
c7b7a70d
MT
49
50make_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
cf29614f 67}
c7b7a70d
MT
68
69restore_backup() {
70 local filename="${1}"
71
72 tar xvzpf "${filename}" -C /
73
28797d48
TF
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
c7b7a70d
MT
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
8c273724
SS
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
ecbf6676
MT
146 # Convert DNS settings
147 convert-dns-settings
148
c7b7a70d 149 return 0
901a50cf 150}
c7b7a70d
MT
151
152find_logfiles() {
153 local filelist=( /var/log/messages* /var/log/*.log /var/log/**/*.log )
154
155 echo "${filelist[@]}"
b90a7e56 156}
c7b7a70d
MT
157
158make_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}")
8e8bbd9d 171}
c7b7a70d
MT
172
173restore_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 /
a609bfb0 181}
8e8bbd9d 182
c7b7a70d
MT
183main() {
184 local command="${1}"
185 shift
186
c7b7a70d
MT
187 case "${command}" in
188 include)
175f5c06
MT
189 local filename="${1}"
190
191 if [ -z "${filename}" ]; then
192 filename="/var/ipfire/backup/${NOW}.ipf"
193 fi
194
c7b7a70d
MT
195 make_backup "${filename}" $(find_logfiles)
196 ;;
197
198 exclude)
175f5c06
MT
199 local filename="${1}"
200
201 if [ -z "${filename}" ]; then
202 filename="/var/ipfire/backup/${NOW}.ipf"
203 fi
204
c7b7a70d
MT
205 make_backup "${filename}"
206 ;;
207
208 restore)
175f5c06
MT
209 local filename="${1}"
210
211 if [ -z "${filename}" ]; then
212 filename="/tmp/restore.ipf"
213 fi
214
c7b7a70d
MT
215 restore_backup "/tmp/restore.ipf"
216 ;;
217
218 addonbackup)
219 make_addon_backup "$@"
220 ;;
cf29614f 221
c7b7a70d
MT
222 restoreaddon)
223 restore_addon_backup "${1/.ipf/}"
224 ;;
84578512 225
c7b7a70d 226 iso)
175f5c06
MT
227 # Desired backup filename
228 local filename="/var/ipfire/backup/${NOW}.ipf"
229
c7b7a70d
MT
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
4f10c0b3
MT
239 list)
240 process_includes "/var/ipfire/backup/include" "/var/ipfire/backup/include.user"
241 ;;
242
c7b7a70d
MT
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 $?
cf29614f 254}
c7b7a70d
MT
255
256main "$@" || exit $?