为了抠出磁盘空间,需要对一台mail_server上对300+万个用户目录进行打包备份后删除,后期减少到80+W个,虽然减少不少,但仍然是大工程。其实也是无效工程,头痛医脚,当做自己写shell练手。以下为正文:
 
#!/bin/sh
#how to: ./shell.sh filename
#read filename line_by_line && xargs $line
#potaski@qq.com
#2012-04-06

#shell start time'定义执行开始时间'
DATE=`date`
shellstart=$(date +%s)
backup_dir=/data/zhangwei
mkdir -p $backup_dir
#cat /dev/null > /home/zhangwei/running.log
#cat /dev/null > /home/zhangwei/error.log
#cat /dev/null > /home/zhangwei/filename.log
 
#main
#默认第n行的内容为变量$line,自定义了参数i计算行数
i=0
while read line
do
        i=$(($i+1))
        bak_dir=/mail${line:50}
        dir_name=`echo $line|awk -F'/' '{print$11}'`
        d_num=`echo $line|awk -F'/' '{print$9}'`
        echo $bak_dir
        echo $d_num@$dir_name.tar.gz
        if [ -d $bak_dir ];then
                tar cvf $backup_dir/$d_num&@dir_name.tar.gz $bak_dir
                rm -rf $bak_dir
        else
                echo "$bak_dir DO-NOT-EXIST-REVIEW LINE:$i $line" >> /home/zhangwei.log
        fi
done < $1

#shell end time
shellend=$(date +%s)

#statistics:the line_number of files
echo "this file got $i lines"

#statistics:shell running time
echo "running time is $((shellend-$shellstart))second"
 
#2012-05-04: 检查命令运行时间的好方法shell> time CMD