rsync
1. 简介
rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH、rsync主机同步。
2. rsync特性
- 可以镜像保存整个目录树和文件系统
- 可以很容易做到保持原来文件的权限、时间、软硬链接等等
- 无须特殊权限即可安装
- 快速:第一次同步时rsync会复制全部内容,但在下一次只传输修改过的文件。rsync在传输数据的过程中可以实行压缩及解压缩操作,因此可以使用更少的带宽
- 安全:可以使用scp、ssh等方式来传输文件,当然也可以通过直接的socket连接
- 支持匿名传输,以方便进行网站镜像
3. rsync+inotify
需求:
- 把源服务器上/etc目录实时同步到目标服务器的/tmp/下
在目标服务器上做以下操作:
//关闭防火墙与SELINUX
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# getenforce
Enforcing
[root@localhost ~]# setenforce 0
[root@localhost ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux
//安装rsync服务端软件
[root@localhost ~]# yum -y install rsync rsync-daemon
//设置rsyncd.conf配置文件
[root@localhost ~]# 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
[etc_from_client]
path = /tmp/
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
hosts allow = 192.168.26.142
//创建用户认证文件
[root@localhost ~]# echo 'admin:123456' > /etc/rsync.pass
[root@localhost ~]# cat /etc/rsync.pass
admin:123456
//设置文件权限
[root@localhost ~]# echo 'admin:123456' > /etc/rsync.pass
[root@localhost ~]# cat /etc/rsync.pass
admin:123456
[root@localhost ~]# chmod 600 /etc/rsync*
[root@localhost ~]# ll /etc/rsync*
-rw-------. 1 root root 421 Sep 23 08:41 /etc/rsyncd.conf
-rw-------. 1 root root 13 Sep 23 08:42 /etc/rsync.pass
//启动rsync服务并设置开机自启动
[root@localhost ~]# systemctl enable --now rsyncd.service
Created symlink /etc/systemd/system/multi-user.target.wants/rsyncd.service → /usr/lib/systemd/system/rsyncd.service.
[root@localhost ~]# ss -anlt
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 [::]:*
在源服务器上做以下操作:
//关闭防火墙与SELINUX
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# getenforce
Enforcing
[root@localhost ~]# setenforce 0
[root@localhost ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux
//安装rsync服务端软件,只需要安装,不要启动,不需要配置
[root@localhost ~]# yum -y install rsync
//创建认证密码文件
[root@localhost ~]# echo '123456' > /etc/rsync.pass
//设置文件权限,只设置文件所有者具有读取、写入权限即可
[root@localhost ~]# chmod 600 /etc/rsync.pass
//在源服务器上创建测试目录,然后在源服务器运行以下命令
[root@localhost ~]# mkdir -pv /root/etc/test
mkdir: created directory '/root/etc'
mkdir: created directory '/root/etc/test'
[root@localhost ~]# rsync -avH --port 873 --progress --delete /root/etc/ admin@192.168.26.142::etc_from_client --password-file=/etc/rsync.pass
sending incremental file list
deleting vmware-root_890-2722107963/
deleting vmware-root_887-4013330030/
deleting vmware-root_885-4021784556/
deleting vmware-root_878-2697663918/
deleting .font-unix/
deleting .XIM-unix/
deleting .X11-unix/
deleting .Test-unix/
deleting .ICE-unix/
./
test/
sent 75 bytes received 223 bytes 198.67 bytes/sec
total size is 0 speedup is 0.00
//运行完成后,在目标服务器上查看,在/tmp目录下有test目录,说明数据同步成功
[root@localhost ~]# ls /tmp/
test
//安装inotify-tools工具,实时触发rsync进行同步
//查看服务器内核是否支持inotify
[root@localhost ~]# ll /proc/sys/fs/inotify/
total 0
-rw-r--r--. 1 root root 0 Sep 23 09:38 max_queued_events
-rw-r--r--. 1 root root 0 Sep 23 09:38 max_user_instances
-rw-r--r--. 1 root root 0 Sep 23 09:38 max_user_watches
//如果有这三个max开头的文件则表示服务器内核支持inotify
//安装inotify-tools
//先下载epel
[root@localhost ~]# dnf -y install epel-release
[root@localhost ~]# dnf -y install inotify-tools
//写同步脚本,此步乃最最重要的一步,请慎之又慎。让脚本自动去检测我们制定的目录下 \
//文件发生的变化,然后再执行rsync的命令把它同步到我们的服务器端去
[root@localhost ~]# mkdir /scripts
[root@localhost ~]# touch /scripts/inotify.sh
[root@localhost ~]# chmod 755 /scripts/inotify.sh
[root@localhost ~]# vi /scripts/inotify.sh
[root@localhost ~]# cat /scripts/inotify.sh
host=192.168.26.142
src=/etc
des=etc_from_client
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@localhost ~]# nohup bash /scripts/inotify.sh &
[1] 9975
[root@localhost ~]# nohup: ignoring input and appending output to 'nohup.out'
//在源服务器上生成一个新文件
[root@localhost ~]# echo "hello world" > /etc/abc
//在目标服务器上面进行查看
[root@localhost tmp]# cat /tmp/etc/abc
hello world
设置脚本开机自动启动:
[root@localhost ~]# vi /etc/rc.local
//追加以下内容
nohup bash /scripts/inotify.sh &
[root@localhost ~]# chmod +x /etc/rc.d/rc.local
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~