由于工作原因,要管理一些图片服务器,其中有几台是作为图片存放的机器,每台机器配有10T的硬盘,最近硬盘告急,都只剩下几百个G,所以要删除一些图片。
目前服务器有一亿多个文件,删过这种大量的小碎文件的朋友一定知道使用rm -rf的下场,效果非常的差,基本上看不到什么效果。通过find命令带delete的效果也不太好,后来使用了rsync将空目录同步过去的方式,效果比较好,一天大概能清理出400G的空间。
为了保证并发的效果,写了脚本充当守护进程
#!/bin/sh #./delete.sh get_dir_path_cmd rsync_delete_cmd_process_total_count #./delete.sh "find /data1/sinawapcms/htdocs/images/iask/2012/08/* -maxdepth 0" 20 rsync_path=rsync empty_dir=/tmp/empty/ get_rsync_process_num() { echo $(ps -ef | grep rsync | grep delete | wc -l); } #$1 is rsync cmd , $2 is rsync total process num do_rsync_cmd() { while test 1 = 1 do if test $(get_rsync_process_num) -lt $2 then ($1 >/dev/null 2>&1 &); break; else sleep 100; fi done } if ! ( $rsync_path --version > /dev/null 2>&1 ) then echo rsync path error; exit 1; fi if ! ls -l $empty_dir > /dev/null 2>&1 then if ! mkdir -p $empty_dir >/dev/null 2>&1 then echo Permission Denied, make sure you have permission to mkdir -p /tmp/empty ; exit 3; fi fi if ! test $( ls -la $empty_dir | wc -l) = 3 then echo Directoty \"$empty_dir\" is not empty, empty directory? ; printf "Enter (Y/N):"; read action < /dev/tty; if test $action = "Y" || test $action = 'y' then if ! rm -rf ${empth_dir}/* >/dev/null 2>&1 then echo Permission Denied, make sure you have permission to rm -rf $empty_dir; exit 3; fi else echo error, Directoty \"$empty_dir\" is not empty; exit 2; fi fi dir=$($1); #rsync --delete-before -a -H /tmp/empty/ /data1/sinawapcms/htdocs/images/iask/2012/08/ff/ >/dev/null & for i in $dir do sleep 5; cmd="$rsync_path --delete-before -a -H $empty_dir ${i}/" ; (do_rsync_cmd "$cmd" $2); done
调用方式:
./.sh 20 第一个参数用来获取我们要清空的目录 第二个参数限定并发数,防止服务器负载过高
本文出自 “邱凯的技术博客” 博客,请务必保留此出处http://xdebug.blog.51cto.com/1135229/1067241