Operating System‎ > ‎Linux‎ > ‎Linux Shell Script‎ > ‎

Lab 5-6 Write a script to create directory backup


Lab 5-6 Shell Script Requirement

Lab5-6 Script Requirement


Lab 5-6 Shell Script Solution

Module 5: Write a script that creates a backup for a directory

#  Write a script that creates a backup for a directory
#!/bin/bash
# Name: ABC, Lab5
# Module 5

# Needed Variable
argCount=0
invalidOpt=0
optionA=0
optionV=0
srcDir=""
destDir=""

# if no argument/option
if (( $# == 0 ))
then
echo "No argument - Usage: ./lab5 [-av] sourceDir [destinationDir]"
exit 1
fi

# Go through each argument/option
for optArg in "$@"
do
if [ ${optArg:0:1} == "-" ] # options
then
# echo "option: "$optArg
optstr=$optArg
for (( indexoptstr = 1; indexoptstr < ${#optstr}; ++indexoptstr)); 
do
charOpt=${optstr:$indexoptstr:1}
# echo $charOpt
if [ $charOpt == "a" ]
then
optionA=1
elif [ $charOpt == "v" ]
then
optionV=1
else
echo "Invalid Option - Usage: ./lab5 [-av] sourceDir [destinationDir]"
exit 1
fi
done
else # argument
(( argCount = $argCount+1 ))
if (( $argCount > 2 ))
then
echo "Too Many Arguments - Usage: ./lab5 [-av] sourceDir [destinationDir]"
exit 1
else
if [ -z $srcDir ]
then
if [ ! -e $optArg ] # if source directory is not exist
then
echo "Source directory not exist"
exit 2
elif [ "$(ls -A $srcDir 2> /dev/null)" == "" ]; # if source directory directory is empty
then
echo "Source directory is empty"
exit 2
else
srcDir=$optArg
fi
else # destination directory
if [ ! -e $optArg ] # if destination directory is not exist
then
echo "Destination directory not exist"
exit 2
else
destDir=$optArg
fi
fi
fi
fi
#echo "Option & Argument: "$optArg
done

if (( argCount == 0 )) # source directory is not passed as an argument
then
echo "Source directory must be necessary - Usage: ./lab5 [-av] sourceDir [destinationDir]"
exit 1
fi

if [ "$(ls -A $srcDir 2> /dev/null)" == "" ]; # if source directory directory is empty
then
echo "Source directory is empty"
exit 2
fi

# backup if no error
timeStamp=$(date +%Y-%m-%d-%H%M)
if (( $argCount == 1 ))
then
# echo "argument count 1"
if (( optionA == 1 ))
then
archiveFileName=$srcDir$timeStamp.tgz
# echo $archiveFileName
if (( optionV == 1 )) # verbose - print message on screen
then
tar czvf $archiveFileName $srcDir
echo "created " $archiveFileName "file in the current directory"
else
tar czf $archiveFileName $srcDir
echo "created " $archiveFileName "file in the current directory"
fi
else # Copy files to current directory folder
backupFolderName=$srcDir$timeStamp
# echo $backupFolderName
mkdir $backupFolderName
if (( optionV == 1 )) # verbose - print message on screen
then
for file in $srcDir/*
do
if  [ -f  $file ] # Regular file only
then
cp $file $backupFolderName
echo "copying " $file
fi
done
else
for file in $srcDir/*
do
if  [ -f  $file ] # Regular file only
then
cp $file $backupFolderName
fi
done
fi
fi
else # here argument count must be 2
# echo "argument count 2"
if (( optionA == 1 ))
then
archiveFileName=$srcDir$timeStamp.tgz
# echo $archiveFileName
if (( optionV == 1 )) # verbose - print message on screen
then
tar czvf $archiveFileName $srcDir
mv $archiveFileName $destDir
echo "created " $archiveFileName "file in the destination directory"
else
tar czf $archiveFileName $srcDir
mv $archiveFileName $destDir
echo "created " $archiveFileName "file in the destination directory"
fi
else # Copy files to destination directory folder
backupFolderName=$destDir/$srcDir$timeStamp
# echo $backupFolderName
mkdir $backupFolderName
if (( optionV == 1 )) # verbose - print message on screen
then
for file in $srcDir/*
do
if  [ -f  $file ] # Regular file only
then
cp $file $backupFolderName
echo "copying " $file
fi
done
else
for file in $srcDir/*
do
if  [ -f  $file ] # Regular file only
then
cp $file $backupFolderName
fi
done
fi
fi
fi

exit 0 # exit code 0: no error




Module 6: Write a test script that runs tests of your module 5 script and prints a summary of the test result

# Write a script that runs tests of your Module 5 script and prints a summary of the test result
#  [Note: QA engineers often have to write test scripts to run test cases repeatedly. This exercise illustrates that a QA person who can write scripts is to be respected!] #!/bin/bash
# Name: ABC, Lab6
# Module 6

# needed variable
indexNoError=0
indexUsageError=0
indexInvalidArg=0

# if incorrect argument
if (( $# != 1 ))
then
echo "Incorrect argument - Usage: ./lab6 testfile"
exit 10 # assumption since not given in lab requirement
fi

testFileName=$1
#echo $testFileName

if [ -e testFileName ]
then
echo "Test file is not exist"
exit 20 # assumption since not given in lab requirement
else
while read line # go through ealine of the file
do
#fileLine+=("$line")
#echo $line
# Execute script, /dev/null is a logical file (also known humorously as the ‘bit bucket’) that accepts all output that should be discarded.
cmdExecute="./lab5 "$line
#echo $cmdExecute
$cmdExecute  > /dev/null

#echo "Error Code: " $? # it will also change error code to 0 in shell variable
errorCode=$? # copy to local variable since each statement execution will change the shell variable value

if (( errorCode == 0 ))
then
arrNoError[indexNoError]='lab5 '$line
#echo $arrNoError[indexNoError]
(( indexNoError = indexNoError+1 ))
elif (( errorCode == 1 ))
then
arrUsageError[indexUsageError]='lab5 '$line
#echo $arrUsageError[indexUsageError]
(( indexUsageError = indexUsageError+1 ))
elif (( errorCode == 2 ))
then
#echo $arrInvalidArg[indexInvalidArg]
arrInvalidArg[indexInvalidArg]='lab5 '$line
(( indexInvalidArg = indexInvalidArg+1 ))
fi
done < $testFileName

# print result
IFS='' # to store space in array
echo ${#arrNoError[@]} " cases with no error"
for  lineNoError in ${arrNoError[@]}
do
echo $lineNoError
done

echo ${#arrUsageError[@]} " cases with usage error"
for  lineUsageError in ${arrUsageError[@]}
do
echo $lineUsageError
done

echo ${#arrInvalidArg[@]} " cases with invalid argument"
for  lineInvalidArg in ${arrInvalidArg[@]}
do
echo $lineInvalidArg
done
fi




Comments