#!/bin/bash # # author: flo # Working on a new version which would compare files and directories paths with spaces # This current version doesnt support file and directory with space in them. # # A bash script to dummify the copying process for users. # # Example: # To copy the folder into the destination folder do the following # Case 1: File only # /var/tmp/foobar/cheesecake.txt /var/tmp/test # Special Case 2: Dir only (recursive) # A case where the destination forward slash is neccessary which is # congenial to the way of the BASH # /var/tmp/foobar /var/tmp/test/ # Case 3: Wild Card Case '*.jpg' kind of thing # /var/tmp/foobar/*.jpg /var/tmp/test # # I cant believed that bash doesnt like spaces, when assigning a value to a variable. # # KCCO srcPath='' dstPath='' result='n' chkMD5Sum1() { if [[ "$1" == "$2" ]]; then echo -e "\n" echo -e "\t MD5 Sum came back normal from Source to Destination." echo -e "\t Continuing onward to dump the md5sum to file." else rm -f $3 echo -e "\n" echo -e "\t MD5 Checksum from SOURCE to DESTINATION doesnt match." echo -e "\t Deleted Destination file." echo -e "\t Please run this script again." exit 1 fi } chkMD5Sum2() { if [[ "$1" == "$2" ]]; then echo -e "\n" echo -e "\t MD5 Sum came back normal from Source to Destination." echo -e "\t Continuing onward to dump the md5sum to file." else rm -fr $3 echo -e "\n" echo -e "\t MD5 Checksum from SOURCE to DESTINATION doesnt match." echo -e "\t Deleted Destination file." echo -e "\t Please run this script again." exit 1 fi } chkMD5Sum3() { if [[ "$1" == "$2" ]]; then echo -e "\n" echo -e "\t MD5 Sum came back normal from Source to Destination." echo -e "\t Continuing onward to dump the md5sum to file." else rm -f $3 echo -e "\n" echo -e "\t MD5 Checksum from SOURCE to DESTINATION doesnt match." echo -e "\t Deleted Destination file." echo -e "\t Please run this script again." exit 1 fi } prtMD5SumMessage() { sleep 2 echo -e "\n" echo -e "\t-----------------------------------------------------------------------" echo -e "\t We will be generating md5sum(s) for our file(s)." echo -e "\t-----------------------------------------------------------------------" sleep 2 } prtEndProgram() { sleep 1 echo -e "\tFinished generating MD5 values." echo -e "\n\n" } echo -e "\n" # Interactive message echo -e "\t-----------------------------------------------------------------------" echo -e "\tWelcome to this interactive bash script file to CIS backup application." echo -e "\t-----------------------------------------------------------------------" sleep 1 echo -e "\tAdjust this path to show the directory . " echo read -e -p " Enter the Source (From) location, then [ENTER]: " -i "/var/tmp" srcPath read -e -p "Enter the Destination (To) location, then [ENTER]: " -i "/var/tmp" dstPath echo -e "\n" # Final Question read -p "Are you sure? [y|n]: " -n 1 result echo -e "\n" #echo -e "\n" if [[ $result =~ ^[Yy]$ ]]; then timestamp=`date +\%Y\%m\%d\%H\%M\%S` # Check to see if either one is empty if [[ ! -n "$srcPath" || ! -n "$dstPath" ]]; then echo -e "\tThere is no path in one of the fields." exit 1 fi # Check to see if both path are empty if [[ ! -n "$srcPath" && ! -n "$dstPath" ]]; then echo -e "\tThere is no path in both of the fields." exit 1 fi if [[ -f "$srcPath" ]]; then # Case 1 # single File ONLY cp -pv $srcPath $dstPath # MD5sum checking before dump procedure dirBasePath=${srcPath%/*} dirFileName=${srcPath##*/} cd $dirBasePath fileMD5Src=`md5sum $dirFileName` cd $dstPath fileMD5Dst=`md5sum $dirFileName` chkMD5Sum1 "$fileMD5Src" "$fileMD5Dst" "$dirFileName" ################################################### echo -e "\n" echo -e "\t File copying has been completed." prtMD5SumMessage fileList=`find $dirFileName -maxdepth 1 -not -type d` cd $dstPath for f in $fileList; do md5sum $f >> ./chkSum_$timestamp.md5; done prtEndProgram elif [[ -d "$srcPath" && -d "$dstPath" ]]; then # Case 2 # Directory ONLY cp -prv $srcPath $dstPath # MD5Sum checking before dump procedure a=${srcPath##*/} # FROM location cd $srcPath lFilesSrc=`find . -type f | sort -V` chkSumSrc=`for f in $lFilesSrc; do md5sum $f; done` # TO location # This is special since the destination will need a # slash initially. cd $dstPath$a cbo=$dstPath$a lFilesDst=`find . -type f | sort -V` chkSumDst=`for f in $lFilesDst; do md5sum $f; done` chkMD5Sum2 "$chkSumSrc" "$chkSumDst" "$cbo" ################################################### echo -e "\n" echo -e "\t File copying has been completed." prtMD5SumMessage fileList=`find $dstPath -type f | sort -V` cd $dstPath for f in $fileList; do md5sum $f >> ./chkSum_$timestamp.md5; done prtEndProgram else # Case 3 # Wild Card case EX: *.html cp -pv $srcPath $dstPath # MD5sum checking before dump procedure dirBasePath=${srcPath%/*} dirFileName=${srcPath##*/} #dirFileExt=${dirFileName##*.} cd $dirBasePath # From location lFilesSrc=`find $dirFileName -maxdepth 1 -not -type d | sort -V` chkSumSrc=`for f in $lFilesSrc; do md5sum $f; done` # To location cd $dstPath lFilesDst=`find $dirFileName -maxdepth 1 -not -type d | sort -V` chkSumDst=`for f in $lFilesDst; do md5sum $f; done` chkMD5Sum3 "$chkSumSrc" "$chkSumDst" "$dirFileName" ############################################################### # this part is a bit tricky since there might be files # in ths dest folder # my code will generate mdsum on this existing folder, # this means that # the preexisiting files will also get to do the # md5sum dance echo -e "\n" echo -e "\t File copying has been completed." prtMD5SumMessage fileList=`find $dstPath -maxdepth 1 -not -type d | sort -V` #cd $dstPath for f in $fileList; do md5sum $f >> ./chkSum_$timestamp.md5; done prtEndProgram fi else echo -e "\t Exiting due to no selecting ." exit 1 fi exit 0