Linux rsync2019-01-22

一.  rsync只传输变化的数据

完全复制

只传输变化的数据

rsync    -a   归档模式保证权限归属关系等同步不变

rsync    -v  显示详细操作信息

rsync    -z    传输过程中启用压缩/解压

rsync    -n  测试同步过程,同步后会有什么变化,并不会发生修改

rsync   【avz..】 /本地目录1     本地目录2         同步本地目录整个文件夹到本地目录二

rsync   【选项 ..】 /本地目录1/     本地目录2         同步本地目录文件夹下的数据到本地目录二

touch /dir/{1..10}.txt   创建文件

rsync    -avz    --delete     /nsd/      /dir/  源目录可以变化 并且同步,被共享目录修改创建文件不同步      

二.远程同步

虚拟机A:

rm -rf   /op /*

cp   /etc/resolv.conf   /etc/hosts  /opt

ls /opt/

rsync  -avz  --delete /opt /   [email protected]:/opt/

虚拟机B:

ls /opt   查看



三.实时同步

1.前提ssh实现免密码操作验证

* 生成公钥和私钥

#ssh-keygen #一路敲回车

#ls  /root/.ssh

*.传递公钥到对方的服务器

# ssh-copy-id  [email protected]

[email protected]‘s password:

*.验证同步无需密码

rsync  -avz  --delete /opt /   [email protected]:/opt/




四 安装inotify-tools实现监控目录内容变化

步骤一:安装开发工具gcc  make

步骤二:tar解包

]# tar -xf  /tools/inotify-tools-3.13.tar.gz -C /test

]# ls  /test/inotify-tools-3.13/

步骤三: ./configure 配置,指定安装目录/功能模块等选项,还可以检测系统是否安装gcc

]# cd  /test/inotify-tools-3.13/

]# ./configure 

步骤四:make 编译,生成可执行的二进制程序文件

]# cd  /test/inotify-tools-3.13/

]# make

步骤五:make install 安装,将编译好的文件复制到安装目录

]# cd  /test/inotify-tools-3.13/

]# make  install

]# ls /usr/local/bin/inotifywait

/usr/local/bin/inotifywait

• 基本用法

– inotifywait [选项] 目标文件夹

• 常用命令选项

– -m,持续监控(捕获一个事件后不退出)

– -r,递归监控、包括子目录及文件

– -q,减少屏幕输出信息

– -e,指定监视的 modify、move、create、delete、

attrib 等事件类别

书写Shell脚本,实时同步


  for比较适合有次数的循环     

  while比较适合死循环 

  while  [ 条件 ]

  do

        重复执行的代码

  done

[root@svr7 /]# cat /root/rsync.sh

#!/bin/bash

while  inotifywait  -rq  /opt/

do

  rsync -az --delete /opt/  [email protected]:/opt/

done &

[root@svr7 /]# chmod +x /root/rsync.sh

[root@svr7 /]# /root/rsync.sh

[root@svr7 /]# pgrep -l rsync   查看这个进程

[root@svr7 /]# killall  rsync.sh    杀死这个进程

你可能感兴趣的:(Linux rsync2019-01-22)