Centos 7系统rsync远程同步

环境

服务器A:(rsync同步源)IP 192.168.2.168

服务器B:(rsync客户端)IP 192.168.2.225

服务器A    #【下行同步 】

1、安装rsync文件备份工具

[root@localhost ~]# yum -y install rsync

2、设置防火墙 ,允许TCP协议 873端口

[root@localhost ~]# firewall-cmd --zone=public --add-port=873/tcp --permanent

3、建立、/etc/rsyncd.conf配置文件

[root@localhost ~]# vim /etc/rsyncd.conf

uid = nobody
gid = nobody
use chroot = yes  #禁锢在源目录
address = 192.168.2.168   #监听地址
port 873   #监听端口
log file = /var/log/rsyncd.log  #日志文件位置
#pid file = /var/run/rsyncd.pid  #存放进程ID的文件位置
hosts allow = 192.168.2.0/24  #允许访问的客户机地址
[ftp]   #共享模块名称
        path = /ftp  #源目录的实际路径
        comment = www.lxy.com   #名称(随便起)
        read only = yes  #是否为只读
        dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2  #同步时不再压缩的文件类型
        auth users = backuper   #授权账号
        secrets file = /etc/rsyncd_users.db   #存放账号信息的数据文件
4、为备份账号创建数据文件

[root@localhost ~]# vim /etc/rsyncd_users.db
backuper:pwd123  #backuper 备份账号、pwd123是备份账号密码

5、给备份账号目录权限

[root@localhost ~]# chmod 600 /etc/rsyncd_users.db

6、备份用户backuper需要对源目录/ftp、有相应的读取权限

[root@localhost ~]# ls -ld /ftp/
drwxr-xr-x 2 root root 21 12月 16 15:46 /ftp/
[root@localhost ~]# systemctl restart rsyncd   #启动rsync服务
[root@localhost ~]# systemctl enable rsyncd  #设置开机自动启动
[root@localhost ~]# netstat -anpt | grep rsync  #查看873端口
tcp        0      0 192.168.2.168:873       0.0.0.0:*               LISTEN      3689/rsync          
[root@localhost ~]# cd /ftp/  
[root@localhost ftp]# touch 666.txt  #给同步的目录创建一个文件,作为备份的文件
[root@localhost ftp]# vim 666.txt
[root@localhost ftp]# cat 666.txt
ni hao wo shi lxy.com !!!

服务器B


1、安装rsync文件备份工具

[root@localhost ~]# yum -y install rsync

2、建立、/etc/rsyncd.conf配置文件

[root@localhost ~]# vim /etc/rsyncd.conf

uid = nobody
gid = nobody
use chroot = yes
address = 192.168.2.225
port = 873
pid file = /var/run/rsyncd.pid

3、开始同步文件

[root@localhost ~]# rsync -avz backuper@192.168.2.168::ftp /root/  #a表示归档模式、v表示同步过程中显示详细信息、z表示在传输文件时进行压缩、backuper表示账号、192.168.2.168表示备份的服务器是谁、ftp表示备份源的目录 、/root/表示备份到本地root目录下
Password:   #输入密码pwd123
receiving incremental file list
./
666.txt

sent 85 bytes  received 154 bytes  95.60 bytes/sec
total size is 26  speedup is 0.11
[root@localhost ~]# cd /root/
[root@localhost ~]# ls   #备份成功
666.txt

3、设置定时备份

[root@localhost ~]# mkdir /myweb  #设置备份的目录

[root@localhost ~]# vim /etc/server.pass #创建一个密码文件,保存backuper用户的密码

pwd123

[root@localhost ~]# chmod 600 /etc/server.pass #设置权限

[root@localhost ~]# crontab -e  

*/1 * * * * /usr/bin/rsync -avz --delete --password-file=/etc/server.pass backuper@192.168.2.168::ftp /myweb  #每一分钟同步一次、--delete表示源服务没有的文件,就是说目标服务没有的本服务器例如:创建一个目标服务没有的111.txt文件,到一分钟后就会删除111.txt 备份666.txt文件。

[root@localhost ~]# systemctl restart crond  #启动crond服务

[root@localhost ~]# systemctl enable crond #设置开启自动启动

[root@localhost ~]# cd /myweb/ 

[root@localhost myweb]# touch 111.txt

[root@localhost myweb]# ls
111.txt

