-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate.sh
More file actions
executable file
·430 lines (400 loc) · 14.8 KB
/
evaluate.sh
File metadata and controls
executable file
·430 lines (400 loc) · 14.8 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
#!/bin/bash
# Declare variable
DIR="testing_backup"
backupFilename="backup.sh"
evaluationLogFilename="logfile_evaluate.txt"
backupEvaluateLogFilename="logfile_backup_evaluate.txt"
config_file_name="testing"
exist_config_file_name="testing"
source="../Documents"
dest="testing_backup/testing_noErrorPredefine"
function datetime() {
echo `date +"%d-%m-%Y_%H:%M:%S:%N"`
}
# Only main function will be call first then only other function will be called. Main function is like the boss, allocate tasks to others
function main() {
while :
do
welcome
initialSetup
menu
read -p "Do you want to continue ('y' for Yes // 'n' for No) : " sessionyn
if [ "$?" != "0" ]; then
printf "\n[Error]!!!\n\n" 1>&2
exit 1
else
if [ $sessionyn = 'n' ]; then
end
thanks
break
elif [ $sessionyn = 'y' ]; then
echo ""
else
printf "Wrong input. Please enter the correct value\n"
fi
fi
done
}
function welcome() {
echo "
~~~ This is a Script that Help you To Check OR Evaluate your Backup your Files ~~~
"
}
function initialSetup() {
if [ ! -d "$DIR" ]; then
mkdir $DIR
fi
}
# Prompt Menu to user
function menu() {
while :
do
echo "
'1' - Pre-configured Test
'2' - Manual Test
'3' - Test based on logfile of '${backupEvaluateLogFilename}' (Must the backup script at least once)
'4' - Summary of number of File(s) // Folder(s)
'5' - View logfile of this test
'6' - Delete Old // Previous '${DIR}' Folder & config file '$config_file.txt' produced by this script
"
read -p "What scenario number you would like to test (From above):" menu
if [ "$?" != "0" ]; then
printf "\n[Error]!!!\n\n" 1>&2
exit 1
else
case $menu in
"1")
predefineCase
break
;;
"2" )
printf "\n\n\n\n\nThis backup script is executed though evaluate script\nSo remember for your backup destination will be at '${DIR}'
and your config file name will be '${config_file_name}'\n\n"
./$backupFilename
break
;;
"3" )
testBackupEvaluateLogfile
break
;;
"4" )
countFile
break
;;
"5" )
viewLog
break
;;
"6" )
rm -rf $DIR
rm -rf asdfad
rm -rf n
cd backup_config_file
if [ -f "$config_file_name.txt" ]; then
rm $config_file_name.txt
fi
cd ../
echo "
Folder '${DIR}' Deleted
"
thanks
break
;;
*)
printf "Wrong input. Please enter the correct value.\n"
;;
esac
fi
done
createLog
}
function predefineCase() {
while :
do
echo "
'1' - Predefine setting without error, no config file existed on the destination (Creation of backup file)
'2' - MUST RUN #1 first ~ Predefine setting without error, existed config file on the destination (Automatically run for 2 Times)
'3' - Error issue, wrong input when prompt user on session or program continuity
'4' - Error issue, file existed but still create backup on the same destination
'5' - Error issue, invalid input when prompt yes or no on do you have a default config file
"
read -p "What scenario number you would like to test (From above):" test
if [ "$?" != "0" ]; then
printf "\n[Error]!!!\n\n" 1>&2
exit 1
else
case $test in
"1")
nPredefineNoError
break
;;
"2" )
yPredefineNoError
break
;;
"3" )
nPredefineError
break
;;
"4" )
yPredefineError
break
;;
"5" )
invalid
break
;;
*)
printf "Wrong input. Please enter the correct value.\n"
;;
esac
fi
done
while :
do
read -p "Do you want to view the log or summary ('y' for Yes // 'n' for No) : " viewLogyn
if [ "$?" != "0" ]; then
printf "\n[Error]!!!\n\n" 1>&2
exit 1
else
if [ $viewLogyn = 'y' ]; then
printLog
break
elif [ $viewLogyn = 'n' ]; then
break
else
printf "Wrong input. Please enter the correct value.\n"
fi
fi
done
}
# '1' - Predefine setting without error, no config file existed on the destination (Creation of backup file)
function nPredefineNoError() {
yn="n"
session="n"
echo -e "$yn\n$config_file_name\n$source\n$dest\n$session" | bash ./$backupFilename
echo ""
echo "All your evaluated backup file included logfile is allocated at '${DIR}' except for the config file."
echo "Your evaluated folder name is '$(basename $dest)' which is destination is inside '${DIR}'"
echo "Your source is from '$(basename $source)'"
echo "Your config file name is at '${config_file_name}.txt' at 'backup_config_file'"
echo "Your logfile name is '${evaluationLogFilename}' at '${DIR}'"
echo ""
}
# '2' - MUST RUN #1 first ~ Predefine setting without error, existed config file on the destination (2 Times)
# It will create 2 folders with the same file(s) inside 2 different folder
# It depends on the source and destination store on the configuration file which is ‘testing.txt’
function yPredefineNoError() {
yn="y"
session="n"
echo -e "$yn\n$exist_config_file_name\n$session" | bash ./$backupFilename
echo -e "$yn\n$exist_config_file_name\n$session" | bash ./$backupFilename
echo ""
echo "All your evaluated backup file included logfile is allocated at '${DIR}' except for the config file."
echo "Your evaluated folder name is '$(basename $dest)' which is destination is inside '${DIR}'"
echo "Your source is from '$(basename $source)'"
echo "Your config file name is at '${exist_config_file_name}.txt' at 'backup_config_file'"
echo "Your logfile name is '${evaluationLogFilename}' at '${DIR}'"
echo ""
}
# '3' - Error issue, wrong input when prompt user on session continuity
# Depending on any file(s) or folder(s) existent
# If file(s) or folder(s) existed, then it will create a folder called "asdfad" outside the "testing_backup folder"
# If file(s) or folder(s) not exist, then it will backed up successfully but it prompt error
function nPredefineError() {
yn="n"
session="asdfad"
echo -e "$yn\n$config_file_name\n$source\n$dest\n$session" | bash ./$backupFilename
}
# '4' - Error issue, file existed but still create backup on the same destination
# Unable to create a backup
# Error pop up
# It depends on the source and destination store on the configuration file which is ‘testing.txt’
function yPredefineError() {
yn="y"
exist_config_file_name="testing_backup/testing_folder"
session="n"
echo -e "$yn\n$exist_config_file_name\n$session" | bash ./$backupFilename
}
# '5' - Error issue, invalid input when prompt yes or no on do you have a default config file
# It directly pop error because of invalid input
function invalid() {
yn="asfad"
echo -e "$yn" | bash ./$backupFilename
}
# Called by menu function to test on the logfile produced by backup script
function testBackupEvaluateLogfile() {
while read co da s de deTime; do
counter=$co logfileDateTime=$da sourceFortestBackupEvaluateLogfile=$s destFortestBackupEvaluateLogfile=$de destTimestamp=$deTime
done < $backupEvaluateLogFilename
countFileFortestBackupEvaluateLogfile
}
# Count number of file(s) // folder(s) in summary
function countFileFortestBackupEvaluateLogfile() {
printf "Total number of file(s) in Source directory '${sourceFortestBackupEvaluateLogfile}'\n"
countSourceFileFortestBackupEvaluateLogfile
printf "\nTotal number of file(s) in most recent backup Destination in '${destTimestamp}' under the directory of '${destFortestBackupEvaluateLogfile} folder'\n"
countDestFileFortestBackupEvaluateLogfile
printf "\nTotal number of file(s) in each Destination folder under the directory of '${destFortestBackupEvaluateLogfile}'\n"
countDestFileOnDirFortestBackupEvaluateLogfile
printf "You have perform $(countDirFortestBackupEvaluateLogfile) of backup and there is total of $(countDirFortestBackupEvaluateLogfile) folder(s) in the directory of '${destFortestBackupEvaluateLogfile}'\n"
if [ $(countSourceFileFortestBackupEvaluateLogfile) = $(countDestFileFortestBackupEvaluateLogfile) ]; then
printf "\nTotal number of file(s) matched which is $(countSourceFileFortestBackupEvaluateLogfile) with the source and destination\n"
fi
printf "In total you have perform $counter of backup\n"
echo ""
printf "===================================================================================================\n"
printf "===================================================================================================\n\n"
}
# Count number of file in source directory
function countSourceFileFortestBackupEvaluateLogfile() {
find $sourceFortestBackupEvaluateLogfile -type f | wc -l
}
# Count number of file in destination
function countDestFileFortestBackupEvaluateLogfile() {
find $destFortestBackupEvaluateLogfile/$destTimestamp -type f | wc -l
}
# Count number of file inside each folder of testing_backup
function countDestFileOnDirFortestBackupEvaluateLogfile() {
for entry in "$destFortestBackupEvaluateLogfile"/*
do
echo $(basename $entry)
find $entry -type f | wc -l
echo ""
done
}
# Count number of folder in the testing_backup folder
function countDirFortestBackupEvaluateLogfile() {
ls -A $destFortestBackupEvaluateLogfile | wc -l
}
# Creation of logfile
function createLog() {
if [ -d "$dest" ]; then
if [ $? -eq 0 ]; then
printf "$(datetime) - Successfully run evaluation script on ${backupFilename}\n" >> $DIR/$evaluationLogFilename
printf "Summary of Number of File(s) or Folder(s)\n" >> $DIR/$evaluationLogFilename
printf "$(countFile)\n\n" >> $DIR/$evaluationLogFilename
else
printf "$(datetime) - [ERROR] Runnung evaluation script on ${backupFilename}\n\n" >> $DIR/$evaluationLogFilename
fi
fi
}
# Printing of log
function printLog() {
if [ -d "$dest" ]; then
if [ $? -eq 0 ]; then
printf "$(datetime) - Successfully run evaluation script on ${backupFilename}\n"
printf "Summary of Number of File(s) or Folder(s)\n"
countFile
else
printf "$(datetime) - [ERROR] Runnung evaluation script on ${backupFilename}\n"
fi
fi
}
# View logfile
function viewLog() {
while :
do
if [ ! -f "$DIR/$evaluationLogFilename" ]; then
printf "\nThere is no $evaluationLogFilename file exist.\nPlease run the Pre-Configuration Test or Manual Test first.\n\n"
main
fi
read -p "Do you have Visual Studio Code ('y' for Yes // 'n' for No):" programA
if [ "$?" != "0" ]; then
printf "\n[Error]!!!\n\n" 1>&2
exit 1
else
case $programA in
"n")
cat $DIR/$evaluationLogFilename
break
;;
"y" )
code $DIR/$evaluationLogFilename
break
;;
*)
printf "Wrong input. Please enter the correct value.\n"
;;
esac
fi
done
}
# Count number of file(s) // folder(s) in summary
function countFile() {
printf "Total number of file(s) in Source directory '${source}'\n"
countSourceFile
printf "\nTotal number of file(s) in most recent backup Destination in '$(getLatestDestFolder)' under the directory of '${dest}'\n"
countDestFile
printf "\nTotal number of file(s) in each Destination folder under the directory of '${dest}'\n"
countDestFileOnDir
printf "You have perform $(countDir) evaluation and there is total of $(countDir) folder(s) in the directory of '${dest}'\n"
if [ $(countSourceFile) = $(countDestFile) ]; then
printf "\nTotal number of file(s) matched which is $(countSourceFile) with the source and destination\n"
fi
echo ""
printf "===================================================================================================\n"
printf "===================================================================================================\n\n"
}
# Count number of file in source directory
function countSourceFile() {
find $source -type f | wc -l
}
# Count number of file in destination
function countDestFile() {
vargetLatestDestFolder=$(getLatestDestFolder)
find $dest/$vargetLatestDestFolder/$getLatestDestFolder -type f | wc -l
}
# Count number of file inside each folder of testing_backup
function countDestFileOnDir() {
for entry in "$dest"/*
do
echo $(basename $entry)
find $entry -type f | wc -l
echo ""
done
}
# Get the folder name of the most recent created
function getLatestDestFolder() {
varGetLatestDestFolder=$(ls -dt1 "$dest"/*/ | head -n1)
echo $(basename $varGetLatestDestFolder)
}
# Count number of folder in the testing_backup folder
function countDir() {
ls $dest -A | wc -l
}
# Prompting user on deletion of folder 'testing_backup'
function end() {
while :
do
read -p "Do you want to delete this '${DIR}' directory ('y' for Yes // 'n' for No) : " delDir
if [ "$?" != "0" ]; then
printf "\n[Error]!!!\n\n" 1>&2
exit 1
else
if [ $delDir = "y" ]; then
rm -rf $DIR
rm -rf asdfad
rm -rf n
cd backup_config_file
if [ -f "$config_file_name.txt" ]; then
rm $config_file_name.txt
fi
cd ../
echo "
Folder '${DIR}' Deleted
"
break
elif [ $delDir = 'n' ]; then
break
else
printf "Wrong input. Please enter the correct value.\n"
fi
fi
done
}
function thanks() {
echo '~~~ Thank You for using me ~~~'
}
# Execution of program
main