Monday, October 24, 2011

[Bash] Check and backup files in two different folders

#!/bin/bash
# to check whether the target folder contains same files as the source folder
#     with an option to back the file not existing in target folder.
#     More actions could be done for the modified ones in the future
#     Also nothing is done to sub-folders so far.
# Usage:
#    CheckDiff.sh -s source_folder -t target_folder -type filetype -copy
# e.g.,
#    CheckDiff.sh -s /mnt/mywork/mfiles -t /mnt/mybackup/mfiles -type m -copy
# Update:
#            oct 25, 2011:
#                                  consider file name with space
#                                  copy sub-folders if not exist
#                                  add debug option to show the filenames [-debug]
#                                  add option to check inside of existing sub-folders [-sub]
#            oct 26, 2011:
#                                 automatically creating sub-folders if filetype is specified
#                                 test-mode [-test]
#                                 redirect the error msgs [2< /dev/null] 
#                     Future: 
#                                check modified date to handle modified files
#                                Simple GUI                
#copyright @ http://scriptdemo.blogspot.com


isSource=0
isTarget=0
isFiletype=0
isCopy=0
isDebug=0
isSubFolder=0
isTest=0
optstr=""
while [ $# -gt 0 ];
do
     case $1 in
       -t|-target)
         [ ! -d $2 ] && echo " target folder: $2 does not exist" && exit
         isTarget=1
         tFolder=`echo $2 | sed 's/\/$//'`
         shift;shift;
         ;;
       -s|-source)
         [ ! -d $2 ] && echo " source folder: $2 does not exist" && exit
         isSource=1
         sFolder=`echo $2 | sed 's/\/$//'`
         shift;shift;
         ;;
       -filetype|-type)
         isFiletype=1
         filetype=$2
         optstr="${optstr} -filetype ${filetype}"
         shift;shift;
         ;;
       -copy|-COPY|-cp)
         isCopy=1
         optstr="${optstr} -cp"
         shift
         ;;
       -d|-debug|-D)
         isDebug=1
         optstr="${optstr} -debug"
         shift
         ;;
       -sub|-all|-subfolder|-deeper)
         isSubFolder=1
         optstr="${optstr} -sub"
         shift
         ;;
       -test|-TEST)
         isTest=1
         optstr="${optstr} -test"
         shift
         ;;
       *)
        echo "unkown option: $1"
        shift
        ;;
    esac
done

if [ ${isSource} -eq 0 ] && [ ${isTarget} -eq 0 ]; then
   echo "Usage: CheckDiff.sh -s source_folder -type filetype -t target_folder [-copy]"
   exit
fi
if [ ${isSource} -eq 0 ] && [ ${isTarget} -eq 1 ]; then
   sFolder=${PWD}
   echo "--------------------------------------------"
   echo "| source folder is set to current folder: $PWD"
   echo "--------------------------------------------"
fi
if [ ${isSource} -eq 1 ] && [ ${isTarget} -eq 0 ]; then
   tFolder=${PWD}
   echo "--------------------------------------------"
   echo "| target folder is set to current folder: $PWD"
   echo "--------------------------------------------"
fi

if [ ${isFiletype} -eq 0 ]; then
   filetype='*'
else
   filetype0=`echo ${filetype} | sed -e 's/^\*\.//'`
   filetype="*.${filetype0}"; unset filetype0
fi
if [ ${isSubFolder} -eq 1 ]; then
   if [ ${filetype} != '*' ]; then
      eval "folderLev0=( `ls -d ${sFolder}/*/ 2< /dev/null | awk -F/ '{print $(NF-1)}'` )"
      eval "fileLev0=( `ls ${sFolder}/${filetype} 2< /dev/null | awk -F/ '{print $NF}'` )"
      eval "mylist=( ${folderLev0[*]} ${fileLev0[*]} )" && unset foldeLev0 fileLev0
   else
      eval "mylist=( `ls ${sFolder}/ 2< /dev/null ` )"
      isFiletype=0
   fi
