2022. 10. 21. 17:47ㆍ리눅스
CRON
- 주기적으로 작업되는 작업 일정을 저장해서 자동으로 실행하는 데몬으로 at이나 anacron등과
같이 다양한 프로그램이 지원된다.
관련 파일
/etc/rc.d/init.d/crond
- 데몬 실행 스크립트(service를 이용한다.)
/usr/lib/systemd/system/crond.service
- 데몬 실행 스크립트(systemctl을 이용한다.)
/etc/crontab
- 기본 스케줄 일정
/usr/bin/crontab
- 스케줄 설정 프로그램 파일
/etc/cron.allow, /etc/cron.deny
/usr/bin/crontab 이용 제한
- 등록된 계정에 대해서만 허용하거나 거부한다.
사용 형식
# crontab ‐u [유저] [옵션]
옵션
- e : 스케줄 등록
꞉ vi 환경으로 스케줄 등록
- l : 스케줄 확인 (출력)
- r : 스케줄 삭제
명령어 형식
- [분] [시] [일] [월] [요일] [작업내용]
시간 범위
꞉ 분 : 0 ~ 59
꞉ 시 : 0 ~ 23
꞉ 일 : 1 ~ 31
꞉ 월 : `1 ~ 12
꞉ 요일: 0(일요일) ~ 6(토요일)
- * : 미등록
# crontab ‐l
no crontab for root
# crontab ‐e
00 12 * * * rdate ‐s time.bora.net
0‐59/10 * * * * chown ‐R data.ist /home/data
~
~
~
‐‐ INSERT ‐‐
# crontab ‐l
00 12 * * * rdate ‐s time.bora.net
0‐59/10 * * * * chown ‐R data.ist /home/data
EX)
00 0‐23 * * * rdate ‐s time.bora.net
- 매시간마다 실행
0‐59/10 * * * * chown ‐R data.st /home/data
- 0에서 59분까지 10분마다 실행
0‐59/10 * * * * chmod ‐R 775 /home/data
기간 설정
- [#‐#] : 실행할 시간 범위를 지정
- [#‐#/#] : 범위내에서 실행 간격 지정
00 9‐18/2 1‐15/3 1‐12/4 1‐5 백업.sh
9-18/2 ->9시부터18시사이에 2시간마다 한번씩 실행
1-15/3 -> 1일부터15일사이에 아무때나 3일간격으로 실행, 1, 2, 3일 중 아무때나 실행
기본 스케줄 등록 파일
# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ minute (0 ‐ 59)
# | .‐‐‐‐‐‐‐‐‐‐‐‐‐ hour (0 ‐ 23)
# | | .‐‐‐‐‐‐‐‐‐‐ day of month (1 ‐ 31)
# | | | .‐‐‐‐‐‐‐ month (1 ‐ 12) OR jan,feb,mar,apr ...
# | | | | .‐‐‐‐ day of week (0 ‐ 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user‐name command to be executed
/var/log/cron
- cron에 등록된 명령어 실행 확인
- [작업 시간] [호스트] [프로세스] [계정] [명령어]
RSYNC
- 두 시스템 간에 지정한 디렉토리를 동기화한다.(동기화 = 복제개념)
- 어떤 시스템은 rsync 서버로 해도 상관없지만 보통 원본 저장 시스템을 서버로 한다.
윈도우 : 서비스 == 리눅스 : 데몬
rsync 설치 확인(IP : 161, 162)
[root@localhost ~]# yum list rsync
[root@localhost ~]# yum install -y rsync.x86_64 3.1.2-11.el7_9
관련 파일
- 데몬 : /usr/bin/rsync
- 관리 스크립트 : /usr/lib/systemd/system/rsyncd.service
- 설정 파일 : /etc/rsyncd.conf
데몬 실행
- # systemctl [start | stop | restart | status] rsyncd.service
설정
RSYNC 서버 설정 : /etc/rsyncd.conf
[root@linux161 ~] # cat /etc/rsyncd.conf
[backup] : 서비스명
path = /backup : 백업 경로
uid = root : 전송자 UID
gid = root : 전송자 GID
use chroot = yes : rsync 경로를 외부에서 / 로 인식
read only = yes : 읽기 전용 으로 접근한다. / 파일을 옮기는 방식은 파일 전체의 무결성이 훼손 될수있다.
hosts allow = 192.168.10.33 : 접속 허용할 호스트(클라이언트만 지정)
max connections = 1 : 동시 접속자 수
timeout = 300 : 파일을 못받고 대기상태(강제 종료 | 전제조건 / 아무것도 하지않을때)
RSYNC 클라이언트 : 동기화
- rsync client 명령을 이용 server의 디렉토리와 client의 디렉토리를 동기화한다.
- rsync ‐avz [‐‐delete] source destination
- rsync ‐avz [‐‐delete] IP::[서비스명] [백업 디렉토리]
- rsync ‐avz [‐‐delete] [백업 디렉토리] IP::[서비스명]
명령 옵션
‐v : 작업내역 출력
‐a : archive mode 작업, 심볼릭 링크, 권한 등 모든 내용을 보존한다.
‐z : 파일을 압축 전송한다.
‐‐delete : source에서 지워진 파일을 destination에서도 지워준다.(완전 동기화)
Local System 내에 동기화 가능
- rsync –avz /home/httpd/ /backup/httpd/
cron 과 rsync를 이용한 자동 백업
- rsync 명령을 cron에 등록한다.
- 매일 새벽 1시에 백업 수행
00 01 * * * rsync ‐avz 192.168.10.32::backup /backup
[root@linux33 ~]# crontab ‐l
00 12 * * * rdate ‐s time.bora.net
00 01 * * * rsync ‐avz 192.168.10.32::backup /backup
실습
[root@Linux1 ~]# yum list rsync
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirror.anigil.com
* epel: ftp.iij.ad.jp
* extras: mirror.anigil.com
* updates: mirror.anigil.com
Installed Packages
rsync.x86_64 3.1.2-11.el7_9 @updates
[root@Linux1 ~]# yum install -y rsync.x86_64 3.1.2-11.el7_9
vi /etc/rsyncd.conf에 추가 입력
[backup]
path = /backup
uid = root
gid = root
use chroot = yes
read only = yes
hosts allow = 192.168.10.33
max connections = 1
timeout = 300
[root@Linux1 ~]# systemctl stop rsyncd.service
[root@Linux1 ~]# systemctl start rsyncd.service
[root@Linux1 ~]# mkdir /backup
[root@Linux1 ~]# cd /backup/
[root@Linux1 ~]# echo 11 > a.txt
[root@Linux1 ~]# echo 22 > b.txt
[root@Linux1 backup]# ls
a.txt b.txt
162 linux로 이동
[root@linux162 ~]# mkdir /backup
양측 설정
ntsysv - 컴퓨터 자동으로 작동하는 프로그램 설정
[ ] firewalld.service - 체크해제
양측 설정
vi /etc/selinux/config
enforcing -> disable
[root@linux162 ~]# rsync ‐avz 192.168.10.32::backup /backup
receiving incremental file list
./
a.txt
b.txt
sent 69 bytes received 189 bytes 516.00 bytes/sec
total size is 6 speedup is 0.02
[root@linux162 ~]# crontab -l
00 01 * * * rdate -s time.bora.net
00 01 * * * rsync -avz 192.168.10.161::backup /backup
Service 관리와 부팅
서비스(service)와 데몬(daemon)
- 서비스와 데몬은 구분하지 않아도 된다. 이둘은 사용자의 요청 시점이 아니라 임의의 시점
(보통 시스템 boot)에서 background process로 시작되어 사용자나 프로세스에 서비스를 제공하는
프로그램을 의미한다.
방식
Stand Alone
- 스스로 listen하며 항상 메모리에 상주한다.
- 서비스 요청에 즉시 대응 가능하다.
- 서비스 요청이 매우 드물거나 idle 한 경우 메모리를 낭비한다.
Super Daemon
- Listen을 직접하지 않는다.
- 메모리에 상주하지 않으며 서비스 요청이 있을때 xinetd에 의해 호출된다.
- 거의 사라진 시스템이다. - 메모리가 커져서 사용x
서비스 조회
# systemctl list‐unit‐files
모든 서비스의 상태 확인
# systemctl [is‐enabled | is‐active] [서비스명]
- is‐enabled : 자동 실행 등록 여부
- is‐active : 현재 실행 여부
[root@linux ~]# systemctl is‐enabled crond.service
enabled
[root@linux ~]# systemctl is‐active crond.service
active
[root@linux ~]# systemctl is‐enabled kdump.service
disabled
[root@linux ~]# systemctl is‐active kdump.service
unknown
서비스 등록
# systemctl [enable | disable] [서비스명]
- [enable | disable] : 등록 및 등록 해제
# systemctl disable crond.service
Removed symlink /etc/systemd/system/multi‐user.target.wants/crond.service.
# systemctl is‐enabled crond.service
disabled
# systemctl is‐active crond.service
active
# systemctl enable crond.service
Created symlink from /etc/systemd/system/multi‐user.target.wants/crond.service to
/usr/lib/systemd/system/crond.service.
# systemctl is‐enabled crond.service
enabled
서비스 실행
# systemctl [start | restart | stop] [서비스명]
- [start | restart | stop] : 실행, 재실행, 종료
# systemctl stop crond.service
# systemctl is‐enabled crond.service
enabled
# systemctl is‐active crond.service
inactive
# systemctl start crond.service
# systemctl is‐enabled crond.service
enabled
# systemctl is‐active crond.service
active
systemctl 추가 옵션
# systemctl [옵션] [서비스명]
- try‐restart : 실행, 재실행, 종료
- Reload : 설정 재구동
- status : 상태 확인