Thursday, October 27, 2011

[Bash] find and backup files

Similar to previous post Check and backup but using the command of find. Also won't create the extra empty folders when backing up files with specified type.

#!/bin/bash
# to check whether the target folder contains same files as the source folder
# Usage:
#           findback.sh -s source_folder -t target_folder -name name_pattern -type filetype -copy
#     e.g.,
#           findback.sh -s /mnt/mywork/mfiles -t /mnt/mybackup/mfiles -type f -name '*.m' -copy -sub
#                             type: f--> regular file; d--> directory; l--> symbolic link [man find]
#copyright @ http://scriptdemo.blogspot.com

isSource=0
isTarget=0
isFiletype=0
isCopy=0
isSubFolder=0
isName=0;
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;
         ;;
       -p|-pattern|-name)
         isName=1;
         namepattern=$2
         shift; shift;
         ;;
       -filetype|-type)
         isFiletype=1
         filetype=$2
         if [ ${filetype} != 'f' ] && [ ${filetype} != 'd' ] && [ ${filetype} != 'l' ]; then
            echo "type option: ${filetype} is not supported yet"
            exit
         fi
         shift;shift;
         ;;
       -copy|-COPY|-cp)
         isCopy=1
         shift
         ;;
       -sub|-all|-subfolder|-deeper)
         isSubFolder=1
         shift
         ;;
       *)
        echo "unkown option: $1"
        shift
        ;;
    esac
done

[ ${isName} -eq 0 ] && echo "-name option missing: No name-pattern is specified" && exit
if [ ${isSource} -eq 0 ] && [ ${isTarget} -eq 0 ] ; then
   echo "Usage: findback.sh -s source_foler -t target_folder -name name-pattern -type f -copy -sub"
   echo " type: f--> regular file; d--> directory; l--> symbolic link"
   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='f'
fi
if [ ${isSubFolder} -eq 1 ]; then
   eval "mylist=( `find ${sFolder} -type ${filetype} -name "${namepattern}" 2< /dev/null` )"
else
   eval "mylist=( `find ${sFolder} -maxdepth 1 -type ${filetype} -name "${namepattern}" 2< /dev/null` )"
fi
sFoldersed=`echo ${sFolder} | sed 's/\//\\\\\//g'`
tFoldersed=`echo ${tFolder} | sed 's/\//\\\\\//g'`
if [ ${#mylist} -gt 0 ]; then
   for fln in ${mylist[*]}
   do
      eval "tfln=\`echo ${fln} | sed 's/${sFoldersed}/${tFoldersed}/'\`"
      if [ ${filetype} == 'f' ] || [ ${filetype} == 'l' ]; then
         # get the path first
         tflpath=`dirname ${tfln}`
         if [ ! -d ${tflpath} ]; then
            if [ ${isCopy} -eq 0 ]; then
               echo -e "[testing] CREATE:\n[testing] mkdir -p ${tflpath}"
            else
               eval mkdir -p "${tflpath}"
            fi
         fi
         if [ -e ${tfln} ]; then
            isDiff=`diff -bB "${fln}" "${tfln}"`
            if [ ${#isDiff} -gt 0 ]; then
               if [ ${isCopy} -eq 1 ]; then
                 echo -e "DIFF: ${tfln} is modified! copying...\n"
                 eval cp -f "${fln}" "${tfln}"
               else
                 echo "[testing] DIFF: ${fln} is modified!"
               fi
            fi
         else
            if [ ${isCopy} -eq 1 ]; then
               echo -e " copy: ${tfln} does not exist copying...\n"
               eval cp -f "${fln}" "${tfln}"
            else
               echo -e "[testing] copy: ${tfln} does not exist\n"
            fi
         fi

      elif [ ${filetype} == 'd' ]; then
           if [ ! -d ${tfln} ]; then
              if [ ${isCopy} -eq 0 ]; then
                 echo -e "[testing] CREATE:\n[testing] mkdir -p ${tfln}"
              else
                 echo -e "CREATE:\n mkdir -p ${tfln}"
                 mkdir -p "${tfln}"
              fi
           fi
      fi
   done
else
   echo "Nothing is found!"
   exit
fi

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

ShowCalendar