★背景:程序自動生成日誌文件,一天生成一個目錄,目錄下爲每小時生成的目錄,即24個目錄,小時目錄下爲文件。因文件較大,須要配置腳本按期進行壓縮並刪除幾天前的文件。shell
★目標文件結構及內容以下:ide
X3950x5-/opt/BOCO.DAL/AFM_LOG> lsspa
20141215 20141216 20141217 autobakdel.sh日誌
X3950x5-/opt/BOCO.DAL/AFM_LOG> cd 20141215crontab
X3950x5-/opt/BOCO.DAL/AFM_LOG/20141215> lsip
00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23rem
X3950x5-/opt/BOCO.DAL/AFM_LOG/20141215> cd 23it
X3950x5-/opt/BOCO.DAL/AFM_LOG/20141215/23> lsio
AFM_10001_8790.data.0.LOG AFM_10001_9404.data.3.LOG AFM_20001_100-103.index.0.LOG AFM_20001_100-111.state.3.LOG AFM_20001_100-501.state.0.LOGfunction
AFM_10001_8790.data.1.LOG AFM_10001_9404.data.4.LOG AFM_20001_100-103.index.1.LOG AFM_20001_100-111.state.4.LOG
★下邊爲自動壓縮並刪除的腳本。
X3950x5-/opt/BOCO.DAL/AFM_LOG> more autobakdel.sh
#!/bin/csh
#it is a shell script which provides function of auto backup and delete logfiles regularly
#by fuxueli
set despath=/opt/BOCO.DAL/AFM_LOG
###########查找一天前的目錄並壓縮,刪除源文件############
set dir=`find $despath/ -maxdepth 1 -type d -ctime +1 | grep -v .gz`
foreach i($dir)
tar zcvf $i.tar.gz $i --remove-files
end
##############刪除三天前的文件##########################
find $despath/ -ctime +3 -exec rm -rf {} \;
完成後添加到crontab中自動執行便可。