[root@localhost myweb]# date 
Sun Dec 17 22:26:03 PST 2017
[root@localhost myweb]# ls
666.txt 


服务器A  #【上行同步 】 也叫实时同步    

[root@localhost ~]# vim /etc/rsyncd.conf

uid = nobody
gid = nobody
use chroot = yes
address = 192.168.2.168
port 873
log file = /var/log/rsyncd.log
#pid file = /var/run/rsyncd.pid
hosts allow = 192.168.2.0/24
[ftp]
        path = /ftp
        comment = www.lxy.com
        read only = no    #修改为no 、让ftp目录有写入权限
        dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
        auth users = backuper
        secrets file = /etc/rsyncd_users.db


[root@localhost ~]# chmod 777 /ftp/
[root@localhost ~]# cd /ftp/
[root@localhost ftp]# touch f.txt
[root@localhost ftp]# ls
f.txt  k.txt



服务器B

1、安装gcc-c++

[root@localhost inotify-tools-3.14]# yum -y install gcc-c++    #安装gcc-c++

2、查看inotify内核参数

[root@localhost ~]# cat /proc/sys/fs/inotify/max_queued_events   #表示监控事件队列(16384)
16384
[root@localhost ~]# cat /proc/sys/fs/inotify/max_user_instances   #表示最多监控实例数(128)
128  
[root@localhost ~]# cat /proc/sys/fs/inotify/max_user_watches   #表示每个实例最多监控文件数(8192)
8192
3、安装inotify-tools
[root@localhost ~]# tar zxf inotify-tools-3.14.tar.gz -C /usr/src/   #解压inotify-tools
[root@localhost ~]# cd /usr/src/inotify-tools-3.14/
4、配置
[root@localhost inotify-tools-3.14]# ./configure && make && make install   #编译与编译安装

5、以监控网站目录/ftp/为例

[root@localhost ~]# inotifywait -mrq -e modify,create,move,delete /ftp/ #运行此命令是才开一个终端命令框 、-e表示用来指定要监控哪些事件、-m表示持续监控、-r表示递归整个目录、-q表示简化输出信息、modify表示修改、create表示创建、move表示移动、delete表示删除、attrib表示属性。
/ftp/ CREATE a.txt
/ftp/ CREATE b.txt
/ftp/ DELETE a.txt
/ftp/ CREATE .b.txt.swp
/ftp/ CREATE .b.txt.swx
/ftp/ DELETE .b.txt.swx
/ftp/ DELETE .b.txt.swp
/ftp/ CREATE .b.txt.swp
/ftp/ MODIFY .b.txt.swp
/ftp/ MODIFY .b.txt.swp
/ftp/ CREATE 4913
/ftp/ DELETE 4913
/ftp/ MOVED_FROM b.txt
/ftp/ MOVED_TO b.txt~
/ftp/ CREATE b.txt
/ftp/ MODIFY b.txt
/ftp/ MODIFY .b.txt.swp
/ftp/ DELETE b.txt~
/ftp/ DELETE .b.txt.swp
/ftp/ MOVED_FROM b.txt

[root@localhost ~]# cd /ftp/
[root@localhost ftp]# touch a.txt   #在这里创建、删除、修改、移动,上面的命令框都会监控到
[root@localhost ftp]# touch b.txt
[root@localhost ftp]# rm -rf a.txt
[root@localhost ftp]# vim b.txt
[root@localhost ftp]# mv b.txt /root/

6、编写触发式同步脚本

[root@localhost ~]# vim /opt/inotify_rsync.sh
#!/bin/bash
INOTIFY_CMD='inotifywait -mrq -e modify,create,attrib,move,delete /ftp/'
RSYNC_CMD='rsync -rlvz --delete --password-file=/etc/server.pass /ftp/ backuper@192.168.2.168::ftp'
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
        if [ $(pgrep rsync | wc -l) -le 4 ] ; then
                $RSYNC_CMD
        fi
done
[root@localhost ~]# chmod 777 /opt/inotify_rsync.sh   #给予权限
[root@localhost ~]# chmod 777 /ftp/  #给予权限
[root@localhost ~]# vim /etc/rc.d/rc.local    #把脚本加入到开机自动启动

. /opt/inotify_rsync.sh &

[root@localhost ~]# cd /ftp/ 
[root@localhost ftp]# touch k.txt
[root@localhost ftp]# ls
f.txt  k.txt











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值