• rsync


    rsync

    1. rsync简介

    • sync(同步)

    • rsynclinux系统下的数据镜像备份工具。

    • 使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH(scp)、rsync主机同步。

    • rsync主机:装了rsync的主机

    2. rsync特性

    2.1 rsync支持很多特性:

    • 可以镜像保存整个目录树和文件系统(结构,权限,类型,数据等是相同的)
    • 可以很容易做到保持原来文件的权限、时间、软硬链接等等
    • 无须特殊权限即可安装(普通用户也可使用)
    • 快速:第一次同步时rsync会复制全部内容,但在下一次只传输修改过的文件。rsync在传输数据的过程中可以实行压缩及解压缩操作,因此可以使用更少的带宽,可以在公网上使用
    • 安全:可以使用scpssh等方式来传输文件,当然也可以通过直接的socket连接(套接字:ip+port)
    • 支持匿名传输,以方便进行网站镜像

    3. rsync的ssh认证协议

    3.1 rsync命令来同步系统文件之前要先登录remote主机认证,认证过程中用到的协议有2种:

    • ssh协议
    • rsync协议
    • 注意:
    rsync server端不用启动rsync的daemon进程,只要获取remote host的用户名和密码就可以直接rsync同步文件
    rsync server端因为不用启动daemon进程,所以也不用配置文件/etc/rsyncd.conf
    
    • 1
    • 2

    3.2 ssh认证协议跟scp的原理是一样的,如果在同步过程中不想输入密码就用ssh-keygen -t rsa打通通道

    //这种方式默认是省略了 -e ssh 的,与下面等价:
    rsync -avz /SRC -e ssh root@172.16.12.129:/DEST 
    -a  //文件宿主变化,时间戳不变
    -z  //压缩数据传输
    -v  //显示过程
    
    //当遇到要修改端口的时候,我们可以:
    rsync -avz /SRC -e "ssh -p2222" root@172.16.12.129:/DEST  
    //修改了ssh 协议的端口,默认是22
    
    rsync -avz /SRC root@172.16.12.129:/DEST 
    
    src 源
    dest 目标
    
    rsync常用选项:
        -a, --archive       //归档
        -v, --verbose       //啰嗦模式
        -q, --quiet         //静默模式
        -r, --recursive     //递归
        -p, --perms         //保持原有的权限属性
        -z, --compress      //在传输时压缩,节省带宽,加快传输速度
        --delete            //在源服务器上做的删除操作也会在目标服务器上同步,源上删除,目标上保留
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    4. rsync命令

    //Rsync的命令格式常用的有以下三种:
        rsync [OPTION]... SRC DEST
        rsync [OPTION]... SRC [USER@]HOST:DEST
        rsync [OPTION]... [USER@]HOST:SRC DEST
    
    • 1
    • 2
    • 3
    • 4
    • 命令用法
    //对应于以上三种命令格式,rsync有三种不同的工作模式:
    1)拷贝本地文件。当SRC和DES路径信息都不包含有单个冒号":"分隔符时就启动这种工作模式。如:
    [root@localhost ~]# ls
    anaconda-ks.cfg  nfs.sh
    [root@localhost ~]# rsync -a nfs.sh a.sh
    [root@localhost ~]# ll
    total 12
    -rw-------. 1 root root 1453 Jun 13 19:27 anaconda-ks.cfg
    -rwxr-xr-x  1 root root 1041 Aug  8 18:14 a.sh
    -rwxr-xr-x  1 root root 1041 Aug  8 18:14 nfs.sh
    [root@localhost ~]# ll -i
    total 12
    33574978 -rw-------. 1 root root 1453 Jun 13 19:27 anaconda-ks.cfg
    33574979 -rwxr-xr-x  1 root root 1041 Aug  8 18:14 a.sh
    33574990 -rwxr-xr-x  1 root root 1041 Aug  8 18:14 nfs.sh
    
    2)使用一个远程shell程序(如rsh、ssh)来实现将本地机器的内容拷贝到远程机器。当DST路径地址包 \
    含单个冒号":"分隔符时启动该模式。如:
    [root@localhost ~]# rsync -avz nfs.sh root@172.16.12.129:/root/b.sh
    sending incremental file list
    nfs.sh
    
    sent 643 bytes  received 35 bytes  1,356.00 bytes/sec
    total size is 1,041  speedup is 1.54
    [root@localhost ~]# ssh root@172.16.12.129 'ls -l /root'
    total 8
    -rw-------. 1 root root 1454 Aug  6 04:39 anaconda-ks.cfg
    -rwxr-xr-x  1 root root 1041 Aug  8  2018 b.sh
    
    3)使用一个远程shell程序(如rsh、ssh)来实现将远程机器的内容拷贝到本地机器。当SRC地址路径 \
    包含单个冒号":"分隔符时启动该模式。如:
    [root@localhost ~]# ls
    anaconda-ks.cfg  a.sh  nfs.sh
    [root@localhost ~]# rsync -avz root@172.16.12.129:/etc/yum.repos.d /root/
    receiving incremental file list
    yum.repos.d/
    yum.repos.d/163.repo
    yum.repos.d/redhat.repo
    
    sent 66 bytes  received 918 bytes  1,968.00 bytes/sec
    total size is 1,820  speedup is 1.85
    [root@localhost ~]# ls
    anaconda-ks.cfg  a.sh  nfs.sh  yum.repos.d
    [root@localhost ~]# ls yum.repos.d/
    163.repo  redhat.repo
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    5. rsync+inotify实时同步

    5.1 简介

    5.1.1 rsync
    • rsync远程同步,只能同步小文件(配置文件,图片),同步时有延迟

    • rsync与传统的cptar备份方式相比,rsync具有安全性高、备份迅速、支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,例如定期的备份文件服务器数据到远端服务器,对本地磁盘定期做数据镜像等。

    • rsync同步数据时,需要扫描所有文件后进行比对,进行差量传输。如果文件数量达到了百万甚至千万量级,扫描所有文件将是非常耗时的,正在发生变化的往往是其中很少的一部分

    • rsync不能实时的去监测、同步数据,虽然它可以通过linux守护进程的方式进行触发同步,但是两次触发动作一定会有时间差,这样就导致了服务端和客户端数据可能出现不一致,无法在应用故障时完全的恢复数据

    5.1.2 inotify
    • Inotify是一种强大的、细粒度的、异步的文件系统事件监控机制

    • Inotify可以监控文件系统中添加、删除,修改、移动等各种细微事件

    • 利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools就是这样的一个第三方软件。

    • rsync可以实现触发式的文件同步,但是通过crontab守护进程方式进行触发,同步的数据和实际数据会有差异

    • inotify可以监控文件系统的各种变化,当文件有任何变动时,就触发rsync同步,这样刚好解决了同步数据的实时性问题

    6. 实时同步

    6.1 环境说明

    服务器类型IP地址应用操作系统
    源服务器192.168.232.128rsync inotify-tools 脚本red8.5
    目标服务器192.168.232.134rsync rsync-daemonred8.5
    6.1.1 需求:
    • 把源服务器上/etc目录实时同步到目标服务器的/tmp/下

    6.2 在目标服务器上做以下操作

    关闭防火墙
    [root@dest ~]# systemctl disable --now firewalld
    Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
    Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
    [root@dest ~]# setenforce 0
    [root@dest ~]# vim /etc/selinux/config 
    
    安装rsync
    [root@dest ~]# dnf -y install rsync rsync-daemon
    
    设置rsyncd.conf配置文件
    [root@dest ~]# vim /etc/rsyncd.conf 
    [root@dest ~]# cat /etc/rsyncd.conf
    log file = /var/log/rsyncd.log    # 日志文件位置,启动rsync后自动产生这个文件
    pidfile = /var/run/rsyncd.pid     # pid文件的存放位置
    lock file = /var/run/rsync.lock   # 支持max connections参数的锁文件
    secrets file = /etc/.rsync.pass    # 用户认证配置文件,里面保存用户名称和密码,必须手动创建这个文件
    
    [mushuang]     # 自定义同步名称,需要唯一
    path = /tmp/          # rsync服务端数据存放路径,客户端的数据将同步至此目录
    comment = sync etc from client #说明
    uid = root        # 设置rsync运行权限为root
    gid = root        # 设置rsync运行权限为root
    port = 873        # 默认端口
    ignore errors     # 表示出现错误忽略错误
    use chroot = no       # 默认为true,修改为no,增加对目录文件软连接的备份
    read only = no    # 设置rsync服务端(目标)为读写权限,等于yes就只能读
    list = no     # 不显示rsync服务端资源列表
    max connections = 200     # 最大连接数
    timeout = 600     # 设置超时时间
    auth users = admin        # 执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开,只能使用secrets file中保存的用户
    hosts allow = 172.16.12.128   # 允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
    hosts deny = 192.168.1.1      # 禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
    host可以不指定
    
    创建用户认证文件
    [root@dest ~]# vim /etc/.rsync.pass
    [root@dest ~]# cat /etc/.rsync.pass
    admin:123123
    [root@dest ~]# 
    
    
    设置文件权限
    [root@dest ~]# chmod 600 /etc/.rsync.pass
    [root@dest ~]# ll /etc/.rsync.pass
    -rw-------. 1 root root 13 Aug  9 17:59 /etc/.rsync.pass
    [root@dest ~]# 
    
    
    启动rsync服务并设置开机自启动
    [root@dest ~]# systemctl enable --now rsyncd
    Created symlink /etc/systemd/system/multi-user.target.wants/rsyncd.service 鈫� /usr/lib/systemd/system/rsyncd.service.
    [root@dest ~]# systemctl status rsyncd
    鈼� rsyncd.service - fast remote file copy program daemon
       Loaded: loaded (/usr/lib/systemd/system/rsyncd.service; enab>
       Active: active (running) since Tue 2022-08-09 18:01:16 CST; >
     Main PID: 207329 (rsync)
        Tasks: 1 (limit: 23502)
       Memory: 304.0K
       CGroup: /system.slice/rsyncd.service
               鈹斺攢207329 /usr/bin/rsync --daemon --no-detach
    
    Aug 09 18:01:16 dest systemd[1]: Started fast remote file copy >
    [root@dest ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64

    6.3 在源服务器上做以下操作

    关闭防火墙和selinux
    [root@source ~]# systemctl disable --now firewalld
    Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
    Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
    [root@source ~]# 
    [root@source ~]# setenforce 0
    [root@source ~]# vim /etc/selinux/config 
    [root@source ~]# 
    
    安装rsync服务端软件,只需要安装,不要启动,不需要配置
    [root@source ~]# dnf -y install rsync
    
    创建认证密码文件
    [root@source ~]# vim /etc/.rync.passwd
    [root@source ~]# cat /etc/.rync.passwd
    123123
    [root@source ~]# 
    
    设置文件权限,只设置文件所有者具有读取、写入权限即可
    [root@source ~]# chmod 600 /etc/.rync.passwd
    [root@source ~]# ll /etc/.rync.passwd
    -rw-------. 1 root root 7 Aug  9 18:06 /etc/.rync.passwd
    [root@source ~]# 
    
    在源服务器上创建测试目录,然后在源服务器运行以下命令
    [root@source ~]# mkdir -pv /root/etc/test
    mkdir: created directory '/root/etc'
    mkdir: created directory '/root/etc/test'
    [root@source ~]# rsync -avH --port 873 --progress --delete /root/etc/ admin@192.168.232.134::mushuang --password-file=/etc/.rync.passwd
    sending incremental file list
    deleting vmware-root_999-4248746174/
    deleting vmware-root_992-2999657319/
    deleting vmware-root_1018-2990547707/
    deleting vmware-root_1000-2965972329/
    deleting .font-unix/
    deleting .XIM-unix/
    deleting .X11-unix/
    deleting .Test-unix/
    deleting .ICE-unix/
    ./
    test/
    
    sent 77 bytes  received 225 bytes  28.76 bytes/sec
    total size is 0  speedup is 0.00
    [root@source ~]# 
    运行完成后,在目标服务器上查看,在/tmp目录下有test目录,说明数据同步成功
    [root@source ~]# ls
    anaconda-ks.cfg  etc
    [root@source ~]# touch etc/abc
    [root@source ~]# ls etc/
    abc  test
    [root@source ~]# rm -rf etc/test/
    [root@source ~]# 
    [root@source ~]# rsync -avH --port 873 --progress --delete /root/etc/ admin@192.168.232.134::mushuang --password-file=/etc/.rync.passwd
    sending incremental file list
    deleting test/
    ./
    abc
                  0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=0/2)
    
    sent 116 bytes  received 55 bytes  31.09 bytes/sec
    total size is 0  speedup is 0.00
    [root@source ~]# 
    
    
    [root@dest tmp]# ls
    test
    [root@dest tmp]# ls
    abc
    [root@dest tmp]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70

    6.4 配置实时同步,在源主机

    安装inotify-tools工具,实时触发rsync进行同步
    查看服务器内核是否支持inotify,并安装工具
    [root@source ~]# ll /proc/sys/fs/inotify/
    total 0
    -rw-r--r--. 1 root root 0 Aug  9 18:16 max_queued_events
    -rw-r--r--. 1 root root 0 Aug  9 18:16 max_user_instances
    -rw-r--r--. 1 root root 0 Aug  9 18:16 max_user_watches
    [root@source ~]# yum -y install inotify-tools
    
    
    写同步脚本,此步乃最最重要的一步,请慎之又慎。让脚本自动去检测我们制定的目录下 
    文件发生的变化,然后再执行rsync的命令把它同步到我们的服务器端去
    [root@source ~]# mkdir /scripts
    [root@source ~]# touch /scripts/inotify.sh
    [root@source ~]# chmod 755 /scripts/inotify.sh
    [root@source ~]# ll /scripts/inotify.sh
    -rwxr-xr-x. 1 root root 0 Aug  9 18:19 /scripts/inotify.sh
    [root@source ~]# 
    [root@source ~]# vim /scripts/inotify.sh
    [root@source ~]# cat /scripts/inotify.sh
    host=192.168.232.134    # 目标服务器的ip(备份服务器)
    src=/etc        # 在源服务器上所要监控的备份目录(此处可以自定义,但是要保证存在)
    des=mushuang      # 自定义的模块名,需要与目标服务器上定义的同步名称一致
    password=/etc/.rync.passwd        # 执行数据同步的密码文件
    user=admin          # 执行数据同步的用户名
    
    $inotifywait -mrq --timefmt '%Y%m%d %H:%M' --format '%T %w%f %e' -e modify,delete,create,attrib $src \
    | while read files;do
        rsync -avzP --delete  --timeout=100 --password-file=${password} $src $user@$host::$des
        echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
    done
    [root@source ~]# 
        modify 修改
        delete 删除
        create 创建
        attrib  属性
    
        
    nohup    
    [root@source ~]# sleep 5000 &
    [1] 270162
    [root@source ~]# ps -ef|grep sleep
    root      271831  271814  0 18:39 ?        00:00:00 sleep 1
    root      271833  199799  0 18:39 pts/2    00:00:00 grep --color=auto sleep
    [root@source ~]# 
    关闭终端查看
    [root@source ~]# ps -ef|grep sleep
    root      276421  276391  0 18:40 ?        00:00:00 sleep 1
    root      276428  276400  0 18:40 ?        00:00:00 sleep 1
    root      276471  275887  0 18:40 pts/2    00:00:00 grep --color=auto sleep
    [root@source ~]# 
        
    [root@source ~]# nohup sleep 6000 &
    [1] 282313
    [root@source ~]# nohup: ignoring input and appending output to 'nohup.out'
    
    [root@source ~]# ps -ef|grep sleep
    root      283018  283001  0 18:42 ?        00:00:00 sleep 1
    root      283020  275887  0 18:42 pts/2    00:00:00 grep --color=auto sleep
    [root@source ~]# 
    关闭终端查看
    [root@source ~]# ps -ef|grep sleep    
    root      285769  285752  0 18:43 ?        00:00:00 sleep 1
    root      285771  285080  0 18:43 pts/2    00:00:00 grep --color=auto sleep
    [root@source ~]# 
        
    启动脚本
    [root@source ~]# ll /scripts/inotify.sh
    -rwxr-xr-x. 1 root root 416 Aug  9 18:25 /scripts/inotify.sh
    [root@source ~]# nohup bash /scripts/inotify.sh &
    [1] 290892
    [root@source ~]# nohup: ignoring input and appending output to 'nohup.out'
    
    [root@source ~]# ls
    anaconda-ks.cfg  nohup.out
    [root@source ~]# 
    
    查看    
    [root@dest ~]# cd /tmp/
    [root@dest tmp]# ls
    [root@dest tmp]# 
    
        
    在源服务器上生成一个新文件或者删除
    [root@source ~]# touch /etc/abcd
    [root@source ~]# 
    [root@source ~]# rm -f /etc/abcd
    [root@source ~]# 
    
        
    查看文件    
    [root@dest tmp]# ls
    etc
    [root@dest tmp]# ls etc/abcd 
    etc/abcd
    [root@dest tmp]# ls etc/abcd 
    ls: cannot access 'etc/abcd': No such file or directory
    [root@dest tmp]# 
    
        
    查看inotify生成的日志
    [root@source ~]# tail /tmp/rsync.log 
    20220809 18:46 /etc/abcd CREATE was rsynced
    20220809 18:46 /etc/abcd ATTRIB was rsynced
    20220809 18:48 /etc/abcd DELETE was rsynced
    [root@source ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106

    6.5 设置脚本开机自动启动

    [root@source ~]# vim /etc/rc.local 
    [root@source ~]# head -5 /etc/rc.local
    #!/bin/bash
    
    nohup /bin/bash /scripts/inotify.sh
    
    # THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
    [root@source ~]# 
    
    [root@source ~]# ll /etc/rc.local 
    lrwxrwxrwx. 1 root root 13 Sep 23  2021 /etc/rc.local -> rc.d/rc.local
    [root@source ~]# ll /etc/rc.d/rc.local 
    -rw-r--r--. 1 root root 512 Aug  9 18:53 /etc/rc.d/rc.local
    [root@source ~]# chmod +x /etc/rc.d/rc.local
    [root@source ~]# ll /etc/rc.d/rc.local 
    -rwxr-xr-x. 1 root root 512 Aug  9 18:53 /etc/rc.d/rc.local
    [root@source ~]# 
    
    重启
    [root@source ~]# reboot
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    6.6 到目标服务器上去查看是否把新生成的文件自动传上去了

    [root@source ~]# reboot
    
    连接断开
    连接主机...
    连接主机成功
    Last login: Tue Aug  9 18:43:04 2022 from 192.168.232.1
    [root@source ~]# ps -ef|grep inotify
    root        1068    1064  0 18:56 ?        00:00:00 /bin/bash /scripts/inotify.sh
    root        1069    1068  0 18:56 ?        00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f %e -e modify,delete,create,attrib /etc
    root        1070    1068  0 18:56 ?        00:00:00 /bin/bash /scripts/inotify.sh
    root        2372    1550  0 18:58 pts/1    00:00:00 grep --color=auto inotify
    [root@source ~]# touch /etc/abcde
    [root@source ~]# ls /etc/abcde 
    /etc/abcde
    [root@source ~]# touch /etc/abcde
    [root@source ~]# touch /etc/abc
    [root@source ~]# 
    
    查看
    [root@dest tmp]# ls
    etc
    [root@dest tmp]# ls etc/abc
    etc/abc
    [root@dest tmp]# ls etc/abcde 
    etc/abcde
    [root@dest tmp]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    7. 作业

    • 部署rsync+inotify同步/runtime目录至目标服务器的/NAME/下。这里的NAME是指你的名字,比如你叫tom,则要把/runtime目录同步至目标服务器的/tom/下。

    7.1 在目标主机关闭防火墙并且创建目录

    [root@dest ~]# mkdir /shenyl
    [root@dest ~]# ls /shenyl
    [root@dest ~]# 
    
    • 1
    • 2
    • 3

    7.2 设置rsyncd.conf配置文件

    [root@dest ~]# vim /etc/rsyncd.conf 
    [root@dest ~]# cat /etc/rsyncd.conf
    log file = /var/log/rsyncd.log
    pidfile = /var/run/rsyncd.pid
    lock file = /var/run/rsync.lock
    secrets file = /etc/.rsync.pass
    
    [mushuang]
    path = /shenyl/
    comment = sync etc from client
    uid = root
    gid = root
    port = 873 
    ignore errors
    use chroot = no
    read only = no
    list = no
    max connections = 200 
    timeout = 600
    auth users = admin
    [root@dest ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    7.3 创建用户认证文件,设置文件权限

    [root@dest ~]# cat /etc/.rsync.pass 
    admin:123123
    [root@dest ~]# ll /etc/.rsync.pass
    -rw-------. 1 root root 13 Aug  9 17:59 /etc/.rsync.pass
    [root@dest ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5

    7.4 启动rsync服务并设置开机自启动

    [root@dest ~]# systemctl status rsyncd
    鈼� rsyncd.service - fast remote file copy program daemon
       Loaded: loaded (/usr/lib/systemd/system/rsyncd.service; enab>
       Active: active (running) since Tue 2022-08-09 18:01:16 CST; >
     Main PID: 207329 (rsync)
        Tasks: 1 (limit: 23502)
       Memory: 25.0M
       CGroup: /system.slice/rsyncd.service
               鈹斺攢207329 /usr/bin/rsync --daemon --no-detach
    
    Aug 09 18:01:16 dest systemd[1]: Started fast remote file copy >
    [root@dest ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    7.5 在源主机,生成runtime目录创建认证密码文件

    [root@source ~]# mkdir /runtime
    [root@source ~]# ls /runtime
    [root@source ~]# cd /runtime/
    [root@source runtime]# ls
    [root@source runtime]# cp -r /etc/yum.repos.d/* .
    [root@source runtime]# ls
    CentOS-Base.repo   epel-testing-modular.repo  epel.repo
    epel-modular.repo  epel-testing.repo          redhat.repo
    [root@source runtime]# cd
    [root@source ~]# 
    
    [root@source ~]# cat /etc/.rync.passwd 
    123123
    [root@source ~]# ll /etc/.rync.passwd
    -rw-------. 1 root root 7 Aug  9 18:06 /etc/.rync.passwd
    [root@source ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    7.6 测试同步

    [root@source runtime]# touch abc
    [root@source runtime]# ls
    CentOS-Base.repo   epel-testing-modular.repo  redhat.repo
    abc                epel-testing.repo
    epel-modular.repo  epel.repo
    [root@source runtime]# rm -rf epel-testing-modular.repo 
    [root@source runtime]# ls
    CentOS-Base.repo  epel-modular.repo  epel.repo
    abc               epel-testing.repo  redhat.repo
    [root@source runtime]# rsync -avH --port 873 --progress --delete /runtime/ admin@192.168.232.134::mushuang --password-file=/etc/.rync.passwd
    sending incremental file list
    deleting epel-testing-modular.repo
    ./
    abc
                  0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=4/7)
    
    sent 238 bytes  received 75 bytes  626.00 bytes/sec
    total size is 6,151  speedup is 19.65
    [root@source runtime]# 
    
    查看
    [root@dest shenyl]# ls
    CentOS-Base.repo  epel-modular.repo  epel.repo
    abc               epel-testing.repo  redhat.repo
    [root@dest shenyl]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    7.7 写实时同步脚本,并运行

    [root@source ~]# vim /scripts/inotify.sh 
    [root@source ~]# cat /scripts/inotify.sh
    host=192.168.232.134
    src=/runtime        
    des=mushuang       
    password=/etc/.rync.passwd
    user=admin          
    inotifywait=/usr/bin/inotifywait
    
    $inotifywait -mrq --timefmt '%Y%m%d %H:%M' --format '%T %w%f %e' -e modify,delete,create,attrib $src \
    | while read files;do
        rsync -avzP --delete  --timeout=100 --password-file=${password} $src $user@$host::$des
        echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
    done
    [root@source ~]# 
        
    [root@source runtime]# nohup /scripts/inotify.sh &
    [root@source runtime]# ls
    CentOS-Base.repo   epel.repo  redhat.repo
    epel-testing.repo  nohup.out
    [root@source runtime]# touch abc
    [root@source runtime]# ls
    CentOS-Base.repo  epel-testing.repo  nohup.out
    abc               epel.repo          redhat.repo
    [root@source runtime]#     
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    7.8 重启目标主机的rsync,并查看

    [root@dest shenyl]# systemctl restart rsyncd
    [root@dest shenyl]# ls
    CentOS-Base.repo   epel-testing.repo  redhat.repo
    epel-modular.repo  epel.repo          runtime
    [root@dest shenyl]# ls runtime/
    CentOS-Base.repo   epel.repo  redhat.repo
    epel-testing.repo  nohup.out
    [root@dest shenyl]# ls runtime/
    CentOS-Base.repo  epel-testing.repo  nohup.out
    abc               epel.repo          redhat.repo
    [root@dest shenyl]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
  • 相关阅读:
    python统计秒数
    UML(类图基础)
    基于springboot的在线电子商城系统设计与实现
    【Java】Java核心要点总结:59
    【Go语言】Gin 框架教程
    绩效考核管理项目|记录1
    Jenkins CLI二次开发工具类
    代码随想录 动态规划Ⅳ
    Spring 5 学习整理
    【C++高阶(一)】二叉搜索树深度剖析
  • 原文地址:https://blog.csdn.net/mushuangpanny/article/details/126256805