2012-06-28

Groovy : Rotate Backup file or Log file



Windows 에서 백업파일을 Rotate 하여 최근 N일 동안의 백업파일을 유지하도록 하였다.

final DAY_LIMIT  = 14      // N일
final TARGET_DIR = "M:/"


def today   = new Date()
def command = "cmd /c m: & cd \\ & mkdir ${today.format('YYYYMMdd')} & move backup\\*.tib ${today.format('YYYYMMdd')}"
command.execute().waitFor()

new File(TARGET_DIR).eachFile { path ->
    if (path.directory && path.name =~ /201[2-9]\d{4}/) {
        def lastModified = new Date(path.lastModified())
        def diffDay = new Date() - lastModified
        if(diffDay > DAY_LIMIT) {
            // println(lastModified.format("YYYY-mm-dd") + " : ${path}")
            println("cmd /c rd /q /s ${path}")
            "cmd /c rd /q /s ${path}".execute().waitFor()
        }
    }
}

2012-06-15

Groovy : 여러 디렉토리의 사이즈를 구하고 크기 순으로 정렬하기

Ruby 버전에 이어서 Groovy 버전도 만들어보았다. 거의 생김새가 비슷한 언어여서, 몇가지만 수정하면 바로 동작한다.

def FOLDER_LIST = [
                    "/path/to1",
                    "/path/to2",
                    "/path/to3",
                    "/path/to4",
                    "/path/to5",
                  ]

def startDate = new Date().format('yyyy/MM/dd HH:mm:ss')

def size_folder_list = []
FOLDER_LIST.each { folder_name ->
    def folder_size = 0
    new File(folder_name).eachFileRecurse { path ->
        folder_size += path.size()
    }
    size_folder_list << "${folder_size}|${folder_name}"
}

size_folder_list = size_folder_list.sort { a, b ->
    (a_size, a_file) = a.split(/\|/)
    (b_size, b_file) = b.split(/\|/)
    // b_size as int <=> a_size as int
    b_size.toInteger() <=> a_size.toInteger()
}

size_folder_list.each { folder ->
    (f_size, f_path) = folder.split(/\|/)
    println("${f_size},${f_path}")
}


def endDate = new Date().format('yyyy/MM/dd HH:mm:ss')
println "\n\n\n"
println "================================================================================"
println "Start Time : $startDate"
println "End   Time : $endDate"
println "================================================================================"

2012-06-14

Ruby : 여러 디렉토리의 사이즈를 구하고 크기 순으로 정렬하기

Ruby 에서 여러 디렉토리의 사이즈를 구하고 크기 순으로 정렬하는 방법을 구현해보았다. 여러 팀에서 공유하는 파일의 서버의 경우, 각 디렉토리별(팀)로 어느 정도 사용하는 지를 알아보고 싶을 때 이용하면 좋을 것이다.

require 'find'


$FOLDER_LIST = [
                'C:\\path\\to1',
                'C:\\path\\to2',
                'C:\\path\\to3',
                'C:\\path\\to4',
                'C:\\path\\to5',
               ]

start_time = Time.now


size_folder_list = Array.new
$FOLDER_LIST.each() do | folder_name |
  folder_size = 0
  Find.find(folder_name) do | path |
    if(File.exist?(path))
      folder_size += File.size(path)
    end
  end
  size_folder_list << "#{folder_size}|#{folder_name}"
end

size_folder_list = size_folder_list.sort do |a, b| 
  (a_size, a_file) = a.split(/\|/)
  (b_size, b_file) = b.split(/\|/)
  b_size.to_i <=> a_size.to_i
end

size_folder_list.each do | folder |
  (f_size, f_path) = folder.split(/\|/)
  print f_size + "," + f_path + "\n"
end


end_time = Time.now
puts "\n\n========================================================================="
puts "Start Time : #{start_time}"
puts "End Time   : #{end_time}"