]> git.ipfire.org Git - ipfire-2.x.git/blame - config/backup/backup.pl
core145: Enable OpenVPN metrics collection
[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
5562f26f
AF
79 # remove wrong vnstat tag file
80 rm -f /var/log/vnstat/tag
81
34b7b986
AF
82 # create dhcpcd user
83 groupadd -g 52 dhcpcd
84 useradd -c 'dhcpcd privsep user' \
85 -d /run/dhcpcd/chroot \
86 -g dhcpcd \
87 -s /bin/false \
88 -u 52 dhcpcd
89
c7b7a70d
MT
90 # Run converters
91
92 # Outgoing Firewall
93 if [ -d "/var/ipfire/outgoing" ]; then
94 # Reset files
95 local file
96 for file in /var/ipfire/firewall/{config,outgoing} \
97 /var/ipfire/fwhosts/custom{hosts,groups,networks}; do
98 : > "${file}"
99 chown nobody:nobody "${file}"
100 done
101
102 # Run converter
103 convert-outgoingfw
104
105 # Remove old configuration
106 rm -rf "/var/ipfire/outgoing"
107 fi
108
109 # External Access
110 if [ -d "/var/ipfire/xtaccess" ]; then
111 : > /var/ipfire/firewall/config
112 chown nobody:nobody "/var/ipfire/firewall/config"
113
114 # Run converter
115 convert-xtaccess
116
117 # Remove old configuration
118 rm -rf "/var/ipfire/xtaccess"
119 fi
120
121 # DMZ Holes
122 if [ -d "/var/ipfire/dmzholes" ] || [ -d "/var/ipfire/portfw" ]; then
123 : > /var/ipfire/firewall/config
124 chown nobody:nobody "/var/ipfire/firewall/config"
125
126 # Run converter
127 convert-dmz
128
129 # Remove old configuration
130 rm -rf "/var/ipfire/dmzholes"
131 fi
132
133 # Port Forwardings
134 if [ -d "/var/ipfire/portfw" ]; then
135 # Run converter
136 convert-portfw
137
138 # Remove old configuration
139 rm -rf "/var/ipfire/portfw"
140 fi
141
142 # Reload firewall
143 firewallctrl
144
145 # Convert old OpenVPN CCD files (CN change, Core Update 75)
146 convert-ovpn
147
8c273724
SS
148 # Snort to suricata converter.
149 if [ -d "/var/ipfire/snort" ]; then
150 # Run converter
151 convert-snort
152
153 # Remove old configuration directory.
154 rm -rf "/var/ipfire/snort"
155 fi
156
ecbf6676
MT
157 # Convert DNS settings
158 convert-dns-settings
159
6df8a22b
AF
160 # move nobeeps if exist
161 [ -e "/var/ipfire/ppp/nobeeps" ] && mv /var/ipfire/ppp/nobeeps /var/ipfire/red/nobeeps
162
c7b7a70d 163 return 0
901a50cf 164}
c7b7a70d
MT
165
166find_logfiles() {
167 local filelist=( /var/log/messages* /var/log/*.log /var/log/**/*.log )
168
169 echo "${filelist[@]}"
b90a7e56 170}
c7b7a70d
MT
171
172make_addon_backup() {
173 local name="${1}"
174 shift
175
176 if [ ! -f "/var/ipfire/backup/addons/includes/${name}" ]; then
177 echo "${name} does not have any backup includes" >&2
178 return 1
179 fi
180
181 local filename="/var/ipfire/backup/addons/backup/${name}.ipf"
182
183 tar cvzf "${filename}" \
184 $(process_includes "/var/ipfire/backup/addons/includes/${name}")
8e8bbd9d 185}
c7b7a70d
MT
186
187restore_addon_backup() {
188 local name="${1}"
189
190 if [ -d "/tmp/${name}.ipf" ]; then
191 mv "/tmp/${name}.ipf" "/var/ipfire/backup/addons/backup/${name}.ipf"
192 fi
193
194 tar xvzpf "/var/ipfire/backup/addons/backup/${name}.ipf" -C /
a609bfb0 195}
8e8bbd9d 196
c7b7a70d
MT
197main() {
198 local command="${1}"
199 shift
200
c7b7a70d
MT
201 case "${command}" in
202 include)
175f5c06
MT
203 local filename="${1}"
204
205 if [ -z "${filename}" ]; then
206 filename="/var/ipfire/backup/${NOW}.ipf"
207 fi
208
c7b7a70d
MT
209 make_backup "${filename}" $(find_logfiles)
210 ;;
211
212 exclude)
175f5c06
MT
213 local filename="${1}"
214
215 if [ -z "${filename}" ]; then
216 filename="/var/ipfire/backup/${NOW}.ipf"
217 fi
218
c7b7a70d
MT
219 make_backup "${filename}"
220 ;;
221
222 restore)
175f5c06
MT
223 local filename="${1}"
224
225 if [ -z "${filename}" ]; then
226 filename="/tmp/restore.ipf"
227 fi
228
c7b7a70d
MT
229 restore_backup "/tmp/restore.ipf"
230 ;;
231
232 addonbackup)
233 make_addon_backup "$@"
234 ;;
cf29614f 235
c7b7a70d
MT
236 restoreaddon)
237 restore_addon_backup "${1/.ipf/}"
238 ;;
84578512 239
c7b7a70d 240 iso)
175f5c06
MT
241 # Desired backup filename
242 local filename="/var/ipfire/backup/${NOW}.ipf"
243
c7b7a70d
MT
244 if make_backup "${filename}"; then
245 /usr/local/bin/backupiso "${NOW}" &
246 fi
247 ;;
248
249 makedirs)
250 mkdir -p /var/ipfire/backup/addons/{backup,includes}
251 ;;
252
4f10c0b3
MT
253 list)
254 process_includes "/var/ipfire/backup/include" "/var/ipfire/backup/include.user"
255 ;;
256
c7b7a70d
MT
257 /var/ipfire/backup/*.ipf|/var/ipfire/backup/addons/backup/*.ipf|/var/tmp/backupiso/*.iso)
258 unlink "${command}"
259 ;;
260
261 *)
262 echo "${0}: [include|exclude|restore|addonbackup <addon>|restoreaddon <addon>|iso]" >&2
263 return 2
264 ;;
265 esac
266
267 return $?
cf29614f 268}
c7b7a70d
MT
269
270main "$@" || exit $?