rsync可以同步按目录同步两台机器的数据文件
主要依赖两个包
inotify-tools-3.13-1.i386.rpm
inotify-tools-devel-3.13-1.i386.rpm
当然也可以安装源码包
两台机器同步依赖SSH信任登录
1。 分别从A和B上登录作为root后运行
#ssh-keygen -t rsa
目的是简单地在/root下创建目录.ssh,当提示输入 passphase时打入两次回车。这将生成下面这两个文件:
/root/.ssh/id_rsa.pub
2。 先测试一下,从A登录作为root,输入
#ssh B
会要求提示
3。 现在从A登录作为root,运行
#scp /root/.ssh/id_rsa.pub root@[B:ip]:/root/.ssh/authorized_keys
文件名必须不能弄错。
4。现在再从A运行
#ssh B
//则不需要密码了,反过来要从B ssh到A,需要在B上执行ssh-keygen -t rsa 命令,然后将生成的文件scp到A上去
实例:
[root@test2 ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
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:
aa:79:22:71:fb:7d:01:cd:2a:c2:49:18:5e:9c:c8:b9 root@test2
[root@test2 ~]# scp /root/.ssh/id_rsa.pub root@test1:/root/.ssh/authorized_keys
root@test1's password:
id_rsa.pub 100% 220 0.2KB/s 00:00
[root@test2 ~]# ssh test1
Last login: Tue Oct 6 11:09:44 2009 from 17.red-88-22-6.staticip.rima-tde.net
[root@test1 ~]#
配置文件脚本
[root@localhost home]# more rsync.sh
#!/bin/sh
#set -x
src="/opt/rose"
des_ip="192.168.100.167 192.168.100.168"
#function
inotify_fun ()
{
/usr/bin/inotifywait -mrq --timefmt '%d/%m/%y-%H:%M' --format '%T %w%f' -e modify,delete,create,move $1|while read time file
do
for ip in $des_ip
do
echo "`date +%Y%m%d-%T`: rsync -avzq --delete --exclude-from=/home/exfile --partial --progress $1 $ip:`dirname $1`"
rsync -avzq --delete --exclude-from=/home/exfile --partial --progress $1 $ip:`dirname $1`
echo
done
done
}
#main
for a in $src
do
inotify_fun $a &
done
--------------------------------------
需要排除同步的文件或文件夹
[root@localhost home]# more /home/exfile
config.properties
logs/ ------文件夹需要加 / 符号
[root@localhost home]# more kill_rsync.sh ---杀进程语句
#!/bin/bash
#若同步目录有增加或减少,请结束所有实时同步进程,然后在开同步脚本,文件在同步的过程中请勿执行
kill -9 `ps -ef|grep -v grep|grep inotifywait |awk '{print $2}'` && kill -9 `ps -ef|grep -v grep|grep rsync| awk '{print $2}' `