[Linux] rsync 와 EFS(NFS)를 이용한 로그 백업

ASHAPPYASIKNOW·2022년 7월 25일
0

OS(Linux, Mac, Windows)

목록 보기
5/9
post-thumbnail

AWS Console에서 EC2 시큐리티 그룹 설정

인바운드 규칙과 아웃바운드 규칙에 2049 포트를 열어준다.

EFS에서는 2049포트를 사용하기 때문에 해당 규칙을 추가해 주어야 한다.

EC2에 EFS(Elastic File System) 연결하기

sudo mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport fs-01234567896.efs.ap-northeast-2.amazonaws.com:/ efs-my-folder

EFS를 특정 폴더에 (efs-my-folder) 에 mount 시킨다.

EC2에 rsync 파일 만들기

RSYNC에 대해서 알고 싶다면?

폴더 및 파일 생성

# user의 home 디렉토리로 이동 
> cd /home/ec2-user

# rsync_scripts 폴더 만들기 및 이동
> mkdir rsync_scripts
> cd ./rsync_scripts

# crond_execution.log 파일 만들기
> touch crond_execution.log

# rsync 실행을 위한 shell script 만들기
> vim rsync_logs.sh

rsync_log.sh

#!/bin/bash

nowdate=$(date "+%Y-%m-%d %H:%M:%S")
echo "Today is ${nowdate}, cron daemon execution complete." >> /home/ec2-user/rsync_scripts/crond_execution.log

echo 'rsync log ...'
rsync -avzh /srv/my-server/logs/ /efs-my-folder/logs/

크론 데몬(cron daemon)이 실행 될 때마다 /home/ec2-user/rsync_scripts/crond_execution.log 에 로그를 남기게 된다.

rsync -avzh 옵션으로 /srv/my-server/logs/에 있는 모든 파일들을 /efs-my-folder/logs/ 폴더에 복사한다.

  • 경로에 슬래시(/) 붙여 주어야 경로 아래에 있는 모든 파일이 대상이 된다.
  • 슬래시(/)가 없으면 폴더가 대상이 된다.

rsync 옵션 설명

-v, --verbose: increase verbosity (중간과정을 자세히 보여준다.)
-h --human-readable: output numbers in a human-readable format (출력형태를 사람이 읽을 수 있도록 변경)
-z, --compress: compress file data during the transfer (전송할 때 파일을 압축해서 전송)
-a, --archive: archive mode; equals -rlptgoD (no -H,-A,-X)


스케줄러 설정

Crontab에 대해서 알고 싶다면?

crontab 등록

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command

* * * * * /home/ec2-user/rsync_scripts/rsync_logs

crontab 재실행 및 실행 확인

sudo systemctl restart crond

crontab에 있는 내용을 다시 반영한다.

실행 확인

/var/spool/mail/ec2-user 확인

vim /var/spool/mail/ec2-user

권한 문제가 있는 것을 확인하였다.

sudo chmod +x ~/rsync_scripts/rsync_logs.sh

정상실행 확인


> cat crond_execution.log
Today is 2022-07-26 06:47:01, cron daemon execution complete.
Today is 2022-07-26 06:48:01, cron daemon execution complete.

실행시간이 추가 되는 것을 확인 할 수 있다.


REFERENCES

rsync(1) - Linux man page
Difference between Cron and Crontab?

profile
36.9 It's good time to start something new

0개의 댓글