yum install rsync xinetd /usr/bin/rsyncd --daemon chkconfig xinetd on chkconfig rsyncd on service xinetd start
[root@localhost ~]# rsync -av ab/ /tmp/ building file list ... done ./ a b q sent 209 bytes received 92 bytes 602.00 bytes/sec total size is 0 speedup is 0.00 [root@localhost ~]# ll /tmp/ total 12 -rw-r--r-- 1 root root 0 Apr 15 21:21 a -rw-r--r-- 1 root root 0 Apr 15 21:21 b -rw-r--r-- 1 root root 0 Apr 15 21:21 q [root@localhost ~]# rsync -av ab /tmp/ building file list ... done ab/ ab/a ab/b ab/q sent 214 bytes received 92 bytes 612.00 bytes/sec total size is 0 speedup is 0.00 [root@localhost ~]# ll /tmp/ total 20 -rw-r--r-- 1 root root 0 Apr 15 21:21 a drwxr-xr-x 2 root root 4096 Apr 15 21:21 ab
[root@localhost ~]# rsync -av ab/ 172.16.21.1:/tmp The authenticity of host '172.16.21.1 (172.16.21.1)' can't be established. RSA key fingerprint is 80:ca:92:a5:74:42:f8:f8:2c:5c:00:22:a7:44:43:a1. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '172.16.21.1' (RSA) to the list of known hosts. [email protected]'s password: building file list ... done ./ a b q sent 209 bytes received 92 bytes 5.52 bytes/sec total size is 0 speedup is 0.00
3、rsync 列表模式
目标主机A(172.16.21.11)
[root@frank /www/web1/web1]#cat /etc/rsyncd.conf uid = root gid = root use chroot = no max connections = 10 strict modes = yes pid file = /var/run/rsyncd.pid log file = /var/log/rsyncd.log port = 55055 [web1] path = /www/web1 ignore errors read only = no write only = no host allow = 192.168.10.0/24 list = no auth users = web1 secrets file = /etc/web.pwd
向web.pwd中添加用户名密码 操作如下:
chmod 600 /etc/web.pwd
如果是Redhat5.4系统自带的rsync软件 则源主机的文件中不需要加用户名 只添加backup的密码就行
目标主机
源主机
开启rsync服务
/usr/bin/rsync --daemon --config=/etc/rsyncd.conf
源主机B(172.16.21.1)
[root@md01 /opt]#rsync -vzrtopgr --progress --port 55055 --password-file=/etc/server.pass /www/web1 [email protected]::web1 sending incremental file list web1/ web1/04-cquser.sql 25130 100% 0.00kB/s 0:00:00 (xfer#1, to-check=37/39) web1/2013-07-09.tgz
[root@frank /www/web1/web1]#tail -f /var/log/rsyncd.log 2013/07/23 15:46:25 [2589] sent 78 bytes received 122 bytes total size 0 2013/07/23 15:46:53 [2591] params.c:Parameter() - Ignoring badly formed line in configuration file: ignore errors 2013/07/23 15:46:53 [2591] Unknown Parameter encountered: "host allow" 2013/07/23 15:46:53 [2591] IGNORING unknown parameter "host allow" 2013/07/23 15:46:53 [2591] name lookup failed for 192.168.10.75: Name or service not known 2013/07/23 15:46:53 [2591] connect from UNKNOWN (192.168.10.75) 2013/07/23 15:46:54 [2591] rsync to web1/ from web1@UNKNOWN (192.168.10.75) 2013/07/23 15:46:54 [2591] receiving file list 2013/07/23 15:46:54 [2591] web1/ 2013/07/23 15:47:04 [2591] sent 746 bytes received 55987156 bytes total size 89367005
[root@md01 /opt]#ll /proc/sys/fs/inotify/ total 0 -rw-r--r-- 1 root root 0 Jul 23 16:06 max_queued_events -rw-r--r-- 1 root root 0 Jul 23 16:06 max_user_instances -rw-r--r-- 1 root root 0 Jul 23 16:06 max_user_watches
安装innotify-tools
wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz --no-check-certificate tar xf inotify-tools-3.14.tar.gz cd inotify-tools-3.14 ./configure make make install ln -sv /usr/local/lib/libinotify* /usr/lib/ ln -s /usr/local/lib/libinotifytools.so.0 /usr/lib64/libinotifytools.so.0
打开shell1 inotifywait -r --timefmt '%d/%m/%y %H:%M' --format '%T %w %f %e' -e create,delete,modify,close_write -m /www/web1/ 对/www/web1这个目录进行监控 在打开shell2 [root@md01 /www/web1]#rm 04-cquser.sql rm: remove regular file `04-cquser.sql'? y 在shell1中我们可以看到 Setting up watches. Beware: since -r was given, this may take a while! Watches established. 23/07/13 16:09 /www/web1/ 04-cquser.sql DELETE
uid = root gid = root use chroot = no max connections = 10 strict modes = yes pid file = /var/run/rsyncd.pid log file = /var/log/rsyncd.log port = 55055 [web1] path = /www/web1 ignore errors read only = no write only = no host allow = 192.168.10.0/24 list = no auth users = web1 secrets file = /etc/web.pwd
#!/bin/bash host1=192.168.10.16 src=/www/web1 des1=web1 user1=web1 /usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,modify,delete,create,attrib ${src} | while read file do rsync -vzrtopgr --delete --port 55055 --progress ${src} ${user1}@${host1}::${des1} --password-file=/etc/server.pass & echo "${files} was rsynced" >> /tmp/rsync.log 2>&1 echo "---------------------------------------------------------------------------" done
执行脚本 [root@md01 /opt]#bash aa 另开一个终端,在md01上删除文件 [root@md01 /opt]#rm all_back.sql rm: remove regular file `all_back.sql'? y 查看 sending incremental file list web1/ deleting web1/all_back.sql sent 761 bytes received 13 bytes 1548.00 bytes/sec total size is 75770879 speedup is 97895.19 你也可以到host1 上看这个文件是否被删除
chmod 755 /www/web1/inotifyrsync.sh /www/web1/inotifyrsync.sh & echo "/www/web1/inotifyrsync.sh" >> /etc/rc.local