0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

lsync、rsyncでのディレクトリ同期 for WordPress

Last updated at Posted at 2022-10-08

WordPressを冗長構成するにあたり、ディレクトリ同期を行う必要があり、試行錯誤の結果、lsycd、rsyncの組み合わせでできたので、やり方をメモしておこうと思います。

Amazon Linux 2 on EC2の環境です。

必要パッケージのインストール

Master側での作業

sudo amazon-linux-extras install -y epel
sudo sed -i -e "s/enabled=1/enabled=0/" /etc/yum.repos.d/epel.repo
sudo yum --enablerepo=epel install -y rsyncd lsyncd

Slave側での作業

sudo amazon-linux-extras install -y epel
sudo sed -i -e "s/enabled=1/enabled=0/" /etc/yum.repos.d/epel.repo
sudo yum --enablerepo=epel install -y xinetd rsyncd

通信するためのSSHキーの作成

Master側でSSHキーを作成する

sudo su
ssh-keygen -t rsa -N ""

Master側で作成されたid_rsa.pubの中身をコピーする

vi /root/.ssh/id_rsa.pub

Slave側にSSHキーをコピー(設置)する

vimでファイルをオープンし、さっきコピーした中身を貼り付ける

vim ~/.ssh/id_rsa.pub

Permissionを変更する

chmod 644 ~/.ssh/id_rsa.pub

Master側でlsyncd.cofの設定をする

/etc/lsyncd.confの設定は以下のとおりです。

lsyncd.conf
settings{
    logfile = "/var/log/lsyncd.log",
    statusFile = "/tmp/lsyncd.stat",
    statusInterval = 1,
}

sync{
  default.rsync,
  source = "/var/test/",
  target = "root@{Slave側のLocal IP}::{Slave側のRsyncのModule}",
  init = false,
  delete="running", 
  exclude = {
    ".*"
  },
  rsync = {
    archive = true,
    links = true,
    update = true,
    verbose = false,
    owner = true,
    group = true
  }
}

Slave側でxinetdとrsyncを設定する

xinetdの設定

sudo vim /etc/xinetd.d/rsync

設定は以下の通り。

# default: off
# description: The rsync server is a good addition to an ftp server, as it \
#   allows crc checksumming etc.
service rsync
{
    disable         = no
    flags           = IPv6
    socket_type     = stream
    wait            = no
    user            = root
    server          = /usr/bin/rsync
    server_args     = --daemon
    log_on_failure  += USERID
}

rsyncの設定

sudo vim /etc/rsyncd.conf

設定は以下の通り。

#--------------
# Global options
#--------------
uid           = root
gid           = root
log file      = /var/log/rsyncd.log
pid file      = /var/run/rsyncd.pid
hosts allow   = xxx.xxx.xxx.xx/32 (<= MasterEC2インスタンスのLocal IP)
hosts deny    = *
dont compress = *.gz *.tgz *.zip *.pdf *.sit *.sitx *.lzh *.bz2 *.jpg *.gif *.png

[wp]
  comment      = rsync server
  path         = /usr/share/nginx/html/xxx
  read only    = false
  exclude      = *.mp
  include      = *.mp30

xinetdを再起動する@Slave

sudo systemctl restart xineted

lsyncdを起動する@Master

sudo systemctl start lsyncd.service

以上でMasterからSlaveへのディレクトリ同期ができるようになっているはずです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?