rsync 远程同步

rsync 远程同步

上行同步:客户端同步到服务端
下行同步:服务端同步到客户端

scp
开源的快速备份的工具,一般是系统自带的。
可以在不同主机之间同步整个目录树(目录)
在远程同步的任务中,负责发起rsync的叫做发起端,也就是服务端,负责响应的同步请求的,就是客户端

rsync特点:
1、支持拷贝文件,链接文件等等
2、可以同步整个目录
3、可以支持保留源文件或者目录的权限等等
4、可以实现增量同步

同步方式:
1、完整备份
2、增量备份

常用选项:
1、-a 归档模式 ,保留权限
2、-v 显示同步的详细过程
3、-z 压缩,在传输的过程中对文件进行压缩
4、-H 同步硬链接
5、–delete 同步删除文件 (两边的内容完全一致)
6、-l 同步连接文件
7、 -r 递归,所有

192.168.11.138 服务端
192.168.11.139 客户端

**正向:**上行同步:写入服务端的数据同步到客户端,客户端的数据无法同步到服务端(所有的同步必须是相同目录下)

---------------------------------------------服务端配置-----------------------------------------------
[root@ha02 ~]# vim /etc/rsyncd.conf 
7  uid = root
  8  gid = root
  9  use chroot = yes
 10 #是否禁锢在源目
 11 address 192.168.11.138
 12 #监听地址
 13 port 873   #可写可不写
 14 # max connections = 4
 15 pid file = /var/run/rsyncd.pid
 16 log file = /var/log.rsyncd.log
 17 hosts allow = 192.168.11.0/24
  18 #允许访问的客户端ip地址
   19 dont compress = *.gz *.bz2 *.tgz *.zip *.rar *.z
 20 #在传输过程中,不再进行压缩的文件类型
  21 [test]
 22 #共享模块名称,后续通过模块名称进行同步
 23 path =/opt/test
 24 #源目录,就是同步的目录
 25 comment = test
 26 #备注信息
  27 read only = no
 28 #源目录,客户端可以度也可以写no 
 29 auth users = backuper
 30 #授权登录的账户
 31 secrets file = /etc/rsyncd_users.db
 32 #授权登录账户的密码文件

vim /etc/rsyncd_users.db    #设置密码文件
   backuper:123456
chmod 600 /etc/rsyncd_users.db
  880  mkdir /opt/test
  881  chmod  777 /opt/test/
  885  systemctl restaart rsyncd
  886  netstat  -antp | grep 873
  887  cd /opt/test/
  888  crontab -e
------------------------------------------客户端------------------------------------------------------
cd /opt/
rsync -a -v -z backuper@192.168.11.138::test /opt
echo "123456" > /etc/server.pass  
 1005  echo "123456" > /etc/server.pass   #将密码写入到
 1006  chmod 600 /etc/server.pass    #赋权
 1007  rsync -a -v -z --password-file=/etc/server.pass backuper@192.168.11.138::test /opt 
#免密登录方式

反向操作:

下行同步:客户端新增的数据同步到服务端(必须是相同目录下)

inotify-tools
inotify 实现监控
inotify watch 监控文件系统的变化
inotify wait 监控修改,创建,移动,删除,属性修改(权限,所有者,所属组)如果有变动,立即输出结果


tar -xf inotify-tools-3.14.tar.gz
yum -y install make
cd inotify-tools-3.14/
./configure 
make -j 4 && make
mkdir xy102
chmod 777 xy102
cd xy102/
yum install -y inotify-tools
vim inotify.sh

#!/bin/bash
inotify_cmd="inotifywait -mrq -e modify,create,move,delete,attrib /opt/xy102"
rsync_cmd="rsync -azH --delete --password-file=/etc/server.pass /opt/xy102 backuper@192.168.11.138::test/"

$inotify_cmd | while read DIRECTORY EVENT FILE
do
  if [ $(pgrep rsync | wc -l ) -le 0  ]
 then
    $rsync_cmd
  fi
done

#解析  
# inotifywait -mrq -e modify,create,move,delete /opt/xy102
# m 持续监控
# r 递归整个目录 只要变化包含子目录的变化全部记录
# q 简化输出信息
# e 指定监控的事件,


chmod 777 inotify.sh 
./inotify.sh
  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码要你命

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值