rsync实现两台服务器上的实时同步--delete

服务端:centos-4 192.168.5.128

客户端:centos-3 192.168.5.132

 

1、创建{1..10}.txt文件在a目录下

[root@centos-4 tmp]# rm -rf a

[root@centos-4 tmp]# mkdir a

[root@centos-4 tmp]# cd a

[root@centos-4 a]# touch {1..10}.txt

[root@centos-4 a]# ls

10.txt  1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt

 

2、同步到远端/tmp/a目录下

因为实在实现不了--password-file的方式同步就做了一个免密码登录

[root@centos-4 a]# ssh-keygen

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:

68:05:51:21:2f:e5:b8:a5:2a:99:5f:23:1a:28:f5:9b root@centos-4

The key's randomart p_w_picpath is:

+--[ RSA 2048]----+

|      +o+.       |

|       B         |

|      o =        |

|       B         |

|  .   = S        |

| o + o           |

|o = + o          |

|.  = = .         |

|  . E            |

+-----------------+

[root@centos-4 a]# ssh-copy-id centos-3

root@centos-3's password:

Now try logging into the machine, with "ssh 'centos-3'", and check in:

 

  .ssh/authorized_keys

 

to make sure we haven't added extra keys that you weren't expecting.

 

[root@centos-4 a]#

 

配置完私钥后.ssh/下会多了一个authorized_keys 文件

[root@centos-3 .ssh]# cat authorized_keys

ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA1vNA0sHBa4FFnMINAj6FM+FGInAP2+eIQ1JSIXJeRWOfM0KyLS3Nn5LzxvENBPrjJs1RR2x4Lhnuy0YL0Qy+wZLdcEIufZWcpRjDkLAhvgQjF7Mivd7mFhfPJ/uKUB/7Di4V/GzaW2SaG4ZWafFyB8igsBqnfw4wu0NyCVAjlpSe6tR1jao5MRMp8+6nksYHyHH8wafdGglF67cusk5fk9Kd0c4rSoiTjUX4EsREuE7mRBnK6UsSjvnJSsIHKYzDwEdB9ix+flTd9znPgEukWO85WN0h4wjqM8BAURRLDQnxKa0b9m+TrYsJnjr5xemj/Gr3q905ZWdoIDsupDNsLw== root@centos-4

 

同步两台服务器/tmp/a目录

[root@centos-4 a]# rsync -avpz /tmp/a root@centos-3:/tmp/

sending incremental file list

a/

a/1.txt

a/10.txt

a/2.txt

a/3.txt

a/4.txt

a/5.txt

a/6.txt

a/7.txt

a/8.txt

a/9.txt

 

sent 513 bytes  received 206 bytes  479.33 bytes/sec

total size is 0  speedup is 0.00

 

查看客户端的centos-3服务器/tmp/a已经同步

[root@centos-3 tmp]# ls

a                pear                                   zabbix_agentd.log

cml              pulse-gvsEPtT6wF1A                     zabbix_agentd.pid

gconfd-root      pulse-ofsu0uqNK8ia                     zabbix_server.log

hsperfdata_root  sess_9262c75f9c8625064dfabb6f1ffe9b1b  zabbix_server.pid

keyring-8nmh6u   sess_9pdslag3gffnplr3oh0mucvrr6

keyring-c6WZng   yum.log

[root@centos-3 tmp]# cd a

[root@centos-3 a]# ls

10.txt  1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt

[root@centos-3 a]#

 

 

3、删除1.txt文件

[root@centos-4 a]# ls

10.txt  1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt

[root@centos-4 a]# rm -rf 1.txt

[root@centos-4 a]# ls

10.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt

[root@centos-4 a]#

 

4、后面测试,当本地主机少了一个文件。而远端主机没有这个文件,删除远端主机的文件。

[root@centos-4 a]# rsync -avpz --delete /tmp/a root@centos-3:/tmp/

sending incremental file list

a/

deleting a/1.txt##显示删除了1.txt同步两台服务器的目录

 

sent 150 bytes  received 16 bytes  332.00 bytes/sec

total size is 0  speedup is 0.00

[root@centos-4 a]#

 

检查客户端centos-3的目录已经少了1.txt文件,代表已经同步成功

[root@centos-3 a]# ls

10.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt

[root@centos-3 a]#

5、每过1min监听这些目录,当发现本地和远程不一致,就再次同步

写一个shell的脚本执行同步两边的/tmp/a目录

[root@centos-4 ~]# vim rsync.sh

#!/bin/bash

 

rsync -avpz --delete /tmp/a root@centos-3:/tmp/

 

[root@centos-4 ~]# chmod a+x rsync.sh

测试脚本:

[root@centos-4 ~]# cd /tmp/a

[root@centos-4 a]# ls

10.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt

[root@centos-4 a]# rm -rf 2.txt

[root@centos-4 a]# ls

10.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt

[root@centos-4 a]# cd

[root@centos-4 ~]# ./rsync.sh

sending incremental file list

a/

deleting a/2.txt##表示脚本已经生效

 

sent 139 bytes  received 16 bytes  310.00 bytes/sec

total size is 0  speedup is 0.00

[root@centos-4 ~]#

这样做执行脚本是还是会输出信息,所以改了一下

[root@centos-4 ~]# vim rsync.sh

#!/bin/bash

 

rsync -avpz --delete /tmp/a root@centos-3:/tmp/  >>  /dev/null

为了实现实时同步服务端和客户端的/tmp/a目录,配置每1min执行一次命令

[root@centos-4 ~]# crontab -e

 

*/1 * * * * rsync -avpz --delete /tmp/a root@centos-3:/tmp/ >> /dev/null

 

 

crontab: installing new crontab

测试1min后是否可以实时同步目录,删除3.txt

[root@centos-4 a]# rm -rf  3.txt/

[root@centos-4 a]# ls

10.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt

 

过了一分钟后查看centos-3/tmp/a目录下3.txt有没有删除,最后同步

[root@centos-3 a]# ls

10.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt

[root@centos-3 a]#