设为首页 收藏本站
查看: 734|回复: 0

[经验分享] rsync+inotify实现实时同步

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-11-9 08:54:57 | 显示全部楼层 |阅读模式
                      vi /etc/rsyncd.conf                                    
uid=rsync                                    
gid=rsync                                    
hosts allow=192.168.120.0/24               
#hosts deny=0.0.0.0/32                     
use chroot=yes                              
max connections=10                          
pid file=/var/run/rsyncd.pid               
lock file=/var/run/rsync.lock               
log file=/var/log/rsyncd.log               
timeout=600                                
port=873                                 
[backup]
path=/backup/
comment=rsync files
read only=no
list=yes
auth users=rsync_backup
secrets file=/etc/rsyncd/rsyncd.secrets

服务端
echo "rsync_backup:oldboy" > /etc/rsyncd/rsyncd.secrets
useradd rsync -s /sbin/nologin -M
chown -R rsync:rsync /backup

客户端
echo "oldboy" > /etc/rsyncd/rsync.secrets
chmod 600 /etc/rsyncd/rsync.secrets
ls -l /etc/rsyncd/rsync.secrets

服务器:
rsync --daemon --address=192.168.120.144

rsync -avz /data rsync_backup@192.168.120.144::backup/  --password-file=/etc/rsyncd/rsync.secrets

  • 安装inotify


QQ截图20161109085446.jpg
inotify的脚本:
#!/bin/bash
inotify=/usr/local/inotify/bin/inotifywait
$inotify -mrq --format '%w%f' -e create,close_write,delete /data/ \
|while read file
do
  cd / &&
  rsync -az /data --delete rsync_backup@192.168.120.144::backup \
  --password-file=/etc/rsync.secrets
done
思路:inotifywait检测/data/文件的变化,将变化的文件增量发送给服务端
rsync有3种工作方式:
       Local:  rsync [OPTION...] SRC... [DEST]

       Access via remote shell:
         Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]
         Push: rsync [OPTION...] SRC... [USER@]HOST:DEST

       Access via rsync daemon:
         Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST]
               rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]
         Push: rsync [OPTION...] SRC... [USER@]HOST::DEST
               rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST
  • local:有点类似于rm,cp,mv
  • remote shell
  • rsync daemon

    附:
    # rsync - 远程shell模式和rsync守护进程模式
    # rsync作为客户端,有两种工作模式,远程shell模式和rsync守护进程模式
    # 远程shell模式常见语法分别如下:
    # /usr/bin/rsync 是rsync命令的路径。
    # -a, --archive
    # This is equivalent to -rlptgoD. It is a quick way of saying you want recursion and want to preserve almost everything (with -H being a notable omission). The only exception to the above equivalence is when --files-from is specified, in which case -r is not implied.
    # 等价于-rlptgoD。简言之即是想最大限度的保留所有事物(值得注意的是忽略了-H)。仅在指定了--files-from时不等价,此时不执行-r。
    # -e, --rsh=COMMAND
    # This option allows you to choose an alternative remote shell program to use for communication between the local and remote copies of rsync. Typically, rsync is configured to use ssh by default, but you may prefer to use rsh on a local network。
    # 该选项允许选择一个远程shell应用程序用于rsync在本地和远程副本间通讯,通常,rsync默认配置成使用ssh,但是在一个本地网络中也可以使用rsh。
    # /bin/ssh 是本地ssh命令的路径。
    # -i 是ssh命令指定私钥的参数
    # identity_file 是ssh命令使用私钥的路径
    # USER 是登录用的帐户,应当和identity_file对应
    # HOST 是远程的主机
    # /path/to/source/ 是远程的路径
    # /path/to/destination/ 是本地的路径
    /usr/bin/rsync -a -e "/bin/ssh -i identity_file" USER@HOST:/path/to/source/ /path/to/destination/
    # rsync守护进程模式常见语法分别如下:
    # /usr/bin/rsync 是rsync命令的路径。
    # -a, --archive
    # This is equivalent to -rlptgoD. It is a quick way of saying you want recursion and want to preserve almost everything (with -H being a notable omission). The only exception to the above equivalence is when --files-from is specified, in which case -r is not implied.
    # 等价于-rlptgoD。简言之即是想最大限度的保留所有事物(值得注意的是忽略了-H)。仅在指定了--files-from时不等价,此时不执行-r。
    # -u, --update
    # This forces rsync to skip any files which exist on the destination and have a modified time that is newer than the source file. (If an existing destination file has a modification time equal  to the source file's, it will be updated if the sizes are different.)
    # 强制rsync跳过目标上已存在而且比源文件新的任何文件。(如果一个已存在的目标文件的修改时间和源文件相等,则在尺寸不同时更新)
    # -z, --compress
    # With this option, rsync compresses the file data as it is sent to the destination machine, which reduces the amount of data being transmitted -- something that is useful over a slow connection.
    # 使用该选项,rsync在发送到目标设备前压缩文件数据,这将减少传输文件的数量——对于慢速连接有用处
    # HOST 是远程的主机
    # module 是rsync守护进程中的模块名
    # /path/to/destination/ 是本地的路径
    /usr/bin/rsync -auz HOST::module /path/to/destination/
    转自:http://blog.csdn.net/hu_zhenghui/article/details/3811100
  • local:
  • rsync -avz /etc/hosts /tmp
  • #tmp目录下
  • rsync -avz --delete /null/ /tmp/ 让tmp目录和/null的内容保持一致

  • 通过remote shell [拉/推]:
  • rsync -avzP -e'ssh -p 22' /tmp root@10.0.0.1:/opt    推  -e:指定隧道
  • rsync -avzP -e'ssh -p 22' root@10.0.0.1:/tmp/ /opt/  拉 [不包含本身]


                   


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-297801-1-1.html 上篇帖子: svn:unable to connect to a repository at url 下篇帖子: 利用PXE自动安装
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表