-
Notifications
You must be signed in to change notification settings - Fork 0
/
Job_Nightly_Backup.sh
265 lines (176 loc) · 6.23 KB
/
Job_Nightly_Backup.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
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
#!/bin/bash
# PATHS ------------------------------------------------------------------------------------
# DIRECTORY OF SOURCE (TO BE BACKED UP)
SURCE=/mnt/speakeasy/jobs
SURCEJOB=$SURCE/
#echo $SURCE
# DIRECTORY OF DESTINATION
DEST=/mnt/pbr/jobs
#DEST=/media/WAT_ARCH/backups
DESTJOB=$DEST/
#echo $DEST
# ------------------------------------------------------------------------------------------
# LOG DIRECTORY AND FILENAME
#LOGDIR=/mnt/speakeasy/library/logs/Oberon/PBR_Bkup_$TODAY.$DATE.log
LOGDIR=$SURCE/"PBR_Bkup_"$TODAY.$DATE".log"
#echo "LOGDIR:" $LOGDIR
EmailAddress="YourEmailAddress"
#echo ''
#echo ''
# JOB AND EXCLUSION FILES ------------------------------------------------------------------
rm $DEST/jobs.txt
cd $SURCE
ls --format=single-column > $DEST/jobs.txt
file=$DEST/jobs.txt
exclusion=$DEST/exclusion_list.txt # Use this to exclude folders from being bkup'd up
#echo $file
#echo $exclusion
# ------------------------------------------------------------------------------------------
#echo ''
#echo ''
# BUILD EXCLUSION ARRAY FROM "exclusion_list.txt" FILE (PRE-BUILT) ------------------
ExAry=()
#LOOP
while IFS= read -r line
do
#echo $line
ExAry=( "${ExAry[@]}" $line )
done < $exclusion
#echo ${ExAry[*]}
# ------------------------------------------------------------------------------------------
#echo ''
#echo ''
# BUILD JOB ARRAY FROM "job.txt" FILE (BUILT AUTOMATICALLY FROM "LS") ----------------------
JobAry=()
#LOOP
while IFS= read -r line
do
#echo $line
JobAry=( "${JobAry[@]}" $line )
done < $file
#echo ${JobAry[*]}
# ------------------------------------------------------------------------------------------
#echo ''
#echo ''
# BUILD JOB BACK UP LIST FROM JOBS AND EXCLUSIONS ----------------------------------------
a=0
aT=${#JobAry[*]} #LENGTH OF ARRAY
while (($a <= $aT))
do
JobAry=( ${JobAry[@]#${ExAry[$a]}} )
#echo $a
a=$(( a+1 ))
done
#echo "Job Backup List:" ${JobAry[*]}
# ------------------------------------------------------------------------------------------
#echo ''
#echo ''
# VARIABLES --------------------------------------------------------------------------------
Grab_Date() {
# DECLARES NUMERICAL VALUE FOR THE DAY OF THE WEEK.
# NOTE: BE SURE TO OFFSET FOR THE ARRAY VALUES
# STARTS AT '1' = 'MONDAY'
NUMDAYOFWEEK=`date +%u`
#echo "NUMDAYOFWEEK:" $NUMDAYOFWEEK
# DECLARES DAYS OF THE WEEK ARRAY
Days=(Mon Tue Wed Thu Fri Sat Sun)
# DECALRES WHAT DAY OF THE WEEK YESTERDAY WOULD BE
# DEFAULT OFFSET = -2
if [ "$NUMDAYOFWEEK" == "1" ]; then
YESTERDAY=${Days[$NUMDAYOFWEEK+5]}
#echo "A YESTERDAY:" $YESTERDAY
else
YESTERDAY=${Days[$NUMDAYOFWEEK-2]}
#echo "B YESTERDAY:" $YESTERDAY
fi
# DECALRES WHAT DAY OF THE WEEK TODAY WOULD BE
# DEFAULT OFFSET = -1
TODAY=${Days[$NUMDAYOFWEEK-1]}
#echo "TODAY:" $TODAY
# DECALRES DATE USING TWO DIGITAL REPRESENTATION (e.g 10.14.09)
DATE=`date +%m.%d.%y`
#echo "TODAY:" $TODAY
TIME=`date +%T`
#echo "TIME:" $TIME
}
Grab_Date
# ------------------------------------------------------------------------------------------
echo " " >> $LOGDIR
echo "--------------------------------------------------------------------------------------------------" >> $LOGDIR
echo "--------------------------------- Nightly Back up Initialization ---------------------------------" >> $LOGDIR
echo "--------------------------------------------------------------------------------------------------" >> $LOGDIR
echo " " >> $LOGDIR
echo "Start Date: $TODAY $DATE" >> $LOGDIR
echo "Start Time: $TIME" >> $LOGDIR
echo " " >> $LOGDIR
echo "Log Name: $LOGDIR" >> $LOGDIR
echo " " >> $LOGDIR
echo "Jobs to be backed up:" >> $LOGDIR
for pj in ${JobAry[*]}
do
echo " " $pj >> $LOGDIR
done
# --------
echo " " >> $LOGDIR
# CP SCRIPT --------------------------------------------------------------------------------
COMMANDS=-fpruv
#COMMANDS=-dpRuv
#cp $COMMANDS $SURCEJOB $DESTJOB >> $LOGDIR
for j in ${JobAry[*]}
do
#echo $j
SURCEJOB=$SURCE/$j/*
#echo $SURCEJOB
DESTJOB=$DEST/$j/
#echo $DESTJOBecho "Start Date: $TODAY $DATE" >> $LOGDIR
if [ ! -d $DESTJOB ]; then
echo $DESTJOB "doesn't exist"
mkdir $DESTJOB
fi
echo "SNAPSHOT DATE & TIME:" $DATE $TIME >> $LOGDIR
cp $COMMANDS $SURCEJOB $DESTJOB >> $LOGDIR
#echo $COMMANDS $SURCEJOB $DESTJOB
done
# ------------------------------------------------------------------------------------------
Grab_Date
echo " " >> $LOGDIR
echo "--------------------------------------------------------------------------------------------------" >> $LOGDIR
echo "--------------------------------- Nightly Back up Complete! ---------------------------------" >> $LOGDIR
echo " " >> $LOGDIR
echo "End Date: $TODAY $DATE" >> $LOGDIR
echo "End Time: $TIME" >> $LOGDIR
echo "--------------------------------------------------------------------------------------------------" >> $LOGDIR
echo " " >> $LOGDIR
# EMAIL ------------------------------------------------------------------------------------
# email subject
SUBJECT="Nightly Back Up"
# Email To ?
EMAIL=$EmailAddress
# Email text/message
EMAILMESSAGE="/tmp/emailmessage.txt"
echo -e "NIGHTLY BACK UP REPORT:" > $EMAILMESSAGE
echo -e "DATE:" $DATE >> $EMAILMESSAGE
echo -e "TIME:" $TIME >> $EMAILMESSAGE
echo -e "------------------------------------------------\n" >> $EMAILMESSAGE
echo "CURRENT JOBS BACKED UP: " >> $EMAILMESSAGE
a=0
aT=${#JobAry[*]} #LENGTH OF ARRAY
while (($a <= $aT))
do
echo ${JobAry[$a]} >> $EMAILMESSAGE
a=$(( a+1 ))
done
echo -e " " >> $EMAILMESSAGE
echo "LOG SIZE & PATH: " >> $EMAILMESSAGE
ls -sh $LOGDIR >> $EMAILMESSAGE
echo -e " " >> $EMAILM
# send an email using /bin/mail
cat $0 | mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE
exit 0
# ------------------------------------------------------------------------------------------
echo " " >> $LOGDIR
echo "--- Email Sent ---" >> $LOGDIR
echo " " >> $LOGDIRESSAGEls -1 | grep -v "^.kde$" | xargs -n 1 -iHERE cp -R HERE <destination
echo -e "DISK SPACE:" >> $EMAILMESSAGE
df -Ph $DEST >> $EMAILMESSAGE
df -Ph $SURCE >> $EMAILMESSAGE