多设备文件夹自动同步

liunx 多设备文件夹自动同步

简介:

rsync + inotify 实现实时同步

Rsync is a fast and extraordinarily versatile file copying tool. It can copy locally, to/from another host over any remote shell, or to/from a remote rsync daemon. It offers a large number of options that control every aspect of its behavior and permit very flexible specification of the set of files to be copied. It is famous for its delta-transfer algorithm, which reduces the amount of data sent over the network by sending only the differences between the source files and the existing files in the destination. Rsync is widely used for backups and mirroring and as an improved copy command for everyday use.

server

sudo apt update 
sudo apt install -y rsync 



sudo vim /etc/rsyncd.conf
----
uid = root
gid = root
use chroot = no

#模组
[test]
secrets file = /etc/rsyncd.password     # 密码文件
auth users = cy                  # 用户名
host allow = 10.10.10.0/24              # 链接设备白名单
path = /home/cy/test01/           # 共享路径

list = yes
read only = no
ignore errors

----

sudo vim /etc/rsyncd.passwd
----
[username]:[password]
----

sudo chmod 600 /etc/rsyncd.passwd

service rsync start 

client

sudo apt update 
sudo apt install rsync inotify-tools

# add passwd file 
sudo vim /etc/rsyncd.password
----
[password] 
----

# test 
nc -zv  IP PORT            # 检查端口是否开通

rsync IP::                 # 会输出模组
rsync IP::模组              # 会输出内容  


# exmplate
----
#!/bin/bash

SRC="/home/cy/test/"
MONITOR=modify,create,delete

while true
do
	echo "1"|sudo -S rsync -avzP --delete ${SRC} cy@10.10.10.3::test --password-file=/etc/rsyncd.password
	inotifywait -rme  ${MONITOR} ${SRC} | while read events;
		do
			echo $events
			echo "start rync"
			echo "1"|sudo -S rsync -avzP --delete ${SRC} cy@10.10.10.3::test --password-file=/etc/rsyncd.password 
			MODIFY=`echo "$events" |grep "MODIFY"`
			if [[ ${MODIFY} != "" ]];then
				break
			fi 
		done
done
-----
----

拓展:

Usage: inotifywait [ 选项 ] file1 [ file2 ] [ file3 ] [ ... ]
选项:
    @<file>       排除某些文件.
	--exclude '*.log'     # 屏蔽所有.log结尾的文件
    --excludei <pattern> 大小写不敏感排除指定样式的文件
    -m|--monitor  保持监听状态,如果不设置这个选项,inotifywait只执行一次就会退出。
    -d|--daemon   类似于-m,但是在后台运行,输出事件到特定的文件可以通过 --outfile. 意味着 --syslog.
    -r|--recursive  递归监听目录
    -s|--syslog     发送错误日志到syslog而不是stderr
    -q|--quiet      静态模式,不输出信息
    -qq             什么都不输出
    --timefmt <fmt>  时间格式
    -e|--event <event1> [ -e|--event <event2> ... ]
        监听指定的事件,如果不指定,所有的事件都被监听。
    
  access    访问,读取文件。
  modify    修改,文件内容被修改。
  attrib    属性,文件元数据被修改。
  move  移动,对文件进行移动操作。
  create    创建,生成新文件
  open  打开,对文件进行打开操作。
  close 关闭,对文件进行关闭操作。
  delete    删除,文件被删除。

目前疑问点:

Q: 多文件同时传输,现象怎样?
A: 会排队进行传输
Q: 设置了什么状态监控
A: 创建,删除,修改
Q: 应该怎么部署
A: NAS服务器当一个client / 备份主机当一个 server (支持一对多)   c/s架构
    NAS跑服务来监控文件夹变化进行实时同步
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值