inotify + rsync 实现自动化时时同步-安装版

inotify + rsync 实现自动化时时同步-安装版
  1. 目的
  2. 测试机信息
  3. 安装配置
    • 4.1. 同步机配置
    • 4.2. 被同步机配置
  4. 测试

 

2. 目的

把192.168.75.233下的/data/httpd/example/ 除rsync目录外,其他的目录和文件时时同步至 192.168.75.234的/data/httpd/example/下

3. 测试机信息

同步机: 192.168.75.233 被同步机: 192.168.75.234

4. 安装配置

4.1. 同步机配置

下载 inotify-tools-3.14.tar.gz

安装

tar -zxvf inotify-tools-3.14.tar.gz -C /usr/local/src
cd inotify-tools-3.14
./configure
make && make install

配置同步脚本

cat >/root/server_scripts/inotify_sync.sh<<'EOF'
#!/bin/bash
src=/data/httpd/example/
des=example
host="192.168.75.234"
/usr/local/bin/inotifywait -mrq --exclude=/data/httpd/example/rsync --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e modify,delete,create,attrib $src | while read files
do
     for hostip in $host
     do
        rsync -vzrtopg --delete --progress --password-file=/etc/rsyncd.secrets --exclude=/data/httpd/example/rsync $src root@$hostip::$des
     done
     echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done
EOF

生成rsync同步key文件

echo "111111" >> /etc/rsyncd.secrets
chmod 0600 /etc/rsyncd.secrets

4.2. 被同步机配置

安装rsync 服务

yum install rsync.x86_64

生成rsync同步key文件,秘要必须要与同不断密码一样,且权限为0600

echo "root:111111" >> /etc/rsyncd.secrets
chmod 0600 /etc/rsyncd.secrets

配置rsync 服务

cat >/etc/rsyncd.conf<<"EOF"
uid = root
gid = root
use chroot = no
max connections = 100
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log

[example]
path=/data/httpd/example/
comment = update
ignore errors
read only = no
list = no
hosts allow = 192.168.75.0/255.255.255.0
auth users = root
uid = root
gid = root
secrets file = /etc/rsyncd.secrets
EOF

启动服务

rsync --daemon						//启动rsync服务,默认为873号端口,如果有防火墙,请修改策略,873端口对同步机放行
nohup /bin/bash /root/server_scripts/inotify_sync.sh &	//把同步脚本放至后台运行

5. 测试

5.1 在同步机192.168.75.233下的/data/httpd/example/ 目录下创建一个测试文件、目录、或是修改文件权限,并查看被同步机是否有变更

5.2 在同步机192.168.75.233下的/data/httpd/example/rsync 目录下创建一个测试文件、目录、或是修改文件权限,并查看被同步机是否有变更

至此配置完成

last modify 2011-11-23 18:52:32 Wednesday

你可能感兴趣的:(职场,rsync,inotify,休闲,自动同步)