#!/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 |
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.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment