-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackupToBoxNet.sh
114 lines (94 loc) · 3.34 KB
/
backupToBoxNet.sh
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
#!/bin/sh
# JACK DWYER
# 06 October 2011
#-------------------
# Backup Script
# Uses a webdav mounted box.net directory
# Can use any mounted path though, just comment out the first check
# and rsync to backup specified files
#-------------------
#first check webdav mount of my box.net is up
grep -q box.net /etc/mtab
if [ $? -eq 1 ]; then
mount ~/box.net
fi
#SOME GLOBAL VARIABLES - THESE NEED TO BE SETUP
#-------------------
#This is path to the log rsynclog file
rs="/home/jack/logs/rSyncLog.log"
#Creates the log file, for rsync to dump its shit in.
touch $rs
#SOURCE = the acutal location of backup directory
#DESTINATION = save location for the corresponding source
#source must match its destination
source=("/home/jack/my/shit/1" "/all/the/shit/2/")
destination=("/home/jack/box.net/myshitBACKUP1" "/home/jack/boxnet/ALLTHESHIT_BACKUP2")
#Validate both source and destination array are matching length
if [ ${#source[@]} != ${#destination[@]} ]; then
#TODO EMAIL error the backup can not be ran.
echo "SOURCE AND DESTINATION ARRAYS DO NOT MATCH, backup can not be ran"
exit 1
fi
#mysql setup values
password=a
user=root
mysqlSource=("/home/jack/Desktop/WORKING.sql") #this is actually where the dump will be stored, good variable name
mysqlDestination=("/home/jack/box.net/mysqlbackup")
#Validate sqlSource and sqlDestination are matching length
if [ ${#sqlSource[@]} != ${#sqlDestination[@]} ]; then
#TODO append if an error to the error email
echo "SQL BACKUP ARRAYS DO NOT MATCH, backup can not run"
exit 1
fi
#Length of each array (sql, directories)
dirLen=${#source[@]}
mysqlLen=${#mysqlSource[@]}
errors=0
#TODO check log files exist, if not create them. As script will fail if they are not floating around
#-------------------
#Setup Up EMAIL MESSAGE - ONLY SENT IF THERE IS AN ERROR
echo "To: [email protected]" > $rs
echo "From: [email protected]" >> $rs
echo "Subject: BACKUP ERROR" >> $rs
echo "" >> $rs
echo "--------------------------------------------------------------------------------------------------------------------" >> $rs
echo "" >> $rs
#Generate a rsync commands
for (( i=0; i<=$(( $dirLen - 1)); i++ ))
do
rSyncArray[$i]="rsync -a --log-file=${rs} ${source[$i]} ${destination[$i]}"
done
#Generate mysqldump and rsnyc commands only if required
if [ $mysqlLen != 0 ]; then
#Generate mysqldump commands, from the array of databases needed to be backed up
for (( i=0; i<=$(( $mysqlLen - 1)); i++ ))
do
#Sort of point less loop as its just doing a total dump, but could change so it specifed databases
mysqlDump[$i]="mysqldump -u ${user} -p${password} --all-databases"
done
#run mysqldump with the specified matching location to save dump (mysqlSource)
dumpLen=${#mysqlDump[@]}
for (( i=0; i<=$(( $dumpLen - 1)); i++ ))
do
${mysqlDump[$i]} > ${mysqlSource[$i]}
done
#Creates new rsync commands, and adds them to the previous rsync command array
for (( i=0; i<=$(( $dumpLen -1)); i++ ))
do
dumprSync="rsync -a --log-file=${rs} ${mysqlSource[$i]} ${mysqlDestination[$i]}"
rSyncArray=("${rSyncArray[@]}" "${dumprSync}")
done
fi
#Actually do the backups - EG: run the rsync commands
rSyncArrayLen=${#rSyncArray[@]}
for (( i=0; i<=$(( $rSyncArrayLen - 1)); i++ ))
do
${rSyncArray[$i]}
if [ $? -gt 0 ]; then
errors=1
fi
done
#Shoot off email to me, if the shit fucks up
if [ $errors -gt 0 ]; then
msmtp -t < ${rs}
fi