#!/bin/bash # This script will copy files from one directory to the next. # It will also check the md5sum of the files and dump it in the # same location for validation and veriflycation. # This version will work with spacing in dirs and filenames. # Tried to hack this in a pythonic way, but it just simply # doesnt want to budge. There is ONLY one way in Bash. # The way of the BASHIDO ! # Example: # To copy the folder into the destination folder do the following # Case 1: File only # /var/tmp/foobar/cheesecake.txt /var/tmp/test # Case 2: Dir only (recursive) # /var/tmp/foobar /var/tmp/test # Case 3: Wild Card Case '*.jpg' kind of thing # /var/tmp/foobar/*.jpg /var/tmp/test # # v0.0.4 2015 02 16 ################################################################## 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 1 echo -e "\n" echo -e "\t-----------------------------------------------------------------------" echo -e "\t We will be generating md5sum(s) for our file(s)." echo -e "\t-----------------------------------------------------------------------" sleep 1 } prtEndProgram() { sleep 1 echo -e "\tFinished generating MD5 values." echo -e "\tAll is well during this transfer." 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-----------------------------------------------------------------------" echo sleep 1 echo -e "\tAdjust this path to show the directory . " echo read -e -p " Enter the Source (From) location, then [ENTER]: " -i "/home/flo" srcPath read -e -p "Enter the Destination (To) location, then [ENTER]: " -i "/var/tmp" dstPath # Final Question read -e -p " Are you sure? [y|n]: " -n 1 result 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 # Case 1 # Single File Only if [[ -f "$srcPath" ]]; then cp -v "$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" >> /var/tmp/chkSum_$timestamp.md5; done echo "$dstPath/""$dirFileName" > /var/tmp/chkFile.txt prtEndProgram # Case 2 # Directory Recursive Only elif [[ -d "$srcPath" && -d "$dstPath" ]]; then cp -rv "$srcPath" "$dstPath" # FROM location a=${srcPath##*/} cd "$srcPath" lFilesSrc=`find . -type f` chkSumSrc=`find . -type f -print0 | xargs -0 md5sum | sort -V` # TO location # Make sure SRC and DEST Paths have NO Slash in the # end # Destination cp -rv /home/flo/blab /var/tmp/test cd "$dstPath"/"$a" cbo="$dstPath"/"$a" lFilesDst=`find . -type f` chkSumDst=`find . -type f -print0 | xargs -0 md5sum | sort -V` chkMD5Sum2 "$chkSumSrc" "$chkSumDst" "$cbo" ################################################################### echo -e "\n" echo -e "\t File copying has been completed." prtMD5SumMessage fileList=`find "$cbo" -type f | sort -V` echo "$chkSumDst" >> /var/tmp/chkSum_$timestamp.md5 echo "$fileList" > /var/tmp/chkFile.txt prtEndProgram else # Case 3 # Wild Card case EX: *.html # MD5sum checking before dump procedure dirBasePath="${srcPath%/*}" dirFileName="${srcPath##*/}" ################################# cd "$dirBasePath" cp -v $dirFileName "$dstPath" chkSumSrc=`find $dirFileName -maxdepth 1 -type f -print0 | xargs -0 md5sum` cd "$dstPath" chkSumDst=`find $dirFileName -maxdepth 1 -type f -print0 | xargs -0 md5sum` chkMD5Sum3 "$chkSumSrc" "$chkSumDst" "$dirFileName" ############################################################### # this part is a bit tricky since there might be files # in ths dest folder echo -e "\n" echo -e "\t File copying has been completed." prtMD5SumMessage fileList=`find "$dstPath"/$dirFileName -maxdepth 1 -type f | sort -V` echo "$chkSumDst" >> /var/tmp/chkSum_$timestamp.md5 echo "$fileList" > /var/tmp/chkFile.txt prtEndProgram fi else echo -e "\t Exiting due to selecting No." exit 1 fi exit 0