linux上文件实时同步inotify+rsyncd

Linux的上文件实时同步的inotify + rsyncd的


服务器1上的文件需要实时同步到服务器2上(服务器1称为主服务器,服务器2称为客户端)

1:主服务器上制作和发送秘钥
   制作秘钥:
   ssh-keygen(一路回车)
   
   查看秘钥:
   cd~ / .ssh /; ls
   id_rsa id_rsa.pub
   
   发送秘密:
   ssh-copy-id客户端ip地址(默认发送给root用户)
   或者:
   ssh-copy-id www @客户端ip地址(发送给www用户)
   
   验证:(能免秘钥登录就ok)
   ssh客户端ip地址(默认使用root登录)
   或者:
   ssh www @客户端ip地址
   
注意事项:
1:使用秘密登录
   vim / etc / ssh / sshd_config
   PermitRootLogin yes(允许root登录)
   PubkeyAuthentication yes(开启
   
   秘书登录)重启shd服务:systemctl restart sshd

2:主服务器上安装inotify
   tar -zxvf inotify-tools-3.13.tar.gz
   cd inotify-tools-3.13
   ./configure
   make && make install
   ln -s /usr/local/lib/libinotifytools.so.0 / usr / lib64的/ libinotifytools.so.0


3:主服务器和客户端操作
   yum -y重新安装rsync
   systemctl restart rsyncd
   systemctl enable rsyncd
   

4:实时同步脚本:,实现实时同步

cat rsync.sh


#!/斌/庆典

killall rsync
systemctl重启rsyncd

dir = / home / wwwroot /(自动定义,需要同步的文件夹或文件)

而inotifywait -rqq“$ dir”

##某一些不
同步chown www.www -R / home / wwwroot /
rsync -avz -e'ssh -p 22' - 删除“$ dir”--exclude-from“exclude.txt”www @客户端1 -ip:“$ dir”> / dev / null
rsync -avz -e'ssh -p 22' - 删除“$ dir”--exclude-from“exclude.txt”www @客户端2-ip:“$ dir“> / dev / null


##全部
同步#rsync -avz -e'ssh -p 22' - 删除“$ dir”www @客户端1-ip:“$ dir”> / dev / null
#rsync -avz -e'ssh -p 22' - 删除“$ dir”www @客户端2-ip:“$ dir”> / dev / null

DONE

备注:--
exclude-from“exclude.txt”(不需要同步的目录名称或者文件名称)
如果rsync.sh此脚本在/ mnt下,exclude.txt文件也直接写在/ mnt下
(如果exclude.txt在其他路径则写绝对路径列: - exclude-from“/opt/exclude.txt”)

cat exclude.txt(文件或者文件夹名称。换行写,不能写在一行)

缓存
上传
log.txt
证书
kaijiang
newk3.sql


 

猜你喜欢

转载自blog.csdn.net/zzhlinux911218/article/details/85122224
今日推荐