인바운드 규칙과 아웃바운드 규칙에 2049 포트를 열어준다.
EFS에서는 2049포트를 사용하기 때문에 해당 규칙을 추가해 주어야 한다.
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 시킨다.
# 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
#!/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/ 폴더에 복사한다.
-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)
# 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
sudo systemctl restart crond
crontab에 있는 내용을 다시 반영한다.
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.
실행시간이 추가 되는 것을 확인 할 수 있다.
rsync(1) - Linux man page
Difference between Cron and Crontab?