搜索体积较大的文件,自动备份

#!/bin/sh
########################################################################
# Desc: thie shell script is used to monitor file size,and backup it.
# Date: 2013-12-27
#########################################################################

#Function: process the backup action.

backup()
{
AVASIZE=`df  .|grep -v Filesystem|awk -F" " '{print $4}'`
AVASIZE=`expr $AVASIZE * 1000`

#echo $1

for F in $1
do
 MBASENAME=`basename $F`
 MDIRNAME=`dirname $F`
 CURFILESIZE=`ls -l $F|grep -v total|grep -v "log.[0-9]{4}"|awk -F' ' '{print $5}'`
 #***** IF FILTING SOME NOT VALID FILE,YOU MUST NOT COMPARE BELOW ******
 if [ "$CURFILESIZE" == "" ];then
   continue;
 fi
 echo $AVASIZE $CURFILESIZE $MBASENAME $MDIRNAME
 if [ $CURFILESIZE -lt $AVASIZE ];then
  echo "--- `date +%Y-%m-%d' '%T`: FOUND SO BIGGER FILE,BEGIN BACKUP ---" >> monitor_file_size.log
  cp $F $F".`date +%Y_%m_%d_%H_%M_%S`"
  #rm -f $F
  >$F
  echo " File $F BACKUP TO $F.`date +%Y_%m_%d_%H_%M_%S`, File Size: $CURFILESIZE" >> monitor_file_size.log

  echo "--- `date +%Y-%m-%d' '%T`: END OF BACKUP ---" >> monitor_file_size.log
  echo "" >> monitor_file_size.log
 else
  echo "WARNING: The partition has not enough space !"
 fi
done;
}

if [ $# -ne 2 ]; then
 echo "Usage: `basename $0` MONITOR_PATH MONITORFILE_SIZE(Mbytes)"
 exit 0
fi

MPATH=$1
MSIZE=`expr $2 * 1024`


backup "`find $MPATH -type f -size +$MSIZE -print`"

标签: 无
返回文章列表 文章二维码
本页链接的二维码
打赏二维码
添加新评论