Unix

날짜 계산하기

ForceCore 2009. 9. 1. 01:21
백업을 하다보면, 7일전의 날짜것을 지워서 최근 7일간의 압축파일만 두고 싶을 때가 있을 것이다.
오늘의 날짜도. 그런 때에는 date명령어를 쓰는 것이 가장 편하다.
아래 스크립트는 필자가 실제로 사용하고 있는 스크립트인데, 굵은 글씨를 해놓은 부분이 날짜를 계산하는 부분이다. 날짜 형식은 "2009.09.01" 이런 식이다.

#!/bin/bash

#
# configuration
#
home=/home/special/ddd
archive_cnt=7

#
# main
#

# make names
now=`date +%Y.%m.%d` # current date
newdir=taengu-$now
newzip=$newdir.zip
old=`date -d "$archive_cnt days ago" +%Y.%m.%d`
oldzip=taengu-$old.zip

# pull stuff
cd $home/taengu
hg pull 2>&1 | cat > /dev/null

# make a fresh copy
cd $home
hg clone taengu $newdir 2>&1 | cat > /dev/null

# make archive
cd $newdir
rm -rf .hg # delete history for this copy.
cd $home
rm -f $newzip
zip -q -r $newzip $newdir
rm -rf $newdir # remove temp dir. we only need the archive.

# remove old file
rm -f $oldzip
참 쉽죠?