centos 通过rsync实现远程文件同步

首先准备两台centos服务器分别是服务器A和服务器B,最终实现效果是将A服务器的文件同步到服务器B上。

开始安装

下面是两台服务器共同的操作

首先打开防火墙的873端口,或直接关闭防火前:service iptables stop

安装rsync工具

      yum install rsync

 配置/etc/rsyncd.conf文件文件内容如下:(注意点:hosts allow: 192.168.1.2  这个ip要生成当前主机的真实ip)

     

  1. # /etc/rsyncd: configuration file for rsync daemon mode  
  2.   
  3. # See rsyncd.conf man page for more options.  
  4.   
  5. # configuration example:  
  6.   
  7. uid = 0 
  8. gid = 0
  9. #use chroot = yes  
  10. #list = yes  
  11. #address = 192.161.1.1  
  12. max connections = 10  
  13. pid file = /var/run/rsyncd.pid  
  14. log file = /var/log/rsyncd.log  
  15. # exclude = lost+found/  
  16. # transfer logging = yes  
  17. # timeout = 900  
  18. # ignore nonreadable = yes  
  19. # dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2  
  20.   
  21. [data]  
  22. path=/www/data/  
  23. transfer logging = yes  
  24. ignore errors  
  25. read only = no  
  26. list = no  
  27. hosts allow = 192.168.1.1,192.168.1.2  
  28. auth users = test  
  29. secrets file = /etc/rsync.password  
  30. # [ftp]  
  31. #        path = /home/ftp  
  32. #        comment = ftp export area

启动rsync服务

rsync --daemon

查看服务是否启动,rsync的默认端口是873

netstat -lnt


      首先我们来设置好服务器B:    

[plain]  view plain  copy
  1. [root@VM_205_23_centos ~]# echo "test:test" > /etc/rsync.password  

 
            这里是创建rsync的password文件,内容为用户名:密码,为了安全起见,同时更改下文件权限: 
 

[plain]  view plain  copy
  1. [root@VM_205_23_centos ~]# chmod 600 /etc/rsync.password  

 
       跟着我们来设置好服务器A: 
 

              

             这里仅仅只需要密码,不需要用户了,免得要同步时还要手动互动

[plain]  view plain  copy
  1. [root@VM_19_53_centos etc]# echo "test" > /etc/rsync.password  
  2. [root@VM_19_53_centos etc]# chmod 600 /etc/rsync.password  

 
               此时就可以在服务器A上面创建一个sh脚本来来执行同步了,也可以直接用此命令执行同步,写成sh脚本也只是为了加入定时任务 
 

[plain]  view plain  copy
  1. #!/bin/bash  
  2. rsync -avzP /www/data/ [email protected]::data  --password-file=/etc/rsync.password  















猜你喜欢

转载自blog.csdn.net/fenqing666/article/details/78366795