2 |
soulskater |
1 |
#!/bin/bash
|
|
|
2 |
|
|
|
3 |
#
|
|
|
4 |
# This script will do various backups of Zimbra depending on which you choose,
|
|
|
5 |
# most of them are cold backups except the msg backup which hot copies the 'store'
|
|
|
6 |
# folder for possible individual mail retrieval. Be sure to change the variables below
|
|
|
7 |
# to point to where you got the Zimbra folder and where you want it to be backed up to.
|
|
|
8 |
#
|
|
|
9 |
# This script must be run as root or a user with equal privileges or it will not work.
|
|
|
10 |
#
|
|
|
11 |
# When you run this script via crontab be sure to add '> /dev/null 2>&1' at the end
|
|
|
12 |
# of the script like below or the tar command will fail for no apparent reason.
|
|
|
13 |
# 00 12 * * * oZimbraBackup.sh --full > /dev/null 2>&1
|
|
|
14 |
#
|
|
|
15 |
# As of 2008-04-16 this script uses some extra software to extend this script,
|
|
|
16 |
# the scripts standard function will still function without these extra software's but if you
|
|
|
17 |
# intend to use this scripts built in file transfer functions you must install the described software below.
|
|
|
18 |
#
|
|
|
19 |
# Required software: rsync for normal operation and ftp or scp & expect for file transfer capabillities.
|
|
|
20 |
#
|
|
|
21 |
# Bits and pieces was adopted from a script created by Daniel W. Martin, 9 Sept 2007
|
|
|
22 |
# Licensed under BSDL license, see license.txt for information.
|
|
|
23 |
#
|
|
|
24 |
# Developer: oSource Development(as of 2009-07-01)
|
|
|
25 |
# DevTeam: Marcus Uddenhed
|
4 |
soulskater |
26 |
# Version: 1.0.7
|
5 |
soulskater |
27 |
# Updated: 2016-04-01 15:30
|
2 |
soulskater |
28 |
#
|
|
|
29 |
|
|
|
30 |
#### Global Settings ####
|
5 |
soulskater |
31 |
ZimInstPath=/opt # Installation path for Zimbra, excluding the Zimbra folder.
|
|
|
32 |
ZimHome=zimbra # The Zimbra installation folder, excluding path to folder.
|
2 |
soulskater |
33 |
ZimBackupPath=/opt/backup # Root folder for backup where backup files will be placed.
|
|
|
34 |
|
|
|
35 |
#### Log Settings ####
|
5 |
soulskater |
36 |
ZimLogEnable=yes # Turns logging on or off(yes/no).
|
|
|
37 |
ZimLogLogRotate=no # Enables log rotating(yes/no)
|
|
|
38 |
ZimLogRotateInt=day # How often should we rotate logs(day, week or month)
|
|
|
39 |
ZimLogPath=/opt/backup/logs # Folder for log files
|
|
|
40 |
ZimLogVerbose=no # Activates extra logging information(yes/no)
|
2 |
soulskater |
41 |
|
|
|
42 |
#### File Transfer Settings ####
|
|
|
43 |
|
|
|
44 |
# Enable Services (yes/no)
|
5 |
soulskater |
45 |
ZimFtpEnable=no # Enable/Disable ftp file transfer(yes/no)
|
|
|
46 |
ZimScpEnable=no # Enable/Disable scp file transfer(yes/no)
|
2 |
soulskater |
47 |
|
|
|
48 |
# Extra FTP Settings
|
5 |
soulskater |
49 |
ZimFtpOpt='' # Extra options for ftp file transfer, see manual for ftp command
|
2 |
soulskater |
50 |
|
|
|
51 |
# Extra SCP Settings
|
5 |
soulskater |
52 |
ZimScpOpt='' # Extra options for scp file transfer, see manual for scp command
|
2 |
soulskater |
53 |
|
|
|
54 |
# Common Settings
|
5 |
soulskater |
55 |
ZimFilehostUser= # Username for file transfers
|
|
|
56 |
ZimFilehostPass= # Password for file transfers
|
|
|
57 |
ZimFilehostAddress= # Host address for file transfers
|
|
|
58 |
ZimFilehostFolder= # Folder on host where files will be placed during file transfer
|
2 |
soulskater |
59 |
|
|
|
60 |
#### File Delete Settings ####
|
|
|
61 |
ZimDeleteLocalFile=no # Enable/Disable compressed backup file deletion after successful backup(yes/no)
|
5 |
soulskater |
62 |
ZimDeleteTimeSet=0 # Set in minutes above 0 to keep a desired amount of files locally,
|
|
|
63 |
# be sure to match the time with your backup schedules.
|
2 |
soulskater |
64 |
|
|
|
65 |
##### Do not change anything below this line unless you know what you are doing #####
|
|
|
66 |
|
|
|
67 |
# Fetch backup type
|
|
|
68 |
ZimBackupType=$1
|
|
|
69 |
|
|
|
70 |
pre_check() {
|
|
|
71 |
# Set default abort value
|
|
|
72 |
ZimAbort=0
|
|
|
73 |
|
|
|
74 |
# Check for rsync
|
|
|
75 |
if [ ! -e /usr/bin/rsync ]
|
|
|
76 |
then
|
|
|
77 |
echo "Cannot find rsync software..."
|
|
|
78 |
ZimAbort=1
|
|
|
79 |
fi
|
|
|
80 |
|
|
|
81 |
# Check for tar
|
|
|
82 |
if [ ! -e /bin/tar ]
|
|
|
83 |
then
|
|
|
84 |
echo "Cannot find tar software..."
|
|
|
85 |
ZimAbort=1
|
|
|
86 |
fi
|
|
|
87 |
|
|
|
88 |
# Check for gzip
|
|
|
89 |
if [ ! -e /bin/gzip ]
|
|
|
90 |
then
|
|
|
91 |
if [ ! -e /usr/bin/gzip ]
|
|
|
92 |
then
|
|
|
93 |
echo "Cannot find gzip software..."
|
|
|
94 |
ZimAbort=1
|
|
|
95 |
fi
|
|
|
96 |
fi
|
|
|
97 |
|
|
|
98 |
# Check if expect, ftp & scp is installed when file transfer is set to yes
|
|
|
99 |
if [ $ZimFtpEnable = 'yes' ] || [ $ZimScpEnable = 'yes' ]
|
|
|
100 |
then
|
|
|
101 |
if [ ! -e /usr/bin/expect ]
|
|
|
102 |
then
|
|
|
103 |
echo "cannot find expect command..."
|
|
|
104 |
ZimAbort=1
|
|
|
105 |
fi
|
|
|
106 |
if [ $ZimFtpEnable = 'yes' ]
|
|
|
107 |
then
|
|
|
108 |
if [ ! -e /usr/bin/ftp ]
|
|
|
109 |
then
|
|
|
110 |
echo "Cannot find ftp command..."
|
|
|
111 |
ZimAbort=1
|
|
|
112 |
fi
|
|
|
113 |
fi
|
|
|
114 |
|
|
|
115 |
if [ $ZimScpEnable = 'yes' ]
|
|
|
116 |
then
|
|
|
117 |
if [ ! -e /usr/bin/scp ]
|
|
|
118 |
then
|
|
|
119 |
echo "Cannot find scp command..."
|
|
|
120 |
ZimAbort=1
|
|
|
121 |
fi
|
|
|
122 |
fi
|
|
|
123 |
fi
|
|
|
124 |
|
|
|
125 |
if [ $ZimAbort = '1' ]
|
|
|
126 |
then
|
|
|
127 |
echo "Please install above software, script exiting..."
|
|
|
128 |
exit
|
|
|
129 |
fi
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
pre_load() {
|
|
|
133 |
# Get temporary folder layout
|
|
|
134 |
temp_folders
|
|
|
135 |
|
|
|
136 |
# Get backup file format
|
|
|
137 |
backup_file
|
|
|
138 |
|
|
|
139 |
# Get log file format
|
|
|
140 |
log_file
|
|
|
141 |
|
|
|
142 |
# Check to see if the backup folder exist, create if not
|
|
|
143 |
mkdir -p $ZimBackupPath
|
|
|
144 |
|
|
|
145 |
# Check which zimbra version that is installed, for recovery purposes
|
|
|
146 |
sudo -u zimbra $ZimInstPath/$ZimHome/bin/zmcontrol -v > $ZimBackupPath/zimbra_version.txt
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
full_backup() {
|
5 |
soulskater |
150 |
# Cleaning LDAP database from backup folder if exist.
|
|
|
151 |
if [ -f "$ZimDiffTmpPath/$ZimHome/data/ldap/mdb/db/data.mdb" ];
|
|
|
152 |
then
|
|
|
153 |
rm $ZimDiffTmpPath/$ZimHome/data/ldap/mdb/db/data.mdb
|
|
|
154 |
fi
|
|
|
155 |
|
|
|
156 |
# Making backup
|
2 |
soulskater |
157 |
if [ $ZimLogEnable = 'yes' ] && [ $ZimLogVerbose = 'yes' ]
|
|
|
158 |
then
|
|
|
159 |
|
|
|
160 |
# Hot sync before shutdown on zimbra folder
|
|
|
161 |
echo "`date "+%Y-%m-%d %H:%M:%S"` - Hot syncing to backup folder..." >> $ZimLogFile
|
5 |
soulskater |
162 |
rsync -avHK --delete --exclude 'data/ldap/mdb/db/data.mdb' $ZimInstPath/$ZimHome $ZimFullTmpPath >> $ZimLogFile
|
2 |
soulskater |
163 |
echo "`date "+%Y-%m-%d %H:%M:%S"` - Hot syncing to backup folder done." >> $ZimLogFile
|
|
|
164 |
|
|
|
165 |
# Stopping Zimbra
|
|
|
166 |
zimbra_stop
|
|
|
167 |
|
|
|
168 |
# Cold sync of zimbra folder
|
|
|
169 |
echo "`date "+%Y-%m-%d %H:%M:%S"` - Cold syncing to backup folder..." >> $ZimLogFile
|
5 |
soulskater |
170 |
rsync -avHK --delete --exclude 'data/ldap/mdb/db/data.mdb' $ZimInstPath/$ZimHome $ZimFullTmpPath >> $ZimLogFile
|
|
|
171 |
# Special LDAP DB copying needed.
|
|
|
172 |
$ZimInstPath/$ZimHome/openldap/bin/mdb_copy $ZimInstPath/$ZimHome/data/ldap/mdb/db $ZimFullTmpPath/$ZimHome/data/ldap/mdb/db >> $ZimLogFile
|
2 |
soulskater |
173 |
echo "`date "+%Y-%m-%d %H:%M:%S"` - Cold syncing to backup folder done." >> $ZimLogFile
|
|
|
174 |
|
|
|
175 |
# Starting Zimbra
|
5 |
soulskater |
176 |
zimbra_start
|
2 |
soulskater |
177 |
|
|
|
178 |
# Compressing backup for space reduction
|
|
|
179 |
echo "`date "+%Y-%m-%d %H:%M:%S"` - Compressing backup folder..." >> $ZimLogFile
|
|
|
180 |
tar -zcvpf $ZimBackupPath/$ZimBackupFile -C $ZimBackupPath tf zimbra_version.txt >> $ZimLogFile
|
|
|
181 |
echo "`date "+%Y-%m-%d %H:%M:%S"` - Compressed backup folder." >> $ZimLogFile
|
|
|
182 |
|
|
|
183 |
# Cleaning differential folder.
|
|
|
184 |
echo "`date "+%Y-%m-%d %H:%M:%S"` - Cleaning differential backup folder..."
|
|
|
185 |
rm -r -f $ZimDiffTmpPath/* >> $ZimLogFile
|
|
|
186 |
echo "`date "+%Y-%m-%d %H:%M:%S"` - Cleaned differential backup folder."
|
5 |
soulskater |
187 |
|
2 |
soulskater |
188 |
else
|
|
|
189 |
|
|
|
190 |
# Hot sync before shutdown on zimbra folder
|
5 |
soulskater |
191 |
rsync -avHK --delete --exclude 'data/ldap/mdb/db/data.mdb' $ZimInstPath/$ZimHome $ZimFullTmpPath
|
2 |
soulskater |
192 |
|
|
|
193 |
# Stopping Zimbra
|
|
|
194 |
zimbra_stop
|
|
|
195 |
|
|
|
196 |
# Cold sync of zimbra folder
|
5 |
soulskater |
197 |
rsync -avHK --delete --exclude 'data/ldap/mdb/db/data.mdb' $ZimInstPath/$ZimHome $ZimFullTmpPath
|
|
|
198 |
# Special LDAP DB copying needed.
|
|
|
199 |
$ZimInstPath/$ZimHome/openldap/bin/mdb_copy $ZimInstPath/$ZimHome/data/ldap/mdb/db $ZimFullTmpPath/$ZimHome/data/ldap/mdb/db
|
2 |
soulskater |
200 |
|
|
|
201 |
# Starting Zimbra
|
|
|
202 |
zimbra_start
|
|
|
203 |
|
|
|
204 |
# Compressing backup for space reduction
|
|
|
205 |
tar -zcvpf $ZimBackupPath/$ZimBackupFile -C $ZimBackupPath tf zimbra_version.txt
|
5 |
soulskater |
206 |
|
2 |
soulskater |
207 |
# Cleaning differential folder.
|
5 |
soulskater |
208 |
rm -r -f $ZimDiffTmpPath/*
|
2 |
soulskater |
209 |
fi
|
|
|
210 |
}
|
|
|
211 |
|
|
|
212 |
diff_backup() {
|
5 |
soulskater |
213 |
# Checking if LDAP database folder exist and fix it.
|
|
|
214 |
if [ ! -f "$ZimDiffTmpPath/$ZimHome/data/ldap/mdb/db/data.mdb" ];
|
|
|
215 |
then
|
|
|
216 |
if [ ! -d "$ZimDiffTmpPath/$ZimHome/data/ldap/mdb/db" ]
|
|
|
217 |
then
|
|
|
218 |
mkdir -p $ZimDiffTmpPath/$ZimHome/data/ldap/mdb/db
|
|
|
219 |
fi
|
|
|
220 |
else
|
|
|
221 |
rm -R $ZimDiffTmpPath/$ZimHome/data/ldap/mdb/db/data.mdb
|
|
|
222 |
fi
|
|
|
223 |
|
|
|
224 |
# Make backup
|
2 |
soulskater |
225 |
if [ $ZimLogEnable = 'yes' ] && [ $ZimLogVerbose = 'yes' ]
|
|
|
226 |
then
|
|
|
227 |
|
|
|
228 |
# Hot syncing to backup folder
|
|
|
229 |
echo "`date "+%Y-%m-%d %H:%M:%S"` - Hot syncing to backup folder..." >> $ZimLogFile
|
5 |
soulskater |
230 |
rsync -avHK --exclude 'data/ldap/mdb/db/data.mdb' --compare-dest=$ZimFullTmpPath/ $ZimInstPath/$ZimHome $ZimDiffTmpPath >> $ZimLogFile
|
2 |
soulskater |
231 |
echo "`date "+%Y-%m-%d %H:%M:%S"` - Hot syncing to backup folder done." >> $ZimLogFile
|
|
|
232 |
|
|
|
233 |
# Stopping Zimbra
|
|
|
234 |
zimbra_stop
|
|
|
235 |
|
|
|
236 |
# Cold syncing to backup folder
|
|
|
237 |
echo "`date "+%Y-%m-%d %H:%M:%S"` - Cold syncing to backup folder..." >> $ZimLogFile
|
5 |
soulskater |
238 |
rsync -avHK --exclude 'data/ldap/mdb/db/data.mdb' --compare-dest=$ZimFullTmpPath/ $ZimInstPath/$ZimHome $ZimDiffTmpPath >> $ZimLogFile
|
|
|
239 |
# Special LDAP DB copying needed.
|
|
|
240 |
$ZimInstPath/$ZimHome/openldap/bin/mdb_copy $ZimInstPath/$ZimHome/data/ldap/mdb/db $ZimDiffTmpPath/$ZimHome/data/ldap/mdb/db >> $ZimLogFile
|
2 |
soulskater |
241 |
echo "`date "+%Y-%m-%d %H:%M:%S"` - Cold syncing to backup folder done." >> $ZimLogFile
|
|
|
242 |
|
|
|
243 |
# Starting Zimbra
|
|
|
244 |
zimbra_start
|
|
|
245 |
|
|
|
246 |
# Compressing backup folder
|
|
|
247 |
echo "`date "+%Y-%m-%d %H:%M:%S"` - Compressing backup folder..." >> $ZimLogFile
|
|
|
248 |
tar -zcvpf $ZimBackupPath/$ZimBackupFile -C $ZimBackupPath td zimbra_version.txt >> $ZimLogFile
|
|
|
249 |
echo "`date "+%Y-%m-%d %H:%M:%S"` - Compressing backup folder done." >> $ZimLogFile
|
|
|
250 |
|
|
|
251 |
else
|
|
|
252 |
|
|
|
253 |
# Hot sync before shutdown on zimbra folder
|
|
|
254 |
#rm -R $ZimDiffTmpPath/*
|
5 |
soulskater |
255 |
rsync -avHK --exclude 'data/ldap/mdb/db/data.mdb' --compare-dest=$ZimFullTmpPath/ $ZimInstPath/$ZimHome $ZimDiffTmpPath
|
2 |
soulskater |
256 |
|
|
|
257 |
# Stopping Zimbra
|
|
|
258 |
zimbra_stop
|
|
|
259 |
|
|
|
260 |
# Cold sync of zimbra folder
|
5 |
soulskater |
261 |
rsync -avHK --exclude 'data/ldap/mdb/db/data.mdb' --compare-dest=$ZimFullTmpPath/ $ZimInstPath/$ZimHome $ZimDiffTmpPath
|
|
|
262 |
# Special LDAP DB copying needed.
|
|
|
263 |
$ZimInstPath/$ZimHome/openldap/bin/mdb_copy $ZimInstPath/$ZimHome/data/ldap/mdb/db $ZimDiffTmpPath/$ZimHome/data/ldap/mdb/db
|
2 |
soulskater |
264 |
# Starting Zimbra
|
|
|
265 |
zimbra_start
|
|
|
266 |
|
|
|
267 |
# Compressing backup for space reduction
|
|
|
268 |
tar -zcvpf $ZimBackupPath/$ZimBackupFile -C $ZimBackupPath td zimbra_version.txt
|
|
|
269 |
fi
|
|
|
270 |
}
|
|
|
271 |
|
|
|
272 |
msgfull_backup() {
|
|
|
273 |
if [ $ZimLogEnable = 'yes' ] && [ $ZimLogVerbose = 'yes' ]
|
|
|
274 |
then
|
|
|
275 |
# Hot syncing to backup folder
|
|
|
276 |
echo "`date "+%Y-%m-%d %H:%M:%S"` - Hot syncing to backup folder..." >> $ZimLogFile
|
|
|
277 |
rsync -avHK --delete $ZimInstPath/$ZimHome/store $ZimMsgFullTmpPath >> $ZimLogFile
|
|
|
278 |
echo "`date "+%Y-%m-%d %H:%M:%S"` - Hot syncing to backup folder done." >> $ZimLogFile
|
|
|
279 |
|
|
|
280 |
# Compressing backup folder
|
|
|
281 |
echo "`date "+%Y-%m-%d %H:%M:%S"` - Compressing backup folder..." >> $ZimLogFile
|
|
|
282 |
tar -zcvpf $ZimBackupPath/$ZimBackupFile -C $ZimBackupPath tmf zimbra_version.txt >> $ZimLogFile
|
|
|
283 |
echo "`date "+%Y-%m-%d %H:%M:%S"` - Compressing backup folder done." >> $ZimLogFile
|
5 |
soulskater |
284 |
|
2 |
soulskater |
285 |
echo "`date "+%Y-%m-%d %H:%M:%S"` - Cleaning differential backup folder..."
|
|
|
286 |
rm -r -f $ZimMsgDiffTmpPath/* >> $ZimLogFile
|
|
|
287 |
echo "`date "+%Y-%m-%d %H:%M:%S"` - Cleaned differential backup folder."
|
|
|
288 |
|
|
|
289 |
else
|
|
|
290 |
# Hot syncing to backup folder
|
|
|
291 |
rsync -avHK --delete $ZimInstPath/$ZimHome/store $ZimMsgFullTmpPath
|
|
|
292 |
|
|
|
293 |
# Compressing backup folder
|
|
|
294 |
tar -zcvpf $ZimBackupPath/$ZimBackupFile -C $ZimBackupPath tmf zimbra_version.txt
|
5 |
soulskater |
295 |
|
2 |
soulskater |
296 |
# Cleaning differential folder
|
|
|
297 |
rm -r -f $ZimMsgDiffTmpPath/*
|
|
|
298 |
fi
|
|
|
299 |
}
|
|
|
300 |
|
|
|
301 |
msgdiff_backup() {
|
|
|
302 |
if [ $ZimLogEnable = 'yes' ] && [ $ZimLogVerbose = 'yes' ]
|
|
|
303 |
then
|
|
|
304 |
# Cleaning backup folder
|
|
|
305 |
#echo "`date "+%Y-%m-%d %H:%M:%S"` - Cleaning backup folder..." >> $ZimLogFile
|
|
|
306 |
#rm -r -f $ZimMsgDiffTmpPath/* >> $ZimLogFile
|
|
|
307 |
#echo "`date "+%Y-%m-%d %H:%M:%S"` - Cleaning backup folder done." >> $ZimLogFile
|
|
|
308 |
|
|
|
309 |
# Hot syncing to backup folder
|
|
|
310 |
echo "`date "+%Y-%m-%d %H:%M:%S"` - Hot syncing to backup folder..." >> $ZimLogFile
|
|
|
311 |
rsync -avHK --compare-dest=$ZimMsgFullTmpPath/ $ZimInstPath/$ZimHome/store $ZimMsgDiffTmpPath >> $ZimLogFile
|
|
|
312 |
echo "`date "+%Y-%m-%d %H:%M:%S"` - Hot syncing to backup folder done." >> $ZimLogFile
|
|
|
313 |
|
|
|
314 |
# Compressing backup folder
|
|
|
315 |
echo "`date "+%Y-%m-%d %H:%M:%S"` - Compressing backup folder..." >> $ZimLogFile
|
|
|
316 |
tar -zcvpf $ZimBackupPath/$ZimBackupFile -C $ZimBackupPath tmd zimbra_version.txt >> $ZimLogFile
|
|
|
317 |
echo "`date "+%Y-%m-%d %H:%M:%S"` - Compressing backup folder done." >> $ZimLogFile
|
|
|
318 |
|
|
|
319 |
else
|
|
|
320 |
# Cleaning backup folder
|
|
|
321 |
#rm -r -f $ZimMsgDiffTmpPath/*
|
|
|
322 |
|
|
|
323 |
# Hot syncing to backup folder
|
|
|
324 |
rsync -avHK --compare-dest=$ZimMsgFullTmpPath/ $ZimInstPath/$ZimHome/store $ZimMsgDiffTmpPath
|
|
|
325 |
|
|
|
326 |
# Compressing backup folder
|
|
|
327 |
tar -zcvpf $ZimBackupPath/$ZimBackupFile -C $ZimBackupPath tmd zimbra_version.txt
|
|
|
328 |
fi
|
|
|
329 |
}
|
|
|
330 |
|
|
|
331 |
file_transfer() {
|
|
|
332 |
# Transfer with ftp
|
|
|
333 |
if [ $ZimFtpEnable == "yes" ]
|
|
|
334 |
then
|
|
|
335 |
if [ "$2" = "--no-send" ]
|
|
|
336 |
then
|
|
|
337 |
# If --no-send is set ignore sending file to off site.
|
|
|
338 |
echo "`date "+%Y-%m-%d %H:%M:%S"` - Backup not sending backup file to external storage, --no-send is set." >> $ZimLogFile
|
|
|
339 |
echo "Not sending backup to external storage, --no-send is set."
|
|
|
340 |
else
|
|
|
341 |
if [ $ZimLogEnable = 'yes' ] && [ $ZimLogVerbose = 'yes' ]
|
|
|
342 |
then
|
|
|
343 |
# Send task start time to log
|
|
|
344 |
echo "`date "+%Y-%m-%d %H:%M:%S"` - Sending file via ftp to off-site storage..." >> $ZimLogFile
|
|
|
345 |
|
|
|
346 |
# Make a temporary script for expect commands
|
|
|
347 |
touch $ZimBackupPath/ftp.exp
|
|
|
348 |
|
|
|
349 |
# Fill script with commands
|
|
|
350 |
echo '#!/usr/bin/expect --' >> $ZimBackupPath/ftp.exp
|
|
|
351 |
echo 'set timeout -1' >> $ZimBackupPath/ftp.exp
|
|
|
352 |
echo 'spawn ftp '$ZimFilehostAddress >> $ZimBackupPath/ftp.exp
|
|
|
353 |
echo 'expect ):' >> $ZimBackupPath/ftp.exp
|
|
|
354 |
echo 'send '$ZimFilehostUser'\r' >> $ZimBackupPath/ftp.exp
|
|
|
355 |
echo 'expect :' >> $ZimBackupPath/ftp.exp
|
|
|
356 |
echo 'send '$ZimFilehostPass'\r' >> $ZimBackupPath/ftp.exp
|
|
|
357 |
echo 'expect >' >> $ZimBackupPath/ftp.exp
|
|
|
358 |
echo 'send '$ZimFtpOpt'\r' >> $ZimBackupPath/ftp.exp
|
|
|
359 |
echo 'send "send '$ZimBackupPath/$ZimBackupFile $ZimFilehostFolder/$ZimBackupFile'\r"' >> $ZimBackupPath/ftp.exp
|
|
|
360 |
echo 'expect >' >> $ZimBackupPath/ftp.exp
|
|
|
361 |
echo 'send quit\r' >> $ZimBackupPath/ftp.exp
|
|
|
362 |
echo 'expect closed' >> $ZimBackupPath/ftp.exp
|
|
|
363 |
|
|
|
364 |
# Run expect with created script
|
|
|
365 |
expect $ZimBackupPath/ftp.exp >> $ZimLogFile
|
|
|
366 |
|
|
|
367 |
# Delete temporary expect script
|
|
|
368 |
rm $ZimBackupPath/ftp.exp
|
|
|
369 |
|
|
|
370 |
# Send task stop time to log
|
|
|
371 |
echo "`date "+%Y-%m-%d %H:%M:%S"` - Sending file via ftp to off-site storage done." >> $ZimLogFile
|
|
|
372 |
|
|
|
373 |
else
|
|
|
374 |
|
|
|
375 |
# Send task start time to log
|
|
|
376 |
echo "`date "+%Y-%m-%d %H:%M:%S"` - Sending file via ftp to off-site storage..." >> $ZimLogFile
|
|
|
377 |
|
|
|
378 |
# Make a temporary script for expect commands
|
|
|
379 |
touch $ZimBackupPath/ftp.exp
|
|
|
380 |
|
|
|
381 |
# Fill script with commands
|
|
|
382 |
echo '#!/usr/bin/expect --' >> $ZimBackupPath/ftp.exp
|
|
|
383 |
echo 'set timeout -1' >> $ZimBackupPath/ftp.exp
|
|
|
384 |
echo 'spawn ftp '$ZimFilehostAddress >> $ZimBackupPath/ftp.exp
|
|
|
385 |
echo 'expect ):' >> $ZimBackupPath/ftp.exp
|
|
|
386 |
echo 'send '$ZimFilehostUser'\r' >> $ZimBackupPath/ftp.exp
|
|
|
387 |
echo 'expect :' >> $ZimBackupPath/ftp.exp
|
|
|
388 |
echo 'send '$ZimFilehostPass'\r' >> $ZimBackupPath/ftp.exp
|
|
|
389 |
echo 'expect >' >> $ZimBackupPath/ftp.exp
|
|
|
390 |
echo 'send '$ZimFtpOpt'\r' >> $ZimBackupPath/ftp.exp
|
|
|
391 |
echo 'send "send '$ZimBackupPath/$ZimBackupFile $ZimFilehostFolder/$ZimBackupFile'\r"' >> $ZimBackupPath/ftp.exp
|
|
|
392 |
echo 'expect >' >> $ZimBackupPath/ftp.exp
|
|
|
393 |
echo 'send quit\r' >> $ZimBackupPath/ftp.exp
|
|
|
394 |
echo 'expect closed' >> $ZimBackupPath/ftp.exp
|
|
|
395 |
|
|
|
396 |
# Run expect with created script
|
|
|
397 |
expect $ZimBackupPath/ftp.exp
|
|
|
398 |
|
|
|
399 |
# Delete temporary expect script
|
|
|
400 |
rm $ZimBackupPath/ftp.exp
|
|
|
401 |
|
|
|
402 |
# Send task stop time to log
|
|
|
403 |
echo "`date "+%Y-%m-%d %H:%M:%S"` - Sending file via ftp to off-site storage done." >> $ZimLogFile
|
|
|
404 |
|
|
|
405 |
fi
|
|
|
406 |
fi
|
|
|
407 |
fi
|
|
|
408 |
|
|
|
409 |
# Transfer with scp
|
|
|
410 |
if [ $ZimScpEnable == "yes" ]
|
|
|
411 |
then
|
|
|
412 |
if [ "$2" = "--no-send" ]
|
|
|
413 |
then
|
|
|
414 |
# If --no-send is set ignore sending file to off site.
|
|
|
415 |
echo "`date "+%Y-%m-%d %H:%M:%S"` - Backup not sending backup file to external storage, --no-send is set." >> $ZimLogFile
|
|
|
416 |
echo "Not sending backup to external storage, --no-send is set."
|
|
|
417 |
else
|
|
|
418 |
if [ $ZimLogEnable = 'yes' ] && [ $ZimLogVerbose = 'yes' ]
|
|
|
419 |
then
|
|
|
420 |
|
|
|
421 |
# Send task start time to log
|
|
|
422 |
echo "`date "+%Y-%m-%d %H:%M:%S"` - Sending file via scp to off-site storage..." >> $ZimLogFile
|
|
|
423 |
|
|
|
424 |
# Make a temporary script for expect commands
|
|
|
425 |
touch $ZimBackupPath/scp.exp
|
|
|
426 |
|
|
|
427 |
# Fill script with commands
|
|
|
428 |
echo '#!/usr/bin/expect --' >> $ZimBackupPath/scp.exp
|
|
|
429 |
echo 'set timeout -1' >> $ZimBackupPath/scp.exp
|
|
|
430 |
echo 'spawn scp '$ZimScpOpt $ZimBackupPath/$ZimBackupFile $ZimFilehostUser'@'$ZimFilehostAddress':'$ZimFilehostFolder >> $ZimBackupPath/scp.exp
|
|
|
431 |
echo 'expect :' >> $ZimBackupPath/scp.exp
|
|
|
432 |
echo 'send '$ZimFilehostPass'\r' >> $ZimBackupPath/scp.exp
|
|
|
433 |
echo 'expect closed' >> $ZimBackupPath/scp.exp
|
|
|
434 |
|
|
|
435 |
# Run expect with created script
|
|
|
436 |
expect $ZimBackupPath/scp.exp >> $ZimLogFile
|
|
|
437 |
|
|
|
438 |
# Delete temporary expect script
|
|
|
439 |
rm $ZimBackupPath/scp.exp
|
|
|
440 |
|
|
|
441 |
# Send task stop time to log
|
|
|
442 |
echo "`date "+%Y-%m-%d %H:%M:%S"` - Sending file via scp to off-site storage done." >> $ZimLogFile
|
|
|
443 |
|
|
|
444 |
else
|
|
|
445 |
|
|
|
446 |
# Send task start time to log
|
|
|
447 |
echo "`date "+%Y-%m-%d %H:%M:%S"` - Sending file via scp to off-site storage..." >> $ZimLogFile
|
|
|
448 |
|
|
|
449 |
# Make a temporary script for expect commands
|
|
|
450 |
touch $ZimBackupPath/scp.exp
|
|
|
451 |
|
|
|
452 |
# Fill script with commands
|
|
|
453 |
echo '#!/usr/bin/expect --' >> $ZimBackupPath/scp.exp
|
|
|
454 |
echo 'set timeout -1' >> $ZimBackupPath/scp.exp
|
|
|
455 |
echo 'spawn scp '$ZimScpOpt $ZimBackupPath/$ZimBackupFile $ZimFilehostUser'@'$ZimFilehostAddress':'$ZimFilehostFolder >> $ZimBackupPath/scp.exp
|
|
|
456 |
echo 'expect :' >> $ZimBackupPath/scp.exp
|
|
|
457 |
echo 'send '$ZimFilehostPass'\r' >> $ZimBackupPath/scp.exp
|
|
|
458 |
echo 'expect closed' >> $ZimBackupPath/scp.exp
|
|
|
459 |
|
|
|
460 |
# Run expect with created script
|
|
|
461 |
expect $ZimBackupPath/scp.exp
|
|
|
462 |
|
|
|
463 |
# Delete temporary expect script
|
|
|
464 |
rm $ZimBackupPath/scp.exp
|
|
|
465 |
|
|
|
466 |
# Send task stop time to log
|
|
|
467 |
echo "`date "+%Y-%m-%d %H:%M:%S"` - Sending file via scp to off-site storage done." >> $ZimLogFile
|
|
|
468 |
|
|
|
469 |
fi
|
|
|
470 |
fi
|
|
|
471 |
fi
|
|
|
472 |
|
|
|
473 |
# Remove local file(s) if ZimDeleteLocalFile is set to 'yes'
|
|
|
474 |
if [ $ZimDeleteLocalFile == "yes" ]
|
|
|
475 |
then
|
|
|
476 |
if [ $ZimLogEnable = 'yes' ] && [ $ZimLogVerbose = 'yes' ]
|
|
|
477 |
then
|
|
|
478 |
|
|
|
479 |
# Send task start time to log
|
|
|
480 |
echo "`date "+%Y-%m-%d %H:%M:%S"` - Deleting local file(s)..." >> $ZimLogFile
|
|
|
481 |
|
|
|
482 |
# Remove backup files matching criteria
|
|
|
483 |
find $ZimBackupPath -maxdepth 1 -type f -mmin +$ZimDeleteTimeSet -name ZimBackup\*.tar.gz -exec rm {} + >> $ZimLogFile
|
|
|
484 |
|
|
|
485 |
# Send task stop time to log
|
|
|
486 |
echo "`date "+%Y-%m-%d %H:%M:%S"` - Deleting local file(s) done." >> $ZimLogFile
|
|
|
487 |
|
|
|
488 |
else
|
|
|
489 |
|
|
|
490 |
# Remove backup files matching criteria
|
|
|
491 |
find $ZimBackupPath -maxdepth 1 -type f -mmin +$ZimDeleteTimeSet -name ZimBackup\*.tar.gz -exec rm {} +
|
|
|
492 |
|
|
|
493 |
fi
|
|
|
494 |
fi
|
|
|
495 |
}
|
|
|
496 |
|
|
|
497 |
log_start() {
|
|
|
498 |
if [ $ZimLogEnable = 'yes' ]
|
|
|
499 |
then
|
|
|
500 |
|
|
|
501 |
# Sending backup start time to log
|
|
|
502 |
echo "" >> $ZimLogFile
|
|
|
503 |
echo "-------------------------------------------------------" >> $ZimLogFile
|
|
|
504 |
echo "Backup Started: `date "+%Y-%m-%d %H:%M:%S"` Type: $ZimBackupType" >> $ZimLogFile
|
|
|
505 |
|
|
|
506 |
fi
|
|
|
507 |
}
|
|
|
508 |
|
|
|
509 |
log_end() {
|
|
|
510 |
if [ $ZimLogEnable = 'yes' ]
|
|
|
511 |
then
|
|
|
512 |
|
|
|
513 |
# Sending backup stop time to log
|
|
|
514 |
echo "Backup Finished: `date "+%Y-%m-%d %H:%M:%S"` Type: $ZimBackupType" >> $ZimLogFile
|
|
|
515 |
echo "-------------------------------------------------------" >> $ZimLogFile
|
|
|
516 |
|
|
|
517 |
fi
|
|
|
518 |
}
|
|
|
519 |
|
|
|
520 |
zimbra_start() {
|
|
|
521 |
if [ "$2" = "--no-start" ]
|
|
|
522 |
then
|
|
|
523 |
# If --no-start is set ignore starting Zimbra
|
|
|
524 |
echo "`date "+%Y-%m-%d %H:%M:%S"` - Zimbra services not starting, --no-start is set." >> $ZimLogFile
|
|
|
525 |
echo "Zimbra services not starting, --no-start is set."
|
|
|
526 |
else
|
|
|
527 |
# Starting Zimbra
|
|
|
528 |
if [ $ZimLogEnable = 'yes' ]
|
|
|
529 |
then
|
|
|
530 |
# Sending task start time to log
|
|
|
531 |
echo "`date "+%Y-%m-%d %H:%M:%S"` - Zimbra services starting..." >> $ZimLogFile
|
|
|
532 |
|
|
|
533 |
# Starting Zimbra
|
4 |
soulskater |
534 |
su zimbra -c -l "$ZimInstPath/$ZimHome/bin/zmcontrol start" >> $ZimLogFile
|
2 |
soulskater |
535 |
|
|
|
536 |
# Send task stop time to log
|
|
|
537 |
echo "`date "+%Y-%m-%d %H:%M:%S"` - Zimbra services started." >> $ZimLogFile
|
|
|
538 |
|
|
|
539 |
else
|
|
|
540 |
|
|
|
541 |
# Starting Zimbra
|
4 |
soulskater |
542 |
su zimbra -c -l "$ZimInstPath/$ZimHome/bin/zmcontrol start"
|
2 |
soulskater |
543 |
|
|
|
544 |
fi
|
|
|
545 |
fi
|
|
|
546 |
}
|
|
|
547 |
|
|
|
548 |
zimbra_stop() {
|
|
|
549 |
# Stopping Zimbra
|
|
|
550 |
if [ $ZimLogEnable = 'yes' ]
|
|
|
551 |
then
|
|
|
552 |
|
|
|
553 |
# Sending task start time to log
|
|
|
554 |
echo "`date "+%Y-%m-%d %H:%M:%S"` - Zimbra services stopping..." >> $ZimLogFile
|
|
|
555 |
|
|
|
556 |
# Stopping Zimbra
|
4 |
soulskater |
557 |
su zimbra -c -l "$ZimInstPath/$ZimHome/bin/zmcontrol stop" >> $ZimLogFile
|
2 |
soulskater |
558 |
|
|
|
559 |
# Sending task stop time to log
|
|
|
560 |
echo "`date "+%Y-%m-%d %H:%M:%S"` - Zimbra services stopped." >> $ZimLogFile
|
|
|
561 |
|
|
|
562 |
# Sleep for 10 seconds to give shutdown some extra time before backup starts
|
|
|
563 |
sleep 10
|
|
|
564 |
|
|
|
565 |
else
|
|
|
566 |
|
|
|
567 |
# Stopping Zimbra
|
4 |
soulskater |
568 |
su zimbra -c -l "$ZimInstPath/$ZimHome/bin/zmcontrol stop"
|
2 |
soulskater |
569 |
|
|
|
570 |
# Sleep for 10 seconds to give shutdown some extra time before backup starts
|
|
|
571 |
sleep 10
|
|
|
572 |
|
|
|
573 |
fi
|
|
|
574 |
}
|
|
|
575 |
|
|
|
576 |
backup_file() {
|
|
|
577 |
# Checks what backup is choosen and sets file-name appropiate
|
|
|
578 |
|
|
|
579 |
# Full system backup
|
|
|
580 |
if [ "$ZimBackupType" = "--full" ]
|
|
|
581 |
then
|
|
|
582 |
ZimBackupFile=ZimBackupSystemFull_`date +%Y%m%d%H%M`.tar.gz
|
|
|
583 |
fi
|
|
|
584 |
|
|
|
585 |
# Differential system backup
|
|
|
586 |
if [ "$ZimBackupType" = "--diff" ]
|
|
|
587 |
then
|
|
|
588 |
ZimBackupFile=ZimBackupSystemDiff_`date +%Y%m%d%H%M`.tar.gz
|
|
|
589 |
fi
|
|
|
590 |
|
|
|
591 |
# Full message backup
|
|
|
592 |
if [ "$ZimBackupType" = "--msg-full" ]
|
|
|
593 |
then
|
|
|
594 |
ZimBackupFile=ZimBackupMsgFull_`date +%Y%m%d%H%M`.tar.gz
|
|
|
595 |
fi
|
|
|
596 |
|
|
|
597 |
# Differential message backup
|
|
|
598 |
if [ "$ZimBackupType" = "--msg-diff" ]
|
|
|
599 |
then
|
|
|
600 |
ZimBackupFile=ZimBackupMsgDiff_`date +%Y%m%d%H%M`.tar.gz
|
|
|
601 |
fi
|
|
|
602 |
}
|
|
|
603 |
|
|
|
604 |
log_file() {
|
|
|
605 |
# Check to see if the log folder exist, create if not
|
|
|
606 |
mkdir -p $ZimLogPath
|
|
|
607 |
|
|
|
608 |
# Check log intervall and decide which file-name to use
|
|
|
609 |
if [ "$ZimLogLogRotate" = "yes" ]
|
|
|
610 |
then
|
|
|
611 |
|
|
|
612 |
# Set log file-name to day
|
|
|
613 |
if [ "$ZimLogRotateInt" = "day" ]
|
|
|
614 |
then
|
|
|
615 |
|
|
|
616 |
# If file do not exist create it
|
|
|
617 |
touch $ZimLogPath/ZimBackupDay_`date +%Y-%m-%d`.log
|
|
|
618 |
|
|
|
619 |
# Set correct file-name
|
|
|
620 |
ZimLogFile=$ZimLogPath/ZimBackupDay_`date +%Y-%m-%d`.log
|
|
|
621 |
fi
|
|
|
622 |
|
|
|
623 |
# Set log file-name to week
|
|
|
624 |
if [ "$ZimLogRotateInt" = "week" ]
|
|
|
625 |
then
|
|
|
626 |
|
|
|
627 |
# If file do not exist create it
|
|
|
628 |
touch $ZimLogPath/ZimBackupWeek_`date +%Y-%V`.log
|
|
|
629 |
|
|
|
630 |
# Set correct file-name
|
|
|
631 |
ZimLogFile=$ZimLogPath/ZimBackupWeek_`date +%Y-%V`.log
|
|
|
632 |
fi
|
|
|
633 |
|
|
|
634 |
# Set log file-name to month
|
|
|
635 |
if [ "$ZimLogRotateInt" = "month" ]
|
|
|
636 |
then
|
|
|
637 |
|
|
|
638 |
# If file do not exist create it
|
|
|
639 |
touch $ZimLogPath/ZimBackupMonth_`date +%Y-%m`.log
|
|
|
640 |
|
|
|
641 |
# Set correct file-name
|
|
|
642 |
ZimLogFile=$ZimLogPath/ZimBackupMonth_`date +%Y-%m`.log
|
|
|
643 |
fi
|
|
|
644 |
|
|
|
645 |
else
|
|
|
646 |
|
|
|
647 |
# If file do not exist create it
|
|
|
648 |
touch $ZimLogPath/ZimBackup.log
|
|
|
649 |
|
|
|
650 |
# Set correct file-name
|
|
|
651 |
ZimLogFile=$ZimLogPath/ZimBackup.log
|
|
|
652 |
|
|
|
653 |
fi
|
|
|
654 |
}
|
|
|
655 |
|
|
|
656 |
temp_folders() {
|
|
|
657 |
# Create & set temp folders under $ZimBackupPath
|
|
|
658 |
ZimFullTmpPath=$ZimBackupPath/tf
|
|
|
659 |
ZimDiffTmpPath=$ZimBackupPath/td
|
|
|
660 |
ZimMsgFullTmpPath=$ZimBackupPath/tmf
|
|
|
661 |
ZimMsgDiffTmpPath=$ZimBackupPath/tmd
|
|
|
662 |
mkdir -p $ZimFullTmpPath
|
|
|
663 |
mkdir -p $ZimDiffTmpPath
|
|
|
664 |
mkdir -p $ZimMsgFullTmpPath
|
|
|
665 |
mkdir -p $ZimMsgDiffTmpPath
|
|
|
666 |
}
|
|
|
667 |
|
|
|
668 |
script_help() {
|
|
|
669 |
# Show help for script
|
|
|
670 |
echo "oZimBackup.sh Usage:"
|
|
|
671 |
echo ""
|
|
|
672 |
echo "--full For full backup (Cold)"
|
|
|
673 |
echo "--diff For differential backup (Cold)"
|
|
|
674 |
echo "--msg-full For complete message backup (Hot)"
|
|
|
675 |
echo "--msg-diff For differential message backup (Hot)"
|
|
|
676 |
echo "--check Check if needed software is installed, depends"
|
|
|
677 |
echo " on script configuration"
|
|
|
678 |
echo "--help Shows this help"
|
|
|
679 |
echo ""
|
|
|
680 |
echo "--no-start Tells Zimbra to stay offline after backup"
|
|
|
681 |
#echo "<file to restore> File to restore from"
|
|
|
682 |
echo ""
|
|
|
683 |
echo "Ex. Full backup:"
|
|
|
684 |
echo " oZimBackup.sh --full"
|
|
|
685 |
echo ""
|
|
|
686 |
echo "Ex. Full differential backup leaving Zimbra in offline mode:"
|
|
|
687 |
echo " oZimBackup.sh --diff --no-start"
|
|
|
688 |
echo ""
|
|
|
689 |
}
|
|
|
690 |
|
|
|
691 |
case $1 in
|
|
|
692 |
--full)
|
|
|
693 |
pre_check
|
|
|
694 |
pre_load
|
|
|
695 |
log_start
|
|
|
696 |
full_backup
|
|
|
697 |
file_transfer
|
|
|
698 |
log_end
|
|
|
699 |
;;
|
|
|
700 |
--diff)
|
|
|
701 |
pre_check
|
|
|
702 |
pre_load
|
|
|
703 |
log_start
|
|
|
704 |
diff_backup
|
|
|
705 |
file_transfer
|
|
|
706 |
log_end
|
|
|
707 |
;;
|
|
|
708 |
--msg-full)
|
|
|
709 |
pre_check
|
|
|
710 |
pre_load
|
|
|
711 |
log_start
|
|
|
712 |
msgfull_backup
|
|
|
713 |
file_transfer
|
|
|
714 |
log_end
|
|
|
715 |
;;
|
|
|
716 |
--msg-diff)
|
|
|
717 |
pre_check
|
|
|
718 |
pre_load
|
|
|
719 |
log_start
|
|
|
720 |
msgdiff_backup
|
|
|
721 |
file_transfer
|
|
|
722 |
log_end
|
|
|
723 |
;;
|
|
|
724 |
--check)
|
|
|
725 |
pre_check
|
|
|
726 |
;;
|
|
|
727 |
--help)
|
|
|
728 |
script_help
|
|
|
729 |
;;
|
|
|
730 |
*)
|
|
|
731 |
script_help
|
|
|
732 |
;;
|
4 |
soulskater |
733 |
esac
|