Backup and Delete

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

###############################################################################
#
# Author:
#       Joey Y Q Mei
#
# Date:
#       10 Dec 2012
#
# Description:
#       zip folder
#
# Usage:
#       Parameter 1: source folder path
#	Parameter 2: job id
#
# Sample:
#	BackupDelete.sh /hp/xxx/aaa jobId
#                       
###############################################################################


if [ $# -lt 2 ]
	then
      echo "Error: Please input the arguement:"
      echo "Example: zip_folder.sh /hp/xxx/aaa jobId"
      exit 1
fi

source_folder_path=$1
job_id=$2
timeStamp=`date +%Y%m%d`
source_folder_name=${source_folder_path##*/}
cd_folder_path=${source_folder_path%/*}
archive_folder=$cd_folder_path/Archive
keep_num=10

if [ ! -d "$source_folder_path" ]
	then
			echo ">>>>> Error: The folder <$source_folder_path> does not exist."
			exit 1
fi

if [ ! -d "$archive_folder" ]
	then
			echo ">>>>> Error: The folder <$archive_folder> does not exist."
			exit 1
fi

zip_folder_path=$cd_folder_path/Archive/$timeStamp
if [ ! -d "$zip_folder_path" ]
			then
						echo ">>>>> Add: dir <$zip_folder_path> "
		        mkdir $zip_folder_path
fi

cd $cd_folder_path
zip_file=$source_folder_name"_"$job_id.zip
zip -r $zip_folder_path/$zip_file $source_folder_name

#remove files function
function_rmfiles(){
	for file in `ls $1`
	do
		rm -r $1/$file
		echo ">>>>> Info: <$1/$file> has been removed."
	done
}

#clear source_folder_path files
if (( $? == "0"))
then
	echo ">>>>> Success: <$zip_folder_path/$zip_file> has been created."
	
	for folder in `ls $source_folder_path`
	do 
		if [ $folder = "Compare" ]
		then
			for subfolder in `ls $source_folder_path/$folder`
			do
				function_rmfiles $source_folder_path/$folder/$subfolder
			done
		else	
			function_rmfiles $source_folder_path/$folder
		fi
	done
else
	echo ">>>>> Error: Create <$zip_folder_path/$source_folder_name.zip> failed."
	exit 1
fi

#clear old backup folder
echo ">>>>> Info: Cleaning old folders."
signal=0
hasOld=0
while((signal!=1))
do
	backup_folders=`ls $archive_folder`
	arr_backup_folders=($backup_folders)
	count=${#arr_backup_folders[@]}
	
	if (( "$count" > "$keep_num"))
	then
			old_file=$archive_folder/${arr_backup_folders[0]}
			rm -rf $old_file
			hasOld=1
      echo ">>>>> Info: <$old_file> has been removed."
  else
  		signal=1
	fi
done
if(( "$hasOld" == "1"))
then
	echo ">>>>> Success: $archive_folder Old folders has been removed."
else
	echo ">>>>> Success: $archive_folder no old folders."
fi