-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup.sh
More file actions
executable file
·479 lines (420 loc) · 11.5 KB
/
backup.sh
File metadata and controls
executable file
·479 lines (420 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#!/bin/bash
#
#### Dependencies: ######
## rsync - for backing up files
## mysql - if you want to backup mysql db's
## bc - calculations
### NOTE: ####
## Backing up over SSH assumes you have a profile for the target setup in your ~/.ssh/config file.
# ------------------ begin config --------------------------
recipient=email@example.com
smtp_login=email@example.com
# Local directory where we'll be doing work and keeping copies of all archived files
storage="/media/backups"
# MySQL username and password and db's
DEFAULT_MYSQLUSER="mysqluser"
DEFAULT_MYSQLPASS="yourpasswordhere"
ROOTMSQL=false
# excludes
excludesdir="/root"
includedir="/root"
excvar="backup_excludes.var"
excusr="backup_excludes.usr"
exchome="backup_excludes.$target"
emailfooter="$0 - $(date) - $HOSTNAME"
lastbackup=$(grep " $target " $storage/backup_log 2>/dev/null|tail -n1)
# --------------------- end config ----------------------
usage() {
cat <<EOF
Usage: $0 [OPTIONS]
Options:
--target=<name> Specify the backup target.
--local Run the script in local mode.
--rootmysql
--mysqluser=<user> Override the default MySQL username.
--mysqlpass=<pass> Specify the MySQL password.
--help Display this help message.
Examples:
$0 --target=production
$0 --rootmysql
$0 --local
$0 --mysqluser=admin
$0 --mysqluser=admin --mysqlpass=supersecure
$0 --target=staging --local --mysqluser=admin --mysqlpass=topsecret
EOF
exit 1
}
if [[ $# -eq 0 ]]; then
echo "Error: No arguments provided."
usage
fi
for arg in "$@"; do
case $arg in
--target=*)
target="${arg#*=}"
shift
;;
--local)
local=true
target="$(hostname)"
shift
;;
--mysqluser=*)
MYSQLUSER="${arg#*=}"
USER_SPECIFIED=true
shift
;;
--mysqlpass=*)
MYSQLPASS="${arg#*=}"
shift
;;
--rootmysql)
ROOTMYSQL=true
shift
;;
--docker=*)
DOCKERID="${arg#*=}"
shift
;;
--help)
usage
;;
*)
echo "Unknown argument: $arg"
usage
;;
esac
done
MYSQLUSER="${MYSQLUSER:-$DEFAULT_MYSQLUSER}"
MYSQLPASS="${MYSQLPASS:-$DEFAULT_MYSQLPASS}"
backupdir="$storage/$target"
logfile="$backupdir/backup.log"
# check if storage is mounted
if ! mount | grep -q "$storage"; then
echo "Error: ${storage} not mounted. Exiting..."
exit 1
fi
checkDir()
{
if [ ! -d "$1" ]
then
mkdir -p "$1"
fi
}
checkDir $backupdir
exec > $logfile 2>&1
## check for var excludes file
if [ -f $excludesdir/$excvar ];
then
excv="$excludesdir/$excvar"
else
excv="/dev/null"
fi
## check for usr excludes file
if [ -f $excludesdir/$excusr ];
then
excu="$excludesdir/$excusr"
else
excu="/dev/null"
fi
## check for home excludes file
if [ -f $excludesdir/$exchome ];
then
exch="$excludesdir/$exchome"
else
exch="/dev/null"
fi
# Prints date of the format YYYY/MM/DD
year=$(date +%Y)
month=$(date +%m)
day=$(date +%d)
today="$year/$month/$day"
# check for backup media
if [ ! -d $backupdir ];
then
line1="NO BACKUP MEDIA LOCATED!"
line2="HALTING BACKUP!"
line3="Last Backup: $(echo $lastbackup)"
echo "NO BACKUP MEDIA LOCATED!\nHALTING BACKUP!"
echo -e "$line1\n$line2\n\n$line3\n\n$emailfooter"|mail -s "$HOSTNAME [BACKUP FAILED!] $target for $(date +%m-%d-%y)" $recipient
exit 0
else
cd $backupdir
fi
# check if doing backup over ssh or locally
if [[ "$local" == true ]]; then
echo "Running backup locally"
via=""
else
echo "Running backup over ssh"
via="-e 'ssh -o StrictHostKeyChecking=no' $target:"
# check for remote target
if ssh -o BatchMode=yes -o ConnectTimeout=5 "$target" true 2>/dev/null; then
echo "SSH Connection established to $target"
else
echo -e "Hostname $target not accessible!\nHALTING BACKUP!"
line1="Hostname $target not accessible!"
line2="HALTING BACKUP!"
line3="Last Backup: $(echo $lastbackup)"
echo -e "$line1\n$line2\n\n$line3\n\n\n$emailfooter"|mail -s "$HOSTNAME [BACKUP FAILED!] $target for $(date +%m-%d-%y)" $recipient
exit 0
fi
fi
# make backup directories.
current="$backupdir/current"
old="$backupdir/old"
now="$old/$today"
checkDir $current
checkDir $old
checkDir $now
# get list of home directories
if [[ "$local" == true ]]; then
homelist=$(ls /home)
else
homelist=$(ssh $target "ls /home/")
fi
# Ugly converting of transferred file sizes from human to machine then back again to get total output in the end
convert2machine () {
KILO=1000
MEGA=`echo "$KILO ^ 2" |bc`
GIGA=`echo "$KILO ^ 3" |bc`
declare -a values
n=1
for i in `grep "Total trans" $logfile|awk '{print $5}'|grep -v ^0|sed 's/,//g'`
do
if [ `echo $i | grep K` ]
then
num=`echo $i | sed -e "s/K//"`
values[$n]=$(echo "$num * $KILO" | bc)
(( n++ ))
elif [ `echo $i | grep M` ]
then
num=`echo $i | sed -e "s/M//"`
values[$n]=$(echo "$num * $MEGA" | bc)
(( n++ ))
elif [ `echo $i | grep G` ]
then
num=`echo $i | sed -e "s/G//"`
values[$n]=$(echo "$num * $GIGA" | bc)
(( n++ ))
else
values[$n]=$i
(( n++ ))
fi
done
arrn=1
sum=0
while [ $arrn -lt $n ]
do
sum=$(echo "$sum + ${values[$arrn]}" | bc)
(( arrn++ ))
done
}
convert2human () {
value=$(echo "$sum"|awk -F "." '{print $1}')
((kilo=value/1024))
((total=kilo/1024))
}
echo "**********************"
echo " Backup started at "
date
echo "**********************"
sdate=$(date)
## HOME
echo "== backing up /home =="
for i in $homelist
do
echo "==== backing up $i ===="
checkDir $current/home/$i
eval rsync -aphv --delete --stats --progress --exclude-from="$exch" $via/home/$i/ $current/home/$i
echo
done
eval rsync -dvp -delete --stats --progress $via/home/ $current/home/
## root
echo "== backing up /root/ =="
checkDir $current/root/
eval rsync -aphv --delete --stats --progress $via/root/ $current/root/
## etc
echo "== backing up /etc/ =="
checkDir $current/etc/
eval rsync -aphv --delete --stats --progress $via/etc/ $current/etc/
## var
echo "== backing up /var/ =="
checkDir $current/var/
eval rsync -aphv --delete --delete-excluded --progress --exclude-from="$excv" --stats $via/var/ $current/var/
## usr
echo "== backing up /usr/ =="
checkDir $current/usr/
eval rsync -aphv --delete --delete-excluded --progress --exclude-from="$excu" --stats $via/usr/ $current/usr/
## check for include file
if [ -f $includedir/backup_includes.$target ] ; then
echo "==== backing up includes ===="
eval rsync -aphv --delete --stats --progress --include-from="$includedir/backup_includes.$target" ${via}/ ${current}/
fi
backup_databases() {
local cmd_prefix=$1
echo "=== Backing up Databases... ==="
if [ "$ROOTMYSQL" = true ]; then
MYSQLAUTH=""
else
MYSQLAUTH="-u $MYSQLUSER ${MYSQLPASS:+--password=$MYSQLPASS}"
fi
if [[ -n "$DOCKERID" ]]; then
DBS=$($cmd_prefix "docker exec $DOCKERID sh -c \"mysql $MYSQLAUTH -Bse 'show databases;'\"")
else
DBS=$($cmd_prefix "mysql $MYSQLAUTH -Bse 'show databases;' 2>/dev/null")
fi
for m in $DBS; do
echo "backing up $m..."
if [[ -n "$DOCKERID" ]]; then
$cmd_prefix "docker exec $DOCKERID sh -c \"mysqldump --single-transaction $MYSQLAUTH $m\"" 2>/dev/null | gzip > "$now/dbs/$m.sql.gz"
else
$cmd_prefix "mysqldump --single-transaction $MYSQLAUTH $m" 2>/dev/null| gzip > "$now/dbs/$m.sql.gz"
fi
done
}
# Ensure directory exists
checkDir "$now/dbs"
# Run backup locally or via SSH
if [[ "$local" == true ]]; then
if command -v mysql &>/dev/null; then
backup_databases ""
else
echo "=== No Local Databases found ==="
fi
else
if [[ -n "$DOCKERID" ]] ; then
if ssh "$target" "docker exec $DOCKERID sh -c 'command -v mysql'" &>/dev/null; then
backup_databases "ssh $target"
else
echo "=== No Remote Databases found in docker container ==="
fi
elif ssh "$target" "command -v mysql" &>/dev/null; then
backup_databases "ssh $target"
else
echo "=== No Remote Databases found ==="
fi
fi
if [[ "$local" == true ]]; then
echo $(date +%y%m%d) > /root/.lastbackup
else
ssh $target "echo $(date +%y%m%d) > /root/.lastbackup"
fi
# Update the mtime to reflect the snapshot time
echo "Updating mtime to reflect the snapshot time..."
touch $backupdir/current
# Make hardlink copy
echo "Making hardlink copy $now/ ...."
cp -al $current/* $now
# Remove old backups
#The past 7 days
day1=$(date +%Y/%m/%d --date="1 days ago")
day2=$(date +%Y/%m/%d --date="2 days ago")
day3=$(date +%Y/%m/%d --date="3 days ago")
day4=$(date +%Y/%m/%d --date="4 days ago")
day5=$(date +%Y/%m/%d --date="5 days ago")
day6=$(date +%Y/%m/%d --date="6 days ago")
day7=$(date +%Y/%m/%d --date="7 days ago")
# past 4 Mondays
Monday1=$(date -d'monday-7 days' +%Y/%m/%d)
Monday2=$(date -d'monday-14 days' +%Y/%m/%d)
Monday3=$(date -d'monday-21 days' +%Y/%m/%d)
Monday4=$(date -d'monday-28 days' +%Y/%m/%d)
# past 4 Tuesdays
Tuesday1=$(date -d'tuesday-7 days' +%Y/%m/%d)
Tuesday2=$(date -d'tuesday-14 days' +%Y/%m/%d)
Tuesday3=$(date -d'tuesday-21 days' +%Y/%m/%d)
Tuesday4=$(date -d'tuesday-28 days' +%Y/%m/%d)
#
lastYear=$(date +"%Y" -d last-year)
last90days() {
for i in `seq 1 90`
do
echo -e "$(date +%Y/%m/%d --date="$i days ago")"
done
}
aYearAgo() {
for i in `seq 365 730`
do
echo -e "$(date +%Y/%m/%d --date="$i days ago")"
done
}
ifdirdel() {
if [ -d $1 ]
then
rm -rf $1
echo "deleted $1"
daycount=$(find ${old}/${i%/*} -maxdepth 1 -mindepth 1 -type d|wc -l)
if [ $daycount = 0 ] ; then
rmdir ${old}/${i%/*}
echo "deleted ${i%/*}"
fi
fi
}
cleanMonth() {
for i in $(last90days)
do
monthcount=$(ls $old/$(echo $i|awk -F "/" '{print $1"/"$2}')/ 2>/dev/null |wc -l)
if [ $i = "$day1" ] || [ $i = "$day2" ] || [ $i = "$day3" ] || [ $i = "$day4" ] || [ $i = "$day5" ] || [ $i = "$day6" ] || [ $i = "$day7" ]; then
continue
elif [[ $i == *01 ]] || [[ $i == *02 ]];
then
continue
elif [ $i = "$Monday1" ] || [ $i = "$Monday2" ] || [ $i = "$Monday3" ] || [ $i = "$Monday4" ];
then
continue
elif [ $i = "$Tuesday1" ] || [ $i = "$Tuesday2" ] || [ $i = "$Tuesday3" ] || [ $i = "$Tuesday4" ];
then
continue
elif [ $monthcount = 1 ];
then
continue
else
ifdirdel $old/$i
fi
done
}
cleanYear() {
for i in $(aYearAgo)
do
yearcount=$(find $old/${i%%/*} -maxdepth 2 -mindepth 2 -type d 2>/dev/null|wc -l)
if [ $i = "$lastYear/01/01" ] || [ $i = "$lastYear/01/02" ]; then
continue
elif [ $yearcount = 1 ]; then
continue
else
ifdirdel $old/$i
fi
done
}
cleanMonth
if [ -d $old/$lastYear ];
then
cleanYear
fi
echo "**********************"
echo " Backup ended at "
date
echo "**********************"
edate=$(date)
seconds=$SECONDS
hours=$((seconds / 3600))
seconds=$((seconds % 3600))
minutes=$((seconds / 60))
seconds=$((seconds % 60))
#get human readable total
convert2machine
convert2human
echo "$(echo $total)M backed up"
echo "Sending mail..."
line1=$(echo "$(echo $total)M Backed up")
line2=$(echo -e "Backup started:\t$sdate")
line3=$(echo -e "Backup ended:\t$edate")
line4=$(echo -e "Backup took: $hours hour(s) $minutes minute(s) $seconds second(s) to run")
line5=$(echo -e "Last Backup: $lastbackup")
line6=$(egrep "backing up|Number of files t|Total transferred file size" $logfile|grep -v -- ": 0")
line7=$(grep -e "deleted $old" $logfile)
echo -e "$line1\n\n$line2\n$line3\n$line4\n$line5\n\n$line6\n\n$line7\n\n$emailfooter"|mail -s "$HOSTNAME [BACKUP OK] $target for $(date +%m-%d-%y)" $recipient
echo -e "$edate $target $(echo $total)M $hours:$minutes:$seconds " >> $storage/backup_log
echo "done"
cp $logfile $now/