Rsync
本文最后更新于 320 天前,如有失效请评论区留言。

Rsync 是一款快速的、开源的、多功能的,可以实现全量和增量的远程、本地的数据同步和数据备份的工具。

Rsync 本地同步

本地使用 Rsync 命令时,Rsync 可以作为 mv 和 cp 命令的替代方法,将文件从发送方同步至接收方。

[root@rsync ~]# rsync -avPz [source] [destination]
-a : 使用递归传输并保持文件的属性
-v : 显示传输过程中的详细信息
-P : 显示文件传输进度
-z : 传输时做压缩,提高传输速度
  • Destination 如果不存在会自动创建。
  • Source/ 表示整个源目录

--delete

默认情况下,Rsync 只确保源目录的所有内容都复制到目标目录。如果源目录和目标目录需要设置成镜像副本,就要用 --delete 参数,用来在目标目录下删除所有源目录里没有的文件。

[root@rsync ~]# rsync -avPz --delete source/ destination

--exclude

在 Source 目录同步文件时如果想排除某些文件或者目录,就要用 --exclude 参数。以下是几个有趣的用法。

a. rsync 默认会同步 Linux 目录下的隐藏文件,可以用 --exclude='.*' 进行排除

[root@rsync ~]# rsync -avPz --exclude '.*' source/ destination

b. 排除目录下的所有文件只保留目录。

[root@rsync ~]# rsync -avPz --exclude 'dir/*' source/ destination

c. 需要排除多个文件/目录。

[root@rsync ~]# rsync -avPz --exclude 'a.txt' --exclude 'b.txt'  source/ destination
[root@rsync ~]# rsync -avPz --exclude={'a.txt', 'b.txt'}  source/ destination
[root@rsync ~]# rsync -avPz --exclude-from='excludefile.txt'  source/ destination
注:excludefile.txt 中写入需要排除的文件,每行一个。

--include

在 Source 目录同步文件时,如果只想保留少许文件或目录,可以用 --exclude 参数先排除所有文件,然后用 --include 参数保留需要同步的文件。

[root@rsync ~]# rsync -avPz --exclude='*' --include='*.txt'  source/ destination
注:上述命令为只同步source目录下的 *.txt 文件。

Rsync 远程同步

基于 SSH 协议

在两台 Linux 上部署 Rsync 。大部分 Linux 系统安装自带 Rsync,没有的话,可以用 yum 命令直接下载。

[root@rsync1 ~]# yum install -y rsync
[root@rsync1 ~]# rpm -qa | grep rsync
rsync-server3.1.3-20.el8_10.x86_64
[root@rsync2 zglt]# yum install -y rsync
[root@rsync2 zglt]# rpm -qa | grep rsync
rsync-server3.1.3-20.el8_10.x86_64

a. 从本地同步文件到远程服务器

[root@rsync1 ~]# rsync -avPz source/ root@192.168.200.200:/destination

b. 从远程服务器同步文件到本地

[root@rsync1 ~]# rsync -avPz  root@192.168.200.200:/source/ /destination

c. -e 参数:Rsync 需要用 -e ssh 指定 SSH 参数

[root@rsync1 ~]# rsync -avPz -e 'ssh -p 18222' source/ root@192.168.200.200:/destination
注:指定 ssh 端口为 18222

基于 Rsync 协议

如果想通过 Rsync 协议来进行同步,需要在两台服务器分别部署 Rsync 服务端和客户端,默认监听 873 端口。

Rsync 服务端部署

在 Linux 上部署 Rsync。大部分 Linux 系统安装自带 Rsync,没有的话,可以用 yum 命令直接下载。

[root@rsync-server ~]# yum install -y rsync
[root@rsync-server ~]# rpm -qa | grep rsync
rsync-server3.1.3-20.el8_10.x86_64

yum 下载 rsync 后服务器上可能没有 rsyncd 相关的配置文件,没关系,自己创建配置文件 rsyncd.conf。

[root@rsync-server ~]# cat > /etc/rsyncd.conf <<\EOF
uid = root
gid = root
port = 873 #端口可以自定义
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup  # Rsync 协议使用的用户名
secrets file = /etc/rsyncauth.passwd # Rsync 协议使用的密码认证文件
log file = /var/logs/rsyncd.log
hosts allow = *
#####################################

[destination1] # 自定义的资源名
comment = welcome to  backup!
path = /data/destination1 #自定义资源对应服务器上的实际路径

[destination2]
comment = welcome to  backup!
path = /data/destination2
EOF

创建密码认证文件 rsyncauth.passwd

[root@rsync-server ~]# cat >/etc/rsyncauth.passwd <<\EOF
rsync_backup:Cuckooyang@2025!
EOF

[root@rsync-server ~]# chmod 600 /etc/rsyncauth.passwd

用 systemctl 管理 Rsync 服务

[root@rsync-server ~]# cat > /etc/sysconfig/rsyncd <<\EOF
OPTIONS=""
EOF

[root@rsync-server ~]# cat > /lib/systemd/system/rsyncd.service <<\EOF
[Unit]
Description=fast remote file copy program daemon
ConditionPathExists=/etc/rsyncd.conf
[Service]
EnvironmentFile=/etc/sysconfig/rsyncd
ExecStart=/usr/bin/rsync --daemon --no-detach "$OPTIONS"
[Install]
WantedBy=multi-user.target
EOF

加入开机启动项

[root@rsync-server ~]# systemctl enable --now rsyncd
[root@rsync-server ~]# systemctl status rsyncd

Rsync 客户端部署

在 Linux 上部署 Rsync 。大部分 Linux 系统安装自带 Rsync,没有的话,可以用 yum 命令直接下载。

[root@scrapy-client ~]# yum install -y rsync
[root@scrapy-client ~]# rpm -qa | grep rsync
rsync-server3.1.3-20.el8_10.x86_64

创建密码文件(内容为我们在部署服务端时在 /etc/rsyncauth.passwd 中指定的密码)

[root@rsync-client ~]# cat > /etc/rsyncserver.pass <<\EOF
Cuckooyang@2025!
EOF

Rsync 文件同步

[root@rsync-client ~]# rsync -avzP --progress --delete --port=873 --password-file=/etc/rsyncserver.pass  /source/ rsync_backup@192.168.200.214::destination1
版权声明:除特殊说明,博客文章均为cuckooyang原创,依据CC BY-SA 4.0许可证进行授权,转载请附上出处链接及本声明。 | 博客订阅:RSS | 广告招租:留言板 | 博客VPS |
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