rsyncでWebコンテンツを同期

LVSでWebサーバを負荷分散できるようにしたので、同じコンテンツをもったWebサーバを作らなければいけない。普通はNFSなどの仕組みで共用ストレージを使うと思うが、ここではバックアップの意味も含めてrsyncでコンテンツのディレクトリをまるごとコピーしてしまう。

①オリジナルサーバ側、コピーサーバ側の両方にrsyncをインストール。多分もともと入ってる。

[root@www1 ~]#yum install rsync
[root@www2 ~]#yum install rsync

②オリジナルサーバ側の設定

[root@www1 ~]# vi /etc/rsyncd.conf
uid = apache
gid = apache

[wwwfiles]
        path = /var/www
        use chroot = no
        read only = no
[root@www1 ~]# cat /etc/hosts.allow
rsync:  192.168.2.5
[root@www1 ~]# chkconfig rsync on

ここまでは単純だったが、コピーサーバ側からrsyncをしてみるとSELinuxで引っかかってしまった。下記の手順で必要なアクセスを許可する。

いったんSELinuxを無効にする

[root@www1 ~]# setenforce 0

コピーサーバ側からrsyncを実行する

[root@www2 ~]# rsync -av --delete www1::wwwfiles /var/www

SELinuxのauditログから許可すべきポリシーを生成する

[root@www1 ~]# cat /var/log/audit/audit.log | grep rsync | audit2allow -M local

ポリシーの内容を確認する

[root@www1 ~]# cat local.te
module local 1.0;

require {
        class dir { getattr read search };
        class file { getattr read };
        type httpd_sys_content_t;
        type httpd_sys_script_exec_t;
        type rsync_t;
        role system_r;
};

allow rsync_t httpd_sys_content_t:dir { getattr read search };
allow rsync_t httpd_sys_content_t:file { getattr read };
allow rsync_t httpd_sys_script_exec_t:dir { getattr read search };
allow rsync_t httpd_sys_script_exec_t:file { getattr read };

ポリシーをインストールする

[root@www1 ~]# semodule -i local.pp

SELinuxを有効にする

[root@www1 ~]# setenforce 1

③crondで自動的に同期をする設定

[root@www2 ~]# vi /etc/cron.d/rsync
30 * * * * root rsync -aq --delete www1::wwwfiles /var/www