오류로 인해 작업한 일을 기록
NAS가 일주일동안 꺼져있던 것을 작업 중에 발견했다...
DB를 mariabackup으로 다시 증분 백업을 하고, 프로그램 log 들도 다시 백업해 두기 위해 확인하고 있는데..
어라...?
서비스 하나가 작년 12월 부터 crontab이 제대로 동작하지 않아서 로그가 백업이 안되고 있었네..?
crontab이 종종 추가하고 나서 service cron stop & service cron start
또는 service cron restart
와 같이 재시작 해주지 않으면 정상적으로 동작하지 않는 경우가 있다..
다섯달 만에 발견해서 그동안 밀려있던 log들을 백업하기 위해 하루치 씩 총 138일치를 돌려야한다..
반복 작업을 위해 shell script를 끄적끄적...
linux에 기본 내장된 date version 8.30 기준으로 아래와 같이 많은 옵션을 제공한다.
%% a literal %
%a locale's abbreviated weekday name (e.g., Sun)
%A locale's full weekday name (e.g., Sunday)
%b locale's abbreviated month name (e.g., Jan)
%B locale's full month name (e.g., January)
%c locale's date and time (e.g., Thu Mar 3 23:05:25 2005)
%C century; like %Y, except omit last two digits (e.g., 20)
%d day of month (e.g., 01)
%D date; same as %m/%d/%y
%e day of month, space padded; same as %_d
%F full date; same as %Y-%m-%d
%g last two digits of year of ISO week number (see %G)
%G year of ISO week number (see %V); normally useful only with %V
%h same as %b
%H hour (00..23)
%I hour (01..12)
%j day of year (001..366)
%k hour, space padded ( 0..23); same as %_H
%l hour, space padded ( 1..12); same as %_I
%m month (01..12)
%M minute (00..59)
%n a newline
%N nanoseconds (000000000..999999999)
%p locale's equivalent of either AM or PM; blank if not known
%P like %p, but lower case
%q quarter of year (1..4)
%r locale's 12-hour clock time (e.g., 11:11:04 PM)
%R 24-hour hour and minute; same as %H:%M
%s seconds since 1970-01-01 00:00:00 UTC
%S second (00..60)
%t a tab
%T time; same as %H:%M:%S
%u day of week (1..7); 1 is Monday
%U week number of year, with Sunday as first day of week (00..53)
%V ISO week number, with Monday as first day of week (01..53)
%w day of week (0..6); 0 is Sunday
%W week number of year, with Monday as first day of week (00..53)
%x locale's date representation (e.g., 12/31/99)
%X locale's time representation (e.g., 23:13:48)
%y last two digits of year (00..99)
%Y year
%z +hhmm numeric time zone (e.g., -0400)
%:z +hh:mm numeric time zone (e.g., -04:00)
%::z +hh:mm:ss numeric time zone (e.g., -04:00:00)
%:::z numeric time zone with : to necessary precision (e.g., -04, +05:30)
%Z alphabetic time zone abbreviation (e.g., EDT)
이중에 내가 자주 사용 하는 옵션들은 아래와 같다.
date -d 'yesterday' -- 하루 전
date -d '1 day ago' -- 하루 전
date -d '+1 day' -- 하루 후
date -d '-1 day' -- 하루 전
date -d '2 day ago' -- 이틀 전
date -d '1 week ago' -- 일주일 전
date -d '1 month ago' -- 한 달 전
date -d '1 year ago' -- 일년 전
date -d 'YYYYMMDD' -- 해당 일
date +%j -- 1월1일부터 12월 31일 까지 1 ~ 365(윤년 366) 으로 변환해서 오늘이 며칠 째인지 출력된다.
### 옵션을 이어서 적으면 그대로 연결되서 출력된다.
date +%Y%m%d -- YYYYMMDD 형식으로 format
date +%Y-%m-%d -- YYYY-MM-DD 형식으로 format
date1=`date -d "20220101" +%s`
date2=`date -d "20211001" +%s`
date3=`echo "($date1 - $date2) / 86400" | bc`
- 실수 연산을 하려면 bc 명령어를 사용한다.
- unixtimestamp 로 변환해서 계산하고 86400(하루를 초로 변환) 로 나눠주면 차이나는 일수를 구할 수 있다.
shell 에서도 C++나 JAVA, JS 와 같은 언어들 처럼 반복문을 사용할 수 있다.
while [조건] :
do
...
done
위와 같이 while 문을 사용할 수 있는데, 조건식에서 아래와 같은 표현식을 사용할 수 있다.
표현식 | 기능 | 예 |
---|---|---|
gt (greater) | 크다 | a gt b |
ge (greater or equal | 크거나 같다 | a ge b |
lt (little) | 작다 | |
le (little or equal | 작거나 같다 | |
eq (equal) | 같다 | |
ne (not equal) | 같지 않다 |
혹은, for문도 사용 가능 하다
for i in 1 2 3
do
...
done
for i in {1..3}
do
...
done
for ((i=1; i<4; i++))
do
...
done
array=("test1" "test2")
for i in ${array[@]}
do
echo $i
done
위와 같이 사용 가능하다.
date1=`date -d "20220101" +%s`
date2=`date -d "20211001" +%s`
date3=`echo "($date1 - $date2) / 86400" | bc`
for var in `seq 1 1 $date3`
do
date=`date +%Y%m%d --date "$var day ago"`
echo $date
done
이런식으로 반복문과 날짜를 함께 이용할 수 있다.