APM으로 게시판 만들기

잡다한거 1 - 백업 하기

파워레인저 2024. 6. 28. 09:25

주기적으로 백업을 하려 한다.

소스파일을 있는 디렉토리를 통째로 백업을 할거다.

 

먼저 드는 생각은 당연하게도, tar와 crontab.

 

#!/bin/bash

# 현재 날짜를 YYYYMMDD 형식으로 가져오기
DATE=$(date +'%Y%m%d')

# 압축 파일 이름 설정
TAR_FILENAME="{directory_name}_$DATE.tar"

# asdf 디렉토리 압축하기
tar -cvf $TAR_FILENAME ./{directory_name}

 

이쯤이면 tar를 이용하여 디렉토리 백업은 문제가 없고..

crontab에 등록을 하면 되기에

 

# crontab -e

0 0 * * * /home/xinet/backup.sh

 

을 등록했다.

 

0분, 0초, 매일, 매월, 모든 요일에 돌아가는 설정이다.

하지만 돌아가지 않는다.

 

[root@jk_test xinet]# systemctl status crond.service
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2024-06-18 20:32:01 EDT; 1 weeks 1 days ago
 Main PID: 1211 (crond)
    Tasks: 1 (limit: 24436)
   Memory: 1.6M
   CGroup: /system.slice/crond.service
           └─1211 /usr/sbin/crond -n

 6월 27 11:01:01 test CROND[112484]: (root) CMD (run-parts /etc/cron.hourly)
 6월 27 12:01:01 test CROND[112997]: (root) CMD (run-parts /etc/cron.hourly)
 6월 27 13:01:01 test CROND[113528]: (root) CMD (run-parts /etc/cron.hourly)
 6월 27 14:01:01 test CROND[114042]: (root) CMD (run-parts /etc/cron.hourly)
 6월 27 15:01:01 test CROND[114556]: (root) CMD (run-parts /etc/cron.hourly)
 6월 27 16:01:01 test CROND[115088]: (root) CMD (run-parts /etc/cron.hourly)
 6월 27 17:01:01 test CROND[115600]: (root) CMD (run-parts /etc/cron.hourly)
 6월 27 18:01:01 test CROND[116111]: (root) CMD (run-parts /etc/cron.hourly)
 6월 27 19:01:01 test CROND[116646]: (root) CMD (run-parts /etc/cron.hourly)
 6월 27 20:01:01 test CROND[117183]: (root) CMD (run-parts /etc/cron.hourly)

 

흠.. 서비스는 정상적으로올라왔는데.. cron.hourly 만 돌아간거 같다.

예전에 공부를 좀 해놓을걸. 시간마다 돌아가는 cron 인가?

 

백업쉘을 /etc/cron.hourly 디렉토리에 옮겨보고 기다려보자.

 

빠른 확인을 위해 시간을 옮기자

# date -s '2024-06-28 09:59'
2024. 06. 28. (금) 09:59:00 EDT

 

 응 EDT 여긴 어디?

 일단 한국이 아닌 듯.. 타임존을 한국으로 옮기자.

# ls -al /etc/localtime
lrwxrwxrwx. 1 root root 38  6월  3 14:09 /etc/localtime -> ../usr/share/zoneinfo/America/New_York

# ln -sf /usr/share/zoneinfo/Asia/Seoul /etc/localtime
#  ls -al /etc/localtime
lrwxrwxrwx 1 root root 30  6월 28 22:59 /etc/localtime -> /usr/share/zoneinfo/Asia/Seoul

# date -s '2024-06-28 09:59'
2024. 06. 28. (금) 09:59:00 KST

 

한국시간으로 옮겼음.

 

백업파일이 생성되지 않아서 /var/log/cron 로그를 살펴 보았다.

Jun 27 21:01:01 test run-parts[117496]: (/etc/cron.hourly) starting backup.sh
Jun 27 21:01:01 test CROND[117482]: (root) CMDOUT (/etc/cron.hourly/backup.sh:)
Jun 27 21:01:01 test CROND[117482]: (root) CMDOUT ()
Jun 27 21:01:01 test CROND[117482]: (root) CMDOUT (tar: ./backup: stat할 수 없습니다: 그런 파일이나 디렉터리가 없습니다)
Jun 27 21:01:01 test CROND[117482]: (root) CMDOUT (tar: 앞서 발생한 오류로 실패 코드를 반환하며 빠져나감)
Jun 27 21:01:01 test run-parts[117502]: (/etc/cron.hourly) finished backup.sh

 

쉘스크립트에 오류가 생긴 듯.

다시 살펴보자.

 

tar에 디렉토리가 상대경로로 지정되어 안된듯.

 

잘됨. 끝.