2012. 7. 27. 19:20 IT


서버에서 로그를 지우고 싶은데,,, 쉘 스크립트를 짜고 싶지 않았다. 

그래서 그루비 쉘로... ;;



로그 파일을 대상으로 lastModified date가 오늘보다 1달 전인 것들을 

월별 디렉토리에 백업해주는 기능.  cron에 등록해놓으면 편할듯. 


cf) TimeCategory 기능 같은게 편하다... 



import groovy.time.TimeCategory;

def backup( def searchPath, def backupdate, def postfix = ~/.*log$/ ){

        def today = new Date();

        def loglist = new File(searchPath).listFiles().grep(postfix);

        def cnt = 0;

        loglist.each{ file ->

                def dt = new Date( file.lastModified())

                        if( backupdate > dt ){

                                def backupdir = searchPath + "/" + dt.format("yyyyMM")

                                new File(backupdir).mkdir();

                                "mv ${searchPath}/${file.name}  ${backupdir}".execute();

                                cnt++;

                       }

        }

        println " ${cnt} files moved "

}

def log_dir_list = ["/software/log", "/software/log/access" ];

def backupdate;

use(TimeCategory) {

        backupdate = new Date()  - 1.month

}

println "[ LOG Backup ] log < " + backupdate.format("yyyyMMdd") + " (lastModified date)"

log_dir_list.each{

        print "\t log backup target :  ${it}  -> "

        backup(it, backupdate);

}


posted by smplnote