반응형
우분투에서 lsyncd를 사용하여 디렉토리 간에 변경 사항을 실시간으로 동기화하는 방법
테스트 환경
운영체제 정보 확인
$ lsb_release -d
Description: Ubuntu 22.04.2 LTS
1. lsyncd 설치
lsyncd를 설치합니다.
sudo apt-get update
sudo apt-get install lsyncd
2. lsyncd 설정 파일 작성
/etc/lsyncd 디렉토리 생성
mkdir /etc/lsyncd
/etc/lsyncd/lsyncd.conf.lua 파일을 작성하거나 편집하여 동기화할 디렉토리 및 옵션을 설정합니다.
vim /etc/lsyncd/lsyncd.conf.lua
settings {
logfile = "/var/log/lsyncd/lsyncd.log",
statusFile = "/var/log/lsyncd/lsyncd.status",
inotifyMode = "CloseWrite",
}
sync {
default.rsync,
source = "/tmp/lsyncd/source",
target = "/tmp/lsyncd/destination",
rsync = {
binary = "/usr/bin/rsync",
archive = true,
verbose = true,
},
}
- source: 동기화할 원본 디렉토리 경로
- target: 동기화될 대상 디렉토리 경로
- rsync: Rsync 관련 옵션
더보기
---
시스템 날짜를 받아와 "자동"으로 업데이트
----
-- User configuration file for lsyncd (Dynamic Year Auto-Detect)
----
settings {
logfile = "/var/log/lsyncd/lsyncd.log",
statusFile = "/var/log/lsyncd/lsyncd.status",
statusInterval = 20,
nodaemon = false
}
-- 현재 연도 자동으로 구하기 (예: "2026")
local current_year = os.date("%Y")
local target_host = "192.168.0.157"
local sync_dirs = { "data", "news", "image" }
for _, dir_name in ipairs(sync_dirs) do
local path = "/tmp/www/" .. dir_name .. "/" .. current_year
-- 🌟 [중요] 동기화 시작 전, 로컬(Source) 서버에 올해 연도 폴더가 없으면 자동 생성
os.execute("mkdir -p " .. path)
sync {
default.rsyncssh,
source = path,
host = target_host,
targetdir = path,
delay = 3,
rsync = {
archive = true,
},
}
end
전년도 + 현재년도 동기화 설정
----
-- User configuration file for lsyncd (Full Name Variables)
----
settings {
logfile = "/var/log/lsyncd/lsyncd.log",
statusFile = "/var/log/lsyncd/lsyncd.status",
statusInterval = 20,
nodaemon = false
}
-- 연도 계산을 위한 전체 이름 변수 정의
local current_year_string = os.date("%Y")
local current_year_number = tonumber(current_year_string)
local previous_year_value = tostring(current_year_number - 1)
local current_year_value = tostring(current_year_number)
-- 동기화 대상 정보를 담은 전체 이름 변수 정의
local target_host_ip = "192.168.0.157"
local synchronization_directories = { "data", "news", "image" }
local target_synchronization_years = { previous_year_value, current_year_value }
-- 이중 반복문을 통해 자동으로 sync 블록 생성 (인덱스 변수도 명확하게 작성)
for directory_index, directory_name in ipairs(synchronization_directories) do
for year_index, target_year in ipairs(target_synchronization_years) do
-- 전체 경로 설정을 위한 변수명 구체화
local synchronization_path = "/tmp/www/" .. directory_name .. "/" .. target_year
-- 로컬 원본 디렉토리가 없으면 자동 생성
os.execute("mkdir -p " .. synchronization_path)
sync {
default.rsyncssh,
source = synchronization_path,
host = target_host_ip,
targetdir = synchronization_path,
delay = 3,
rsync = {
archive = true,
},
}
end
end
---
문법 및 초기 rsync 기동 확인
lsyncd -nodaemon /etc/lsyncd/lsyncd.conf.lua
테스트를 위한 source 디렉토리 생성
mkdir -p /tmp/lsyncd/source
로그 및 상태 파일 디렉토리 생성
- lsyncd는 로그 파일과 상태 파일을 저장할 디렉토리를 필요로 합니다.
- 디렉토리를 생성합니다.
mkdir -p /var/log/lsyncd
lsyncd 실행
- 설정 파일을 작성한 후에는 lsyncd를 실행합니다.
systemctl --now enable lsyncd
위 명령은 지정된 설정 파일을 사용하여 lsyncd를 시작합니다.
728x90
3. logrotate lsyncd 설정 파일 작성
lsyncd 파일을 작성하거나 편집합니다.
vim /etc/logrotate.d/lsyncd
/var/log/lsyncd/*.log {
daily
dateext
rotate 7
missingok
notifempty
compress
delaycompress
sharedscripts
postrotate
if [ -f /var/lock/lsyncd ]; then
/etc/init.d/lsyncd restart > /dev/null 2>/dev/null || true
fi
endscript
}
로그로테이션 테스트
- logrotate 설정이 올바른지 확인하려면 다음 명령어로 로그로테이션을 직접 실행해 볼 수 있습니다.
logrotate -vf /etc/logrotate.d/lsyncd
$ logrotate -vf /etc/logrotate.d/lsyncd
reading config file /etc/logrotate.d/lsyncd
Reading state from file: /var/lib/logrotate/status
Allocating hash table for state file, size 64 entries
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Handling 1 logs
rotating pattern: /var/log/lsyncd/*.log forced from command line (7 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/lsyncd/lsyncd.log
Now: 2023-12-01 11:20
Last rotated at 2023-12-01 11:15
log does not need rotating (log is empty)
not running postrotate script, since no logs were rotated
이제 /tmp/lsyncd/source 디렉토리의 변경 사항이 /tmp/lsyncd/destination으로 실시간으로 동기화됩니다.
필요에 따라 설정 파일을 조정하여 더 많은 옵션을 사용하고 특정 요구 사항을 충족시킬 수 있습니다.
참고URL
- ubuntu manuals : lsyncd
- github : Lsyncd - Live Syncing (Mirror) Daemon
728x90
반응형
'리눅스' 카테고리의 다른 글
| Grafana 대시보드를 가져오는 방법(Import dashboard) (0) | 2023.12.05 |
|---|---|
| Nginx에서 map 디렉티브를 사용하는 방법 (0) | 2023.12.05 |
| 리눅스 rev 명령어 사용법 정리 (0) | 2023.11.29 |
| Jenkins 컨테이너에서 Docker를 실행하기 위해 Docker 이미지를 만들기(Jenkins in Docker) (0) | 2023.11.26 |
| Docker Compose를 사용하여 Jenkins를 설치하고 실행하는 방법 (0) | 2023.11.25 |