Ubuntu 20.04에서 Lsyncd를 사용하여 디렉토리를 동기화하는 방법
이 페이지에서
- 전제 조건
- 시작하기\n
- Lsyncd 설치
- 로컬 디렉토리를 동기화하도록 Lsyncd 구성\n
- 원격 디렉토리를 동기화하도록 Lsyncd 구성\n
- 결론
Lsyncd는 로컬 디렉터리를 원격 서버의 다른 디렉터리로 미러링할 수 있는 간단하고 가벼운 도구입니다. 지정된 디렉토리에서 몇 초마다 변경 사항을 지속적으로 모니터링하고 변경 사항이 있으면 원격 서버와 동기화합니다. 이 도구는 보안 영역에서 비보안 영역으로 데이터를 동기화하는 데 매우 유용합니다.
이 자습서에서는 Lsyncd를 설치하고 사용하여 로컬 디렉터리와 원격 디렉터리를 동기화하는 방법을 보여줍니다.
전제 조건
- Ubuntu 20.04를 실행하는 서버.\n
- 서버에 루트 암호가 설정되어 있습니다.\n
시작하기
시작하기 전에 시스템 패키지를 최신 버전으로 업데이트하는 것이 좋습니다. 다음 명령을 사용하여 업데이트할 수 있습니다.
apt-get update -y
apt-get upgrade -y
모든 패키지가 업데이트되면 시스템을 다시 시작하여 변경 사항을 적용하십시오.
Lsyncd 설치
기본적으로 Lsyncd 패키지는 대부분의 Linux 운영 체제에서 사용할 수 있습니다. 다음 명령을 실행하여 설치할 수 있습니다.
apt-get install lsyncd -y
Lsyncd가 설치되면 다음 명령을 사용하여 설치된 Lsyncd 버전을 확인할 수 있습니다.
lsyncd --version
다음과 같은 결과가 표시되어야 합니다.
Version: 2.2.3
로컬 디렉터리를 동기화하도록 Lsyncd 구성
이 섹션에서는 /etc/ 디렉토리를 로컬 시스템의 /mnt/ 디렉토리와 동기화하도록 Lsyncd를 구성합니다.
먼저 다음 명령을 사용하여 Lsyncd용 디렉터리를 만듭니다.
mkdir /etc/lsyncd
다음으로 새 Lsyncd 구성 파일을 만들고 동기화하려는 소스 및 대상 디렉터리를 정의합니다.
nano /etc/lsyncd/lsyncd.conf.lua
다음 줄을 추가합니다.
settings { logfile = "/var/log/lsyncd/lsyncd.log", statusFile = "/var/log/lsyncd/lsyncd.status", statusInterval = 20, nodaemon = false } sync { default.rsync, source = "/etc/", target = "/mnt" }
완료되면 파일을 저장하고 닫습니다.
systemctl start lsyncd
systemctl enable lsyncd
다음 명령을 사용하여 Lsyncd 서비스의 상태를 확인할 수도 있습니다.
systemctl status lsyncd
다음 출력이 표시되어야 합니다.
? lsyncd.service - LSB: lsyncd daemon init script Loaded: loaded (/etc/init.d/lsyncd; generated) Active: active (running) since Fri 2020-05-01 03:31:20 UTC; 9s ago Docs: man:systemd-sysv-generator(8) Process: 36946 ExecStart=/etc/init.d/lsyncd start (code=exited, status=0/SUCCESS) Tasks: 2 (limit: 4620) Memory: 12.5M CGroup: /system.slice/lsyncd.service ??36921 /usr/bin/lsyncd -pidfile /var/run/lsyncd.pid /etc/lsyncd/lsyncd.conf.lua ??36952 /usr/bin/lsyncd -pidfile /var/run/lsyncd.pid /etc/lsyncd/lsyncd.conf.lua May 01 03:31:20 ubuntu20 systemd[1]: lsyncd.service: Succeeded. May 01 03:31:20 ubuntu20 systemd[1]: Stopped LSB: lsyncd daemon init script. May 01 03:31:20 ubuntu20 systemd[1]: Starting LSB: lsyncd daemon init script... May 01 03:31:20 ubuntu20 lsyncd[36946]: * Starting synchronization daemon lsyncd May 01 03:31:20 ubuntu20 lsyncd[36951]: 03:31:20 Normal: --- Startup, daemonizing --- May 01 03:31:20 ubuntu20 lsyncd[36946]: ...done. May 01 03:31:20 ubuntu20 systemd[1]: Started LSB: lsyncd daemon init script.
아래와 같이 Lsyncd 로그 파일에서 자세한 내용을 확인할 수 있습니다.
tail -f /var/log/lsyncd/lsyncd.log
다음 출력이 표시되어야 합니다.
/lsyncd/lsyncd.conf.lua Fri May 1 03:30:57 2020 Normal: Finished a list after exitcode: 0 Fri May 1 03:31:20 2020 Normal: --- Startup, daemonizing --- Fri May 1 03:31:20 2020 Normal: recursive startup rsync: /etc/ -> /mnt/ Fri May 1 03:31:20 2020 Normal: Startup of /etc/ -> /mnt/ finished.
다음 명령을 사용하여 동기화 상태를 확인할 수도 있습니다.
tail -f /var/log/lsyncd/lsyncd.status
다음 명령을 사용하여 /mnt 디렉토리의 변경 사항을 볼 수 있어야 합니다.
ls /mnt/
/etc 디렉토리의 모든 파일과 디렉토리가 /mnt 디렉토리에 추가되었는지 확인해야 합니다.
acpi dconf hosts logrotate.conf newt rc2.d subuid- adduser.conf debconf.conf hosts.allow logrotate.d nginx rc3.d sudoers alternatives debian_version hosts.deny lsb-release nsswitch.conf rc4.d sudoers.d apache2 default init lsyncd ntp.conf rc5.d sysctl.conf apparmor deluser.conf init.d ltrace.conf openal rc6.d sysctl.d apparmor.d depmod.d initramfs-tools lvm opt rcS.d systemd apport dhcp inputrc machine-id os-release resolv.conf terminfo apt dnsmasq.d insserv.conf.d magic overlayroot.conf rmt timezone at.deny docker iproute2 magic.mime PackageKit rpc tmpfiles.d bash.bashrc dpkg iscsi mailcap pam.conf rsyslog.conf ubuntu-advantage bash_completion e2scrub.conf issue mailcap.order pam.d rsyslog.d ucf.conf bash_completion.d environment issue.net manpath.config passwd screenrc udev bindresvport.blacklist ethertypes kernel mdadm passwd- securetty ufw binfmt.d fonts kernel-img.conf mime.types perl security update-manager byobu fstab landscape mke2fs.conf php selinux update-motd.d ca-certificates fuse.conf ldap modprobe.d pki sensors3.conf update-notifier ca-certificates.conf fwupd ld.so.cache modules pm sensors.d vdpau_wrapper.cfg calendar gai.conf ld.so.conf modules-load.d polkit-1 services vim console-setup groff ld.so.conf.d mtab pollinate shadow vmware-tools cron.d group legal multipath popularity-contest.conf shadow- vtrgb cron.daily group- letsencrypt multipath.conf profile shells vulkan cron.hourly grub.d libaudit.conf mysql profile.d skel wgetrc cron.monthly gshadow libnl-3 nanorc protocols sos.conf X11 crontab gshadow- locale.alias netplan pulse ssh xattr.conf cron.weekly gss locale.gen network python3 ssl xdg cryptsetup-initramfs hdparm.conf localtime networkd-dispatcher python3.8 subgid zsh_command_not_found crypttab host.conf logcheck NetworkManager rc0.d subgid- dbus-1 hostname login.defs networks rc1.d subuid
원격 디렉터리를 동기화하도록 Lsyncd 구성
이 섹션에서는 로컬 시스템의 /etc/ 디렉토리를 원격 시스템의 /opt/ 디렉토리와 동기화하도록 Lsyncd를 구성합니다.
시작하기 전에 로컬 시스템이 암호 없이 원격 서버에 연결할 수 있도록 로컬 시스템과 원격 서버 간에 SSH 키 기반 인증을 설정해야 합니다.
로컬 시스템에서 다음 명령을 실행하여 공개 및 비공개 키를 생성합니다.
ssh-keygen -t rsa
다음 출력이 표시되어야 합니다.
Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa Your public key has been saved in /root/.ssh/id_rsa.pub The key fingerprint is: SHA256:c7fhjjhAamFjlk6OkKPhsphMnTZQFutWbr5FnQKSJjE The key's randomart image is: +---[RSA 3072]----+ | E .. | | ooo | | oo= + | |=.+ % o . . | | oSo. o | |ooo=B o .o o o | |=o.... o o | |+. o .. o | | . ... . | +----[SHA256]-----+
위의 명령은 ~/.ssh 디렉토리 내에 개인 및 공개 키를 생성합니다.
다음으로 공개 키를 원격 서버에 복사해야 합니다. 다음 명령으로 복사할 수 있습니다.
ssh-copy-id
아래와 같이 원격 루트 사용자의 비밀번호를 입력하라는 메시지가 표시됩니다.
's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh ''" and check to make sure that only the key(s) you wanted were added.
사용자가 인증되면 공개 키가 원격 사용자 authorized_keys 파일에 추가되고 연결이 닫힙니다.
이제 비밀번호를 입력하지 않고도 원격 서버에 로그인할 수 있습니다.
테스트하려면 SSH를 통해 원격 서버에 로그인해 보십시오.
ssh
모든 것이 잘 되었다면 즉시 로그인됩니다.
다음으로 Lsyncd 구성 파일을 편집하고 rsyncssh 및 대상 호스트 변수를 정의해야 합니다.
nano /etc/lsyncd/lsyncd.conf.lua
아래와 같이 파일을 변경합니다.
settings { logfile = "/var/log/lsyncd/lsyncd.log", statusFile = "/var/log/lsyncd/lsyncd.status", statusInterval = 20, nodaemon = false } sync { default.rsyncssh, source = "/etc/", host = "remote-server-ip", targetdir = "/opt" }
완료되면 파일을 저장하고 닫습니다. 그런 다음 Lsyncd 서비스를 다시 시작하여 동기화를 시작하십시오.
systemctl restart lsyncd
다음 명령을 사용하여 동기화 상태를 확인할 수 있습니다.
tail -f /var/log/lsyncd/lsyncd.log
다음 출력이 표시되어야 합니다.
Fri May 1 04:32:05 2020 Normal: --- Startup, daemonizing --- Fri May 1 04:32:05 2020 Normal: recursive startup rsync: /etc/ -> 45.58.38.21:/opt/ Fri May 1 04:32:06 2020 Normal: Startup of "/etc/" finished: 0
다음 명령을 사용하여 원격 서버의 /opt 디렉토리에서 변경 사항을 볼 수 있어야 합니다.
ls /opt
/etc 디렉토리의 모든 파일과 디렉토리가 원격 서버의 /opt 디렉토리에 추가되었는지 확인해야 합니다.
acpi dconf hosts logrotate.conf newt rc2.d subuid- adduser.conf debconf.conf hosts.allow logrotate.d nginx rc3.d sudoers alternatives debian_version hosts.deny lsb-release nsswitch.conf rc4.d sudoers.d apache2 default init lsyncd ntp.conf rc5.d sysctl.conf apparmor deluser.conf init.d ltrace.conf openal rc6.d sysctl.d apparmor.d depmod.d initramfs-tools lvm opt rcS.d systemd apport dhcp inputrc machine-id os-release resolv.conf terminfo apt dnsmasq.d insserv.conf.d magic overlayroot.conf rmt timezone at.deny docker iproute2 magic.mime PackageKit rpc tmpfiles.d bash.bashrc dpkg iscsi mailcap pam.conf rsyslog.conf ubuntu-advantage bash_completion e2scrub.conf issue mailcap.order pam.d rsyslog.d ucf.conf bash_completion.d environment issue.net manpath.config passwd screenrc udev bindresvport.blacklist ethertypes kernel mdadm passwd- securetty ufw binfmt.d fonts kernel-img.conf mime.types perl security update-manager byobu fstab landscape mke2fs.conf php selinux update-motd.d ca-certificates fuse.conf ldap modprobe.d pki sensors3.conf update-notifier ca-certificates.conf fwupd ld.so.cache modules pm sensors.d vdpau_wrapper.cfg calendar gai.conf ld.so.conf modules-load.d polkit-1 services vim console-setup groff ld.so.conf.d mtab pollinate shadow vmware-tools cron.d group legal multipath popularity-contest.conf shadow- vtrgb cron.daily group- letsencrypt multipath.conf profile shells vulkan cron.hourly grub.d libaudit.conf mysql profile.d skel wgetrc cron.monthly gshadow libnl-3 nanorc protocols sos.conf X11 crontab gshadow- locale.alias netplan pulse ssh xattr.conf cron.weekly gss locale.gen network python3 ssl xdg cryptsetup-initramfs hdparm.conf localtime networkd-dispatcher python3.8 subgid zsh_command_not_found crypttab host.conf logcheck NetworkManager rc0.d subgid- dbus-1 hostname login.defs networks rc1.d subuid
결론
위 가이드에서는 로컬 동기화 및 원격 동기화를 위해 Lsyncd를 설치하고 구성하는 방법을 배웠습니다. 이제 프로덕션 환경에서 백업 목적으로 Lsyncd를 사용할 수 있습니다. 궁금한 점이 있으면 언제든지 문의해 주세요.