• rsync入门


    rsync是Linux/unix下一个用于远程文件(目录)同步的一个精巧的小工具程序,有很多文章讨论了其功能和实现原理,本文主要就不赘述了。

    主要介绍下实践时使用的一些方法和细枝末节留作工作笔记以便日后参考。

    大部分的Linux发布包中基本都默认包含了rsync这个小工具,这里就不介绍其安装了。

    使用场景

    用rsync同步程序部署包到单个或多个测试机上。

    模式

    rsync支持6种工作模式,总体划分为pull和push两种。具体参看官方文档 rsync man page,本文的场景适合于push模式。

    过程

    下面以 rsync daemon方式的push模式做说明,其命令模式如下

     Push: rsync [OPTION...] SRC... [USER@]HOST::DEST

     本地机作为client端,远程服务器作为server端,文件从本地push到远程服务器指定目录下。首先需在远程服务器用daemon模式启动rsync,启动前需对rsync的配置文件进行配置。

    配置文件默认为 /etc/rsyncd.conf,若之前没有使用过,/etc目录下默认没有该文件,需新建该文件,如下所示:

    配置文件是由一个或多个模块结构组成。一个模块定义以方括弧中的模块名开始,直到下一个模块定义开始或者文件结束,模块中包含格式为name = value的参数定义

    # 全局参数定义
    uid = root
    gid = root
    use chroot = no
    max connections = 1              #指定该模块的最大并发连接数量以保护服务器,超过限制的连接请求将被告知随后再试。默认值是0,也就是没有限制。
    pid file = /tmp/rsyncd.pid 
    lock file = /tmp/rsyncd.lock
    log file = /tmp/rsyncd.log
    # tmp 模块
    [tmp]
    path = /tmp/rsync
    read only =  no                  #不能只读,因为要向服务器传输文件,必须可写
    list = yes
    hosts allow = 192.168.1.5        #只允许这个ip访问
    hosts deny = 0.0.0.0/32
    auth users = root                #授权用户
    secrets file = /etc/rsyncd.pas   #授权密码  格式:root:123456  可如此生成该文件:echo "root:123456" > /etc/rsyncd.pas; chmod 600 /etc/rsyncd.pas(只有所有者可以读写)
    # vdisk 模块
    [vdisk]
    path = /backup/vdisk
    read only =  no
    list = yes
    hosts allow = 192.168.1.5
    hosts deny = 0.0.0.0/32
    auth users = root
    secrets file = /etc/rsyncd.pas
    

    配置完成后,按如下命令启动rsync daemon服务

    rsync --daemon 或 rsync --daemon --config=/etc/rsyncd.conf
    

    若要停止服务,执行如下命令

    cat /tmp/rsyncd.pid | xargs kill -9 && rm -rf /tmp/rsyncd.pid
    

    完成了服务端配置,并启动服务后在client端执行如下命令进行文件同步:

    rsync -varz --delete --exclude ".*" --progress --password-file=/etc/rsyncd.pas /tmp root@192.168.1.5::tmp
    

    以上命令是将本地/tmp目录同步到远程服务器的tmp模块指定的目录,也就是服务端配置文件中path的位置,如上path = /tmp/rsync

    具体参数选项参考man page,这里需要提一点的是client端的密码文件只需要密码,不需要用户名否则会报如下错误:

    @ERROR: auth failed on module testlink  
    rsync error: error starting client-server protocol (code 5) at main.c(1527) [receiver=3.0.6] 
    

    生成客户端密码文件可用如下脚本命令

    echo "123456" > /etc/rsyncd.pas; chmod 600 /etc/rsyncd.pas
    

      

      

      

  • 相关阅读:
    read、write 与recv、send区别 gethostname
    网络粘包问题解决办法
    C++中 =default 和 =delete 使用
    c++ unordered_map 自定义key
    c++ list的坑
    c++ vector 的坑
    对于RBAC与shiro的一些思考
    求两个数的最大公约数&求N个数的最大公约数
    Nginx是什么?有什么用?
    如何做可靠的分布式锁,Redlock真的可行么
  • 原文地址:https://www.cnblogs.com/weizaiyes/p/9505696.html
Copyright © 2020-2023  润新知