LoginSignup
23
27

More than 5 years have passed since last update.

CentOSでlsyncdを使ってファイルを同期する

Last updated at Posted at 2014-04-17

環境

  • CentOS 7系
  • SELinux

準備

説明のために、同期元のディレクトリを/tmp/originalとして同期先のディレクトリを/tmp/targetとする

インストール

yum --enablerepo=epel install lsyncd
  • lsyncd.conf の編集
cp -ip /etc/lsyncd.conf{,.org}
vi /etc/lsyncd.conf
/etc/lsyncd.conf
settings {
  logfile    = "/var/log/lsyncd/lsyncd.log",
  statusFile = "/var/log/lsyncd/lsyncd.status"
}

sync{
  default.rsync,
  source = "/tmp/origin",
  target = "/tmp/target",
  rsync = {
    archive  = true,
    compress = true
  },
  delete = false
}

詳しくは、作者作成の Manual を参照

/etc/sysconfig/lsyncdの編集(任意)

lsyncdのログレベルを変更するために、起動時のオプションを変更する

cp -ip /etc/sysconfig/lsyncd{,.org}
vi /etc/sysconfig/lsyncd
/etc/sysconfig/lsyncd
LSYNCD_OPTIONS="-log Inotify"

ログファイル用準備

mkdir -m 700 /var/log/lsyncd && chcon -R -u system_u "/var/log/lsyncd"
systemctl restart rsyslog

デーモンに登録

systemctl start lsyncd
systemctl enable lsyncd

トラブル対応

ログは/var/log/lsyncd/lsyncd.logに保存されているので、トラブルがある時は眺めてみる

Error: Terminating since out of inotify watches.

inotifyで監視しているディレクトリ数が上限に達しているのが原因
/etc/sysctl.confを修正しサービスを再起動する。/var/log/lsyncd/lsyncd.statusにinotifyが現在監視しているディレクトリ数が出力されています。

cp -ip /etc/sysctl.conf{,.org}
vi /etc/sysctl.conf
/etc/sysctl.conf
fs.inotify.max_user_watches = 819200
sysctl -p

SELinux関連

CentOS 6系だとSELinux関連のセキュリティ保護が働いていたので、備忘のために。

SELinuxの設定を変更

Red Hat Enterprise Linuxの製品マニュアル第12章 rsyncを参考にした

  1. ラベルの適用

    chcon -R -t public_content_rw_t '/tmp/target'
    
  2. Booleanの変更

    setsebool -P allow_rsync_anon_write on
    

rsync: Permission denied (13)

rsync によるNFSおよびCIFSのエクスポートを許可する(どちらか片方で良いかも…)

  • SELinuxのBooleanを変更をする

    setsebool -P rsync_export_all_ro on
    
  • rsync によるCIFSのエクスポートを許可する

    setsebool -P rsync_use_cifs on
    

参考

  1. lsyncd rsyndの設定方法 | ネットワークチェンジニアとして
  2. lsyncd+rsyncd でサーバー間の自動同期 | ベリーキュート Blog
  3. lsyncdで上限ファイル数を超えた時の対処策 - UNIX的なアレ
  4. lsyncdでミラー元のファイル変更が検知できていなかった | Oreradio.memo
23
27
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
23
27