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