]> git.ipfire.org Git - ipfire-2.x.git/blame - config/backup/backup.pl
Merge branch 'next' of git.ipfire.org:/pub/git/ipfire-2.x into next
[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
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
144find_logfiles() {
145 local filelist=( /var/log/messages* /var/log/*.log /var/log/**/*.log )
146
147 echo "${filelist[@]}"
b90a7e56 148}
c7b7a70d
MT
149
150make_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
165restore_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
175main() {
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 ;;
cf29614f 198
c7b7a70d
MT
199 restoreaddon)
200 restore_addon_backup "${1/.ipf/}"
201 ;;
84578512 202
c7b7a70d
MT
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
4f10c0b3
MT
213 list)
214 process_includes "/var/ipfire/backup/include" "/var/ipfire/backup/include.user"
215 ;;
216
c7b7a70d
MT
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 $?
cf29614f 228}
c7b7a70d
MT
229
230main "$@" || exit $?