]>
Commit | Line | Data |
---|---|---|
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 |
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 | |
8e8bbd9d | 33 | } |
c7b7a70d MT |
34 | |
35 | process_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 | |
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 | |
cf29614f | 67 | } |
c7b7a70d MT |
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 | ||
8c273724 SS |
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 | ||
c7b7a70d | 141 | return 0 |
901a50cf | 142 | } |
c7b7a70d MT |
143 | |
144 | find_logfiles() { | |
145 | local filelist=( /var/log/messages* /var/log/*.log /var/log/**/*.log ) | |
146 | ||
147 | echo "${filelist[@]}" | |
b90a7e56 | 148 | } |
c7b7a70d MT |
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}") | |
8e8bbd9d | 163 | } |
c7b7a70d MT |
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 / | |
a609bfb0 | 173 | } |
8e8bbd9d | 174 | |
c7b7a70d MT |
175 | main() { |
176 | local command="${1}" | |
177 | shift | |
178 | ||
c7b7a70d MT |
179 | case "${command}" in |
180 | include) | |
175f5c06 MT |
181 | local filename="${1}" |
182 | ||
183 | if [ -z "${filename}" ]; then | |
184 | filename="/var/ipfire/backup/${NOW}.ipf" | |
185 | fi | |
186 | ||
c7b7a70d MT |
187 | make_backup "${filename}" $(find_logfiles) |
188 | ;; | |
189 | ||
190 | exclude) | |
175f5c06 MT |
191 | local filename="${1}" |
192 | ||
193 | if [ -z "${filename}" ]; then | |
194 | filename="/var/ipfire/backup/${NOW}.ipf" | |
195 | fi | |
196 | ||
c7b7a70d MT |
197 | make_backup "${filename}" |
198 | ;; | |
199 | ||
200 | restore) | |
175f5c06 MT |
201 | local filename="${1}" |
202 | ||
203 | if [ -z "${filename}" ]; then | |
204 | filename="/tmp/restore.ipf" | |
205 | fi | |
206 | ||
c7b7a70d MT |
207 | restore_backup "/tmp/restore.ipf" |
208 | ;; | |
209 | ||
210 | addonbackup) | |
211 | make_addon_backup "$@" | |
212 | ;; | |
cf29614f | 213 | |
c7b7a70d MT |
214 | restoreaddon) |
215 | restore_addon_backup "${1/.ipf/}" | |
216 | ;; | |
84578512 | 217 | |
c7b7a70d | 218 | iso) |
175f5c06 MT |
219 | # Desired backup filename |
220 | local filename="/var/ipfire/backup/${NOW}.ipf" | |
221 | ||
c7b7a70d MT |
222 | if make_backup "${filename}"; then |
223 | /usr/local/bin/backupiso "${NOW}" & | |
224 | fi | |
225 | ;; | |
226 | ||
227 | makedirs) | |
228 | mkdir -p /var/ipfire/backup/addons/{backup,includes} | |
229 | ;; | |
230 | ||
4f10c0b3 MT |
231 | list) |
232 | process_includes "/var/ipfire/backup/include" "/var/ipfire/backup/include.user" | |
233 | ;; | |
234 | ||
c7b7a70d MT |
235 | /var/ipfire/backup/*.ipf|/var/ipfire/backup/addons/backup/*.ipf|/var/tmp/backupiso/*.iso) |
236 | unlink "${command}" | |
237 | ;; | |
238 | ||
239 | *) | |
240 | echo "${0}: [include|exclude|restore|addonbackup <addon>|restoreaddon <addon>|iso]" >&2 | |
241 | return 2 | |
242 | ;; | |
243 | esac | |
244 | ||
245 | return $? | |
cf29614f | 246 | } |
c7b7a70d MT |
247 | |
248 | main "$@" || exit $? |