else
   if [ ${filetype} != '*' ]; then
      eval "mylist=( `ls ${sFolder}/${filetype} 2< /dev/null | awk -F/ '{print $NF}'` )"
   else
      eval "mylist=( `ls ${sFolder}/ 2< /dev/null` )"
      isFiletype=0
   fi
fi
if [ ${#mylist} -gt 0 ]; then
   [ ${isDebug} -eq 1 ] && echo -e "\nmylist: \n ${mylist[*]}\n"
   for fln in ${mylist[*]}
   do
      fname="${sFolder}/${fln}"
      [ ${isDebug} -eq 1 ] && echo "fname=${fname}"
      if [ -d "${fname}" ]; then
         if [ ! -d "${tFolder}/${fln}" ]; then
            echo "${fln} does not exist in ${tFolder}"
            if [ ${isCopy} -eq 1 ]; then
               if [ ${isFiletype} -eq 1 ]; then
                  echo " |- creating ${tFolder}/${fln}..........."
                  if [ ${isTest} -eq 1 ]; then
                     echo " |- mkdir -p ${tFolder}/${fln}"
                     echo " |- digging deeper into ${sFolder}/${fln}........"
                     echo " |- CheckDiff.sh -s ${sFolder}/"${fln}" -t ${tFolder}/"${fln}" ${optstr}"
                     echo " "
                  else
                     eval "mkdir -p ${tFolder}/${fln}"
                     [ ${isDebug} -eq 1 ] && echo " |- digging deeper into ${sFolder}/${fln}........"
                     [ ${isDebug} -eq 1 ] && echo -e " |- CheckDiff.sh -s ${sFolder}/"${fln}" -t ${tFolder}/"${fln}" ${optstr}"
                     eval CheckDiff.sh -s ${sFolder}/"${fln}" -t ${tFolder}/"${fln}" ${optstr}
                   fi
               else
                  echo " copying whole folder..........."
                  if [ ${isTest} -eq 1 ]; then
                     echo "cp -r ${fname} ${tFolder}"
                  else
                     eval "cp -r "${fname}" ${tFolder}"
                  fi
               fi
            else
               echo "OMIT: ${fname} is a folder...."
            fi
         else
            if [ ${isSubFolder} -eq 0 ]; then
               echo "OMIT: ${fln} is a folder...."
            else
               [ ${isDebug} -eq 1 ] && echo " digging deeper into ${sFolder}/${fln}........"
               if [ ${isFiletype} -eq 1 ]; then
                  [ ${isDebug} -eq 1 ] && echo -e " |- CheckDiff.sh -s ${sFolder}/${fln} -t ${tFolder}/${fln} ${optstr}\n"
                  eval "CheckDiff.sh -s ${sFolder}/"${fln}" -t ${tFolder}/"${fln}" ${optstr}"
               else
                  [ ${isDebug} -eq 1 ] && echo -e " |- CheckDiff.sh -s ${sFolder}/${fln} -t ${tFolder}/${fln} ${optstr}\n"
                  eval "CheckDiff.sh -s ${sFolder}/"${fln}" -t ${tFolder}/"${fln}" ${optstr}"
               fi
            fi
         fi

      else
         if [ -e ${tFolder}/"${fln}" ]; then
            isDiff=`diff -bB "${tFolder}/${fln}" "${tFolder}/${fln}"`
            if [ ${#isDiff} -gt 0 ]; then
               echo "DIFF: $fln is modified."
               #consider to make a new copy
            fi
         else
            if [ ${isCopy} -eq 0 ]; then
               echo "COPY: ${fln} does not exist in ${tFolder}"
            else
               echo -e "ALERT: ${fln} does not exist in ${tFolder} --copying....."
               if [ ${isTest} -eq 1 ]; then
                  echo "cp "${fname}" ${tFolder}"
               else
                  eval "cp "${fname}" ${tFolder}"
               fi
            fi
         fi
      fi
   done
fi

No comments:

ShowCalendar