backup: Rewrite backup script
[people/pmueller/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                         while read file; do
42                                 if [ -e "${file}" ]; then
43                                         echo "${file}"
44                                 fi
45                         done <<< ${file}
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         return 0
133 }
134
135 find_logfiles() {
136         local filelist=( /var/log/messages* /var/log/*.log /var/log/**/*.log )
137
138         echo "${filelist[@]}"
139 }
140
141 make_addon_backup() {
142         local name="${1}"
143         shift
144
145         if [ ! -f "/var/ipfire/backup/addons/includes/${name}" ]; then
146                 echo "${name} does not have any backup includes" >&2
147                 return 1
148         fi
149
150         local filename="/var/ipfire/backup/addons/backup/${name}.ipf"
151
152         tar cvzf "${filename}" \
153                 $(process_includes "/var/ipfire/backup/addons/includes/${name}")
154 }
155
156 restore_addon_backup() {
157         local name="${1}"
158
159         if [ -d "/tmp/${name}.ipf" ]; then
160                 mv "/tmp/${name}.ipf" "/var/ipfire/backup/addons/backup/${name}.ipf"
161         fi
162
163         tar xvzpf "/var/ipfire/backup/addons/backup/${name}.ipf" -C /
164 }
165
166 main() {
167         local command="${1}"
168         shift
169
170         # Desired backup filename
171         local filename="/var/ipfire/backup/${NOW}.ipf"
172
173         case "${command}" in
174                 include)
175                         make_backup "${filename}" $(find_logfiles)
176                         ;;
177
178                 exclude)
179                         make_backup "${filename}"
180                         ;;
181
182                 restore)
183                         restore_backup "/tmp/restore.ipf"
184                         ;;
185
186                 addonbackup)
187                         make_addon_backup "$@"
188                         ;;
189
190                 restoreaddon)
191                         restore_addon_backup "${1/.ipf/}"
192                         ;;
193
194                 iso)
195                         if make_backup "${filename}"; then
196                                 /usr/local/bin/backupiso "${NOW}" &
197                         fi
198                         ;;
199
200                 makedirs)
201                         mkdir -p /var/ipfire/backup/addons/{backup,includes}
202                         ;;
203
204                 /var/ipfire/backup/*.ipf|/var/ipfire/backup/addons/backup/*.ipf|/var/tmp/backupiso/*.iso)
205                         unlink "${command}"
206                         ;;
207
208                 *)
209                         echo "${0}: [include|exclude|restore|addonbackup <addon>|restoreaddon <addon>|iso]" >&2
210                         return 2
211                         ;;
212         esac
213
214         return $?
215 }
216
217 main "$@" || exit $?