自动备份脚本

  最近正在学习shell脚本编程,正好,线上又个系统的日志原来都是没半个月手动备份一次,特烦,而且一不小心就备份错误,所以就写了这个脚本,大家帮忙看看,写的比较粗糙,各位有啥好建议,欢迎指点啊~

脚本内容:

#!/bin/bash
######################################
#          creat in 2011/4/1         #
######################################

YEAR=`date '+%Y'`

MON=`date '+%m' -d last-month`

DAY=`date '+%d'`

logpath=/data/logs/www

logbackpath=/data2/log.bak/

IP=`/sbin/ifconfig -a |grep inet |grep -v "127.0.0.1" |grep -v "inet6" |grep -v "192.168.*.*" |head -1 |awk '{split($2,a,":"); print a[2]}'`

Mailto="[email protected]"

if [ ! -d $logpath/"$YEAR$MON" ];then

   mkdir $logpath/"$YEAR$MON"

  if [ `echo $?` -eq 0 ];then

    mv $logpath/*.$YEAR-$MON-* $logpath/"$YEAR$MON"/

    cd $logpath

    tar -czf "$YEAR$MON".tgz "$YEAR$MON"

    mv $logpath/"$YEAR$MON".tgz $logbackpath

      if [ `ps -ef |grep mv |grep -v "grep" |wc -l` -eq 0 ];then

         rm -rf $logpath/"$YEAR$MON"

      fi

  else

  echo -e "usousou log copy is errored" | mail -s "usousou copy is errored on $IP in `date '+%m/%d/%Y %H:%M'`" $Mailto

  fi

fi

你可能感兴趣的:(shell,职场,自动备份,休闲)