LinuxEye - Linux系统教程

LinuxEye - Linux系统教程

当前位置: 主页 > Linux教程 >

Rsync+inotify文件实时同步

时间:2012-11-23 11:59来源:未知 编辑:admin 点击:
Rsync+inotify文件传输同步 目的:把主服务器192.168.2.132上的文件实时同步到备服务器192.168.2.133上。 使用inotify实时监控主服务器/servyou/webroot目录变化,并通过rsync把改变的部分推送给备服
Rsync+inotify文件传输同步
目的:把主服务器192.168.2.132上的文件实时同步到备服务器192.168.2.133上。
使用inotify实时监控主服务器/servyou/webroot目录变化,并通过rsync把改变的部分推送给备服务器。
主备服务器安装rsync
主服务器(数据源)安装inotify

1、ssh认证生成密钥对

# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
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:
c2:81:43:6b:4f:cf:03:60:64:b2:d7:04:b6:65:80:f7 root@servyouhome
一路回车,不做任何修改

2、ssh认证copy公钥到备份服务器192.168.2.133

# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.2.133
输入yes,一路回车
The authenticity of host '192.168.2.133 (192.168.2.133)' can't be established.
RSA key fingerprint is 67:82:61:72:fa:37:06:3f:8e:03:3c:63:ac:1b:d1:d4.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.2.133' (RSA) to the list of known hosts.
root@192.168.2.133's password:
Now try logging into the machine, with "ssh 'root@192.168.2.133'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.

3、主备服务器安装rsync

#wget  http://rsync.samba.org/ftp/rsync/src/rsync-3.0.9.tar.gz
#tar  -zxvf  rsync-3.0.9.tar.gz
#cd  rsync-3.0.9
#./configure
#make
#make  install

4、主备服务器以守护进程启动rsync,并设置开机启动

# rsync  --daemon
#chkconfig  rsync  on

5、主服务器(源数据端)安装inotify

#wget  http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
#tar  -zxvf  inotify-tools-3.14.tar.gz
#cd  inotify-tools-3.14
#./configure
#make
#make  install

6、备服务器配置rsync

#vim  /etc/rsyncd.conf
uid = root
gid = root
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync/lock
log file = /var/log/rsync.log
[webroot]
path = /servyou/webroot/
comment = webroot files
ignore errors
read only = no
write only = no
hosts allow = 192.168.2.0/255.255.255.0
#hosts deny = 0.0.0.0/32
list = false
uid = root
gid = root
#auth users = backup
#secrets file = /etc/server.pass

7、在主服务器配置内容发布节点

#!/bin/bash
SRC=/servyou/webroot/
DES=root@192.168.2.133:/servyou/webroot/
HOST=192.168.2.133
/usr/local/bin/inotifywait  -mrq --timefmt '%Y-%m-%d %H:%M:%S' --format  '%T %w%f%e'  -e modify,close_write,delete,create,attrib $SRC | while read files
     do
         /usr/bin/rsync -azvl --delete --progress --exclude-from=/servyou/backup/exclude.list  $SRC $DES
         echo "${files}  was rsynced" >> /var/log/rsync.log 2>&1
     done
将这个脚本命名为rsync.sh,放到/servyou/目录下,然后指定可执行权限,放到后台运行,如下:
#chmod  755  /servyou/rsync.sh
#nohup   /servyou/rsync.sh  & > /dev/null 2>&1
#echo  “nohup  /servyou/rsync.sh  & ”  >> /etc/rc.local

转载请保留固定链接: https://linuxeye.com/Linux/953.html

------分隔线----------------------------
标签:rsyncinotify文件实时同步
栏目列表
推荐内容