今天要做两台服务器,作为cdn的源头,两台服务器内容文件要同步.
一台主Master:192.168.1.230
一台备份slave:192.168.1.236
操作系统为centos 5.6 x86_64 .默认安装了rsync
1. 我们只需要在Master上安inotify-tools这个工具
下载地址.
解压,按照INSTALL说明安装好.
2. 在Master上生成key免密码 ssh-keygen -t rsa 生成一对密钥~/.ssh/
3. 把Master主机~/.ssh/id_rsa.pub 拷贝为slave主机的~/.ssh/authorized_keys
4. 特别注意在master和slave上都要把.ssh的目录权限改为700 ,里面的文件改为600.不然还是会失败的.其实把sshd_conf中的StrictModes yes 改为no这个这样就不用改权限也能成功(不建议).
5. ssh 192.168.1.236 无需密码测试成功后.在master上
对于端口目标机器上ssh端口不是22的解决方法是: –e 'ssh –p 222'
#/bin/bashsrc_dir=/var/www/html/dst_ip="192.168.1.236"dst_dir="/home/web/"username="username"/usr/local/bin/inotifywait-mrq--timefmt'%d/%m/%y%H:%M'--format'%T%w%f%e'-emodify,delete,create,attrib$src_dir\|whilereadfilesdoforip_addrin$dst_ipdo/usr/bin/rsync-vzrtopg-e'ssh-p12324'--delete--progress$src_dir$username@$ip_addr:$dst_direcho"${files}wasrsynced">>/tmp/rsync.log2>&1donedone
6.运行脚本,在目录中添加删除文件都可以看到输出内容的.