rsync

该博客介绍了如何结合rsync和inotify实现文件系统的实时同步,详细阐述了在目标服务器和源服务器上配置rsync、设置Inotify监控、创建认证文件、编写监控脚本并设置开机自启的过程,确保了.runtime目录的改动能够快速同步到目标服务器。

rsync

rsync 具有如下的基本特性:

    可以镜像保存整个目录树和文件系统
    可以很容易做到保持原来文件的权限、时间、软硬链接等
    无须特殊权限即可安装
    优化的流程,文件传输效率高
    可以使用 rsh、ssh 方式来传输文件,当然也可以通过直接的 socket 连接
    支持匿名传输,以方便进行网站镜象
Inotify是一种强大的、细粒度的、异步的文件系统事件监控机制,linux内核从2.6.13起,加入了Inotify支持,通过Inotify可以监控文件系统中添加、删除,修改、移动等各种细微事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools就是这样的一个第三方软件
实例

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

配置方法
目标服务器

关闭防火墙 并且安装rsync

[root@czh ~]# systemctl stop firewalld
[root@czh ~]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@czh ~]# setenforce 0
[root@czh ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux
[root@czh ~]# yum -y install rsync

创建目标文件夹和用户

[root@czh ~]# useradd czh
[root@czh ~]# id czh
uid=1000(czh) gid=1000(czh)=1000(czh)
[root@czh ~]# chmod 777 /home/czh/
[root@czh ~]# ll /home/czh/
总用量 0
[root@czh ~]# ll /home
总用量 0
drwxrwxrwx. 2 czh czh 62 67 20:00 czh

设置/etc/rsyncd.conf的配置文件

[root@czh ~]# vim /etc/rsyncd.conf
[root@czh ~]# 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

[runtime]
path = /home/hf
uid = root
gid = root
port = 873
ignore errors
use chroot = no
read only = no
list = no
max connections = 200
timeout = 600
auth users = admin
hosts allow = 192.168.31.140

创建用户认证文件并且设置权限

[root@czh ~]# echo 'admin:123456' > /etc/rsync.pass
[root@czh ~]# chmod 600 /etc/rsync*
[root@czh ~]# ll /etc/rsync*
-rw-------. 1 root root 323 67 20:03 /etc/rsyncd.conf
-rw-------. 1 root root  13 67 20:05 /etc/rsync.pass

编写.service文件

[root@czh ~]# echo 'OPTIONS=""' > /etc/sysconfig/rsyncd
[root@czh ~]# vim /lib/systemd/system/rsyncd.service 
[root@czh ~]# cat /lib/systemd/system/rsyncd.service 
[Unit]
Description=fast remote file copy program daemon

[Service]
User=root
Group=root
EnvironmentFile=/etc/sysconfig/rsyncd
ExecStart=/usr/bin/rsync --daemon --config=/etc/rsyncd.conf --no-detach
ExecReload=/bin/kill -HUP 
KillMode=process
Restart=on-failure
RestartSec=30s

[Install]
WantedBy=multi-user.target

设置服务开机自启

[root@czh ~]# systemctl start rsyncd.service
[root@czh ~]# systemctl enable rsyncd.service
Created symlink /etc/systemd/system/multi-user.target.wants/rsyncd.service → /usr/lib/systemd/system/rsyncd.service.
[root@czh ~]# systemctl status rsyncd.service
● rsyncd.service - fast remote file copy program daemon
   Loaded: loaded (/usr/lib/systemd/system/rsyncd.service; enabled; vendor preset: >
   Active: active (running) since Mon 2021-06-07 20:07:08 EDT; 22s ago
 Main PID: 222634 (rsync)
    Tasks: 1 (limit: 11201)
   Memory: 276.0K
   CGroup: /system.slice/rsyncd.service
           └─222634 /usr/bin/rsync --daemon --config=/etc/rsyncd.conf --no-detach

607 20:07:08 czh systemd[1]: Started fast remote file copy program daemon.
[root@czh ~]# ss -antl
State    Recv-Q   Send-Q     Local Address:Port       Peer Address:Port   Process   
LISTEN   0        5                0.0.0.0:873             0.0.0.0:*                
LISTEN   0        128              0.0.0.0:22              0.0.0.0:*                
LISTEN   0        5                   [::]:873                [::]:*                
LISTEN   0        128                 [::]:22                 [::]:*  

源服务器

关闭防火墙 安装rsync和inotify-tools

[root@czh ~]# systemctl stop firewalld
[root@czh ~]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@czh ~]# setenforce 0
[root@czh ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux

配置yum源,安装 rsync epel-release 和inotify-tools

 cd /etc/yum.repos.d/
 wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
[root@czh yum.repos.d]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@czh yum.repos.d]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@czh yum.repos.d]# yum -y install epel-release
[root@czh yum.repos.d]#  yum -y install rsync
[root@czh yum.repos.d]# yum -y install inotify-tools

创建认证密码文件并设置权限

[root@czh yum.repos.d]# echo '123456' > /etc/rsync.pass
[root@czh yum.repos.d]# cd
[root@czh ~]# chmod 600 /etc/rsync.pass 
[root@czh ~]# ll /etc/rsync.pass
-rw-------. 1 root root 7 67 20:18 /etc/rsync.pass

编写监控runtime目录并且发生改变时执行rsync的脚本

[root@czh ~]# mkdir /scripts
[root@czh ~]# vim /scripts/inotify.sh
[root@czh ~]# cat /scripts/inotify.sh
host=192.168.31.139
src=/runtime/
des=runtime
password=/etc/rsync.pass
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@czh ~]# chmod 755 /scripts/inotify.sh 
[root@czh ~]# ll /scripts/inotify.sh
-rwxr-xr-x. 1 root root 396 67 20:20 /scripts/inotify.sh

执行脚本

[root@czh ~]# nohup bash /scripts/inotify.sh &
[1] 504197
[root@czh ~]# nohup: 忽略输入并把输出追加到'nohup.out'

设置监控runtime目录脚本并且开机自启

[root@czh ~]# chmod +x /etc/rc.d/rc.local
[root@czh ~]# echo 'nohup /bin/bash /scripts/inotify.sh' >> /etc/rc.d/rc.local
[root@czh ~]# cat /etc/rc.d/rc.local 
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local

nohup /bin/bash /scripts/inotify.sh

测试

[root@czh czh]# ll /home/czh
总用量 0
-rw-r--r--. 1 root root 0 67 20:34 haha
-rw-r--r--. 1 root root 0 67 20:34 hehe
-rw-r--r--. 1 root root 0 67 20:34 xixi

[root@czh ~]# touch /runtime/zzz


[root@czh czh]# ll 
总用量 0
-rw-r--r--. 1 root root 0 67 20:34 haha
-rw-r--r--. 1 root root 0 67 20:34 hehe
-rw-r--r--. 1 root root 0 67 20:34 xixi
-rw-r--r--. 1 root root 0 67 20:43 zzz

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值