rsync特点
Rsync是一款开源的、快速的、多功能的、可实现全量及增量的本地或远程数据同步备份的优秀工具。并且可以不进行改变原有数据的属性信息,实现数据的备份迁移特性。
适用于多种操作平台(unix/linux/windows等)
Rsync是一个快速和非常通用的文件复制工具。它能本地复制,远程复制,或者远程守护进程方式复制
rsync默认运行端口 873端口
Rsync功能
Rsync具备使本地和远程两台主机之间的数据快速复制,远程备份的功能,Rsync命令本身即可实现异地主机复制数据,功能类似scp又优于scp,scp每次都是全量备份,rsync可以实现增量拷贝(和scp一样都是基于ssh服务传输),Rsync软件还支持配置守护进程,实现异机数据复制。
增量复制是Rsync一特点,优于scp,cp 命令
本地数据同步复制,效果如cp
远程数据同步复制,如scp
本地数据删除,如rm
远程数据查看,如ls
后台服务模式,常用,用于实时数据同步,安全性更高
Rsync软件特性
支持拷贝普通文件,特殊文件(link 文件,设备文件)
支持排除指定文件、目录的同步功能(同步数据时,指定文件不同步)
能够保持原有文件所有属性均不变(stat 查看的状态)
实现增量复制(只复制变化的数据,数据传输效率极高)
可以配合ssh、rcp、rsh等方式进行隧道加密文件传输(rsync本身不加密数据)
可以通过socket(进行通信文件)传输文件和数据(c/s架构)
支持匿名用户模式传输+
rsync备份架构
rsync常用语法
命令语法,分几个模式
- 本地模式
rsync 参数 源路径 目标路径
- 远程模式,推送方式,把自己的数据推送到另一台机器上(上传)
语法1
rsync 参数 源路径 user @ip :目标路径
语法2
rsync 参数 源路径 user @ip ::目标路径
- 远程模式,拉取方式,拉取别人机器的数据到自己的机器上(下载)
rsync 参数 user @ip :源路径 目标路径
rsync 参数 user @ip ::源路径目标路径
参数解释
- v 详细模式输出
- a 归档模式,递归的方式传输文件,并保持文件的属性,等同于 - rlptgoD
- r 递归拷贝目录
- l 保留软链接
- p 保留原有权限
- t 保留原有时间(修改)
- g 保留属组权限
- o 保留属主权限
- D 等于
- R 保留相对路径
- H 保留硬链接
- A 保留ACL策略
- e 指定要执行的远程shell命令
- E 保留可执行权限
- X 保留扩展属性信息 a属性
比较常用的组合参数
rsync - avzP
- a 保持文件原有属性
- v 显示传输细节情况
- z 对传输数据压缩传输
- P 显示文件传输的进度信息
本地模式
对机器本身数据的上传下载
1 、下载rsync服务
[root@rsync-41 ~ ]#yum install rsync -y
2 、创建文件,练习拷贝
[root@rsync-41 ~ ]#mkdir /opt/666
[root@rsync-41 ~ ]#touch /opt/666/2022.png
拷贝文件
[root@rsync-41 ~ ]#rsync -avzP /opt/666/2022.png /root
sending incremental file list
2022. png
sent 86 bytes received 35 bytes 242.00 bytes/sec
total size is 0 speedup is 0.00
[root@rsync-41 ~ ]#ls
2022. png anaconda-ks.cfg
生成一个3 G文件,再拷贝大文件时,会特别的占用磁盘IO,占用网络宽带,造成其他程序手影响
[root@rsync-41 ~ ]#dd bs=100M count=30 if =/dev/zero of=/root/2000.png
限速50 M每秒 参数 --bwlimit
[root@rsync-41 ~ ]#rsync -avzP --bwlimit=50 /root/2000.png /opt/666/
sending incremental file list
2000. png
3 ,145 ,728 ,000 100 % 50.94 MB/s 0 :00 :58 (xfr#1, to-chk=0/1)
sent 3 ,059 ,278 bytes received 35 bytes 50 ,567.16 bytes/sec
total size is 3 ,145 ,728 ,000 speedup is 1 ,028.25
[root@rsync-41 ~ ]#ls /opt/666/
2000. png 2022. png
对目录的拷贝
1 、拷贝目录本身
[root@rsync-41 ~ ]#ls
1.l og 2.l og 3.l og anaconda-ks.cfg
[root@rsync-41 ~ ]#rsync -avzP /root /420/
[root@rsync-41 ~ ]#ls /420/
root
[root@rsync-41 ~ ]#ls /420/root/
1.l og 2.l og 3.l og anaconda-ks.cfg
2 、不拷贝目录本身
[root@rsync-41 ~ ]#ls
1.l og 2.l og 3.l og anaconda-ks.cfg
[root@rsync-41 ~ ]#rsync -avzP /root/ /420/
[root@rsync-41 ~ ]#ls -a /420/
. 1.l og 3.l og .bash_history .bash_profile .cshrc .tcshrc
.. 2.l og anaconda-ks.cfg .bash_logout .bashrc .ssh .viminfo
3 、增量拷贝,只拷贝 更新内容
[root@rsync-41 ~ ]#ls /420
1.l og 2.l og 3.l og
[root@rsync-41 ~ ]#ls /421
1.l og 2.l og 3.l og
[root@rsync-41 ~ ]#touch /420/333.png
[root@rsync-41 ~ ]#echo '123456' > /420/1.log
[root@rsync-41 ~ ]#rsync -avzP /420/ /421/
sending incremental file list
./
1.l og
7 100 % 0.00 kB/s 0 :00 :00 (xfr#1, to-chk=5/14)
333. png
0 100 % 0.00 kB/s 0 :00 :00 (xfr#2, to-chk=2/14)
sent 399 bytes received 58 bytes 914.00 bytes/sec
total size is 5 ,026 speedup is 11.00
4 、无差异拷贝 命令--delete 将目标目录数据清空,与源数据一致,是有一点危险的
[root@rsync-41 ~ ]#ls /420
1.l og 2.l og 333. png 3.l og
[root@rsync-41 ~ ]#ls /421
1.l og 2.l og 333. png 3.l og
[root@rsync-41 ~ ]#touch /421/{7..9}.txt
[root@rsync-41 ~ ]#rm -rf /421/333.png
[root@rsync-41 ~ ]#ls /421
1.l og 2.l og 3.l og 7. txt 8. txt 9. txt
[root@rsync-41 ~ ]#rsync -avzP --delete /420/ /421/
sending incremental file list
deleting 9. txt
deleting 8. txt
deleting 7. txt
./
333. png
0 100 % 0.00 kB/s 0 :00 :00 (xfr#1, to-chk=2/14)
sent 352 bytes received 66 bytes 836.00 bytes/sec
total size is 5 ,026 speedup is 12.02
[root@rsync-41 ~ ]#ls /421
1.l og 2.l og 333. png 3.l og
远程模式
1 、准备两台机器,都下载rsync服务
[root@rsync-41 ~ ]#yum install rsync -y
[root@rsync-31 ~ ]#yum install rsync -y
2 、传输目录加目录本身
[root@rsync-41 ~ ]#ls /420
1.l og 2.l og 333. png 3.l og
[root@rsync-41 ~ ]#rsync -avzP /420 root@nfs-31:/root
root@nfs-31' s password:
sending incremental file list
420 /
420 /.bash_history
[root@nfs-31 ~ ]#ls
420 anaconda-ks.cfg
[root@nfs-31 ~ ]#ls ./420/
1.l og 2.l og 333. png 3.l og
3 、不传输目录本身,传输目录下的数据
[root@rsync-41 ~ ]#ls /420
1.l og 2.l og 333. png 3.l og
[root@rsync-41 ~ ]#rsync -avzP /420/ root@nfs-31:/root
root@nfs-31' s password:
sending incremental file list
[root@nfs-31 ~ ]#ls
420 anaconda-ks.cfg
[root@nfs-31 ~ ]#ls
1.l og 2.l og 333. png 3.l og 420 anaconda-ks.cfg
4 、下载
[root@nfs-31 ~ ]#ls
1. png 2. png 3. png anaconda-ks.cfg
[root@rsync-41 ~ ]#rsync -avzP root@nfs-31:/root/ /root
root@nfs-31' s password:
receiving incremental file list
./
1. png
0 100 % 0.00 kB/s 0 :00 :00 (xfr#1, to-chk=5/14)
2. png
0 100 % 0.00 kB/s 0 :00 :00 (xfr#2, to-chk=4/14)
3. png
0 100 % 0.00 kB/s 0 :00 :00 (xfr#3, to-chk=3/14)
sent 85 bytes received 425 bytes 204.00 bytes/sec
total size is 6 ,419 speedup is 12.59
[root@rsync-41 ~ ]#ls
1. png 2. png 3. png anaconda-ks.cfg
rsync服务模式(服务端)
守护进程
rsync默认借助ssh协议进行数据同步
1.采用系统默认用户,如root,不安全,可能对系统造成危害,root密码泄露
2.使用普通用户的话,又导致权限不足
3.因此配置守护进程模式,rsync提供了配置文件,有各种功能
守护进程传输模式是在客户端和服务端之间进行的数据复制。
服务端需要配置守护进程,在客户端执行命令,实现数据拉取和推送。
rsync守护进程模式
1.通过配置文件设置rsync功能
2.rsync命令变了,通过模块名同步指定的文件夹,而不是一个指定的文件夹路径。
修改rsync配置文件(rsync服务端)
uid = www
gid = www
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_user
secrets file = /etc/rsync.passwd
[backup]
comment = 注释信息
path = /backup
[data]
path = /data
EOF
创建用户、数据目录
创建用户
[root@rsync-41 ~ ]#useradd -u 1000 -M -s /sbin/nologin www
[root@rsync-41 ~ ]#id www
uid=1000 (www) gid=1000 (www) groups=1000 (www)
创建数据目录
[root@rsync-41 ~ ]#mkdir /data
[root@rsync-41 ~ ]#mkdir /backup
修改属主属组
[root@rsync-41 ~ ]#chown -R www:www /data/
[root@rsync-41 ~ ]#chown -R www:www /backup/
[root@rsync-41 ~ ]#ll -d /data/
drwxr-xr-x 2 www www 6 Apr 20 16 :50 /data/
[root@rsync-41 ~ ]#ll -d /backup/
drwxr-xr-x 2 www www 6 Apr 20 16 :51 /backup/
创建虚拟用户密码文件与授权
创建虚拟用户密码
[root@rsync-41 ~ ]#echo 'rsync_user:111111' > /etc/rsync.passwd
[root@rsync-41 ~ ]#cat /etc/rsync.passwd
rsync_user:111111
给密码文件授权
[root@rsync-41 ~ ]#chmod 600 /etc/rsync.passwd
[root@rsync-41 ~ ]#ll /etc/rsync.passwd
-rw------- 1 root root 18 Apr 20 16 :55 /etc/rsync.passwd
开机启动,运行rsyncd服务
[root@rsync -41 ~ ]#systemctl start rsyncd
[root@rsync -41 ~ ]#systemctl enable rsyncd
Created symlink from / etc/ systemd/ system / multi- user.target.wants/ rsyncd.service to / usr/ lib/ systemd/ system / rsyncd.service.
[root@rsync -41 ~ ]#systemctl status rsyncd
● rsyncd.service - fast remote file copy program daemon
Loaded: loaded (/ usr/ lib/ systemd/ system / rsyncd.service; enabled; vendor preset: disabled)
Active: active (running ) since Wed 2022 -04 -20 16 :58 :21 CST; 1 min 0 s ago
Main PID: 14926 (rsync)
CGroup: / system.slice/ rsyncd.service
└─14926 / usr/ bin/ rsync
Apr 20 16 :58 :21 rsync-41 systemd[1 ]: Started fast remote file copy progra....
Apr 20 16 :58 :21 rsync-41 systemd[1 ]: Starting fast remote file copy progr....
Apr 20 16 :58 :21 rsync-41 rsyncd[14926 ]: params.c:Parameter () - Ignoring b...s
Apr 20 16 :58 :21 rsync-41 rsyncd[14926 ]: rsyncd version 3.1 .2 starting, li...3
Hint: Some lines were ellipsized, use - l to show in full.
检查端口、进程
[root@rsync-41 ~]
tcp 0 0 0 .0 .0 .0 :873 0 .0 .0 .0 :* LISTEN 14926 /rsync
tcp6 0 0 :::873 :::* LISTEN 14926 /rsync
[root@rsync-41 ~]
root 14926 1 0 16 :58 ? 00 :00 :00 /usr/bin/rsync --daemon --no -detach
root 14960 14013 0 17 :00 pts/1 00 :00 :00 grep --color=auto rsync
[root@rsync-41 ~]
rsync服务模式(客户端)
密码验证的三种方式
1 、交互式,需要手动输入密码
rsync -avzP /tmp/200M.log rsync_user@rsync -41 : :backup
2 、生成密码文件 /etc/rsync.mima,参数--password
echo '111111' > /etc/rsync .mima
rsync -avzP --password-file=/etc/rsync .mima /tmp/200M.log rsync_backup@rsync -41 : :data
如果是脚本,可以去掉vP显示过程的参数
rsync -az --password-file=/etc/my _rsync.pwd /tmp/200M.log rsync_backup@rsync -41 : :data
3 、生成密码变量,让当前系统中存在叫做 RSYNC_PASSWORD 这个变量,以及变量的值,是配置文件中的密码即可
export RSYNC_PASSWORD='111111'
rsync -avzP /tmp/200M.log rsync_backup@rsync -41 : :backup
交叉式
上传数据到backup模块
[root@rsync-41 ~ ]#ls /backup/
[root@rsync-41 ~ ]#
[root@nfs-31 ~ ]#rsync -avzP /root/啦啦啦.log rsync_user@rsync-41::backup
Password:
sending incremental file list
啦啦啦.log
0 100 % 0.00 kB/s 0 :00 :00 (xfr#1, to-chk=0/1)
sent 96 bytes received 43 bytes 30.89 bytes/sec
total size is 0 speedup is 0.00
[root@rsync-41 ~ ]#ls /backup/
啦啦啦.log
下载data模块数据
[root@rsync-41 ~ ]#ls /data/
对对对.png
[root@nfs-31 ~ ]#rsync -avzP rsync_user@rsync-41::data /root
Password:
receiving incremental file list
./
对对对.png
0 100 % 0.00 kB/s 0 :00 :00 (xfr#1, to-chk=0/2)
sent 50 bytes received 135 bytes 41.11 bytes/sec
total size is 0 speedup is 0.00
[root@nfs-31 ~ ]#ls
anaconda-ks.cfg 啦啦啦.log 对对对.png
[root@nfs-31 ~ ]#
生成密码文件
1 、生成密码文件
[root@nfs-31 ~ ]#echo '111111' > /etc/rsync.mima
[root@nfs-31 ~ ]#cat /etc/rsync.mima
111111
2 、降低密码文件的权限,必须是600
3 、上传数据到backup模块
[root@rsync-41 ~ ]#ls /backup/
啦啦啦.log
[root@nfs-31 ~ ]#rsync -avzP --password-file=/etc/rsync.mima /root/对对对.png rsync_user@rsync-41::backup
sending incremental file list
对对对.png
0 100 % 0.00 kB/s 0 :00 :00 (xfr#1, to-chk=0/1)
sent 96 bytes received 43 bytes 278.00 bytes/sec
total size is 0 speedup is 0.00
[root@rsync-41 ~ ]#ls /backup/
啦啦啦.log 对对对.png
4 、下载data模块的数据
[root@rsync-41 ~ ]#touch /data/那个女孩.mp5
[root@rsync-41 ~ ]#ls /data/
对对对.png 那个女孩.mp5
[root@nfs-31 ~ ]#rsync -avzP --password-file=/etc/rsync.mima rsync_user@rsync-41::data /root
receiving incremental file list
./
那个女孩.mp5
0 100 % 0.00 kB/s 0 :00 :00 (xfr#1, to-chk=0/3)
sent 50 bytes received 170 bytes 440.00 bytes/sec
total size is 0 speedup is 0.00
[root@nfs-31 ~ ]#ls
anaconda-ks.cfg 啦啦啦.log 对对对.png 那个女孩.mp5
生成密码变量
1 、设置环境变量
[root@nfs-31 ~ ]#export RSYNC_PASSWORD=111111
2 、上传数据到backup模块
[root@rsync-41 ~ ]#ls /backup/
啦啦啦.log 对对对.png
[root@nfs-31 ~ ]#rsync -avzP /root/偶像剧.mv rsync_user@rsync-41::backup
sending incremental file list
偶像剧.mv
0 100 % 0.00 kB/s 0 :00 :00 (xfr#1, to-chk=0/1)
sent 94 bytes received 43 bytes 274.00 bytes/sec
total size is 0 speedup is 0.00
[root@rsync-41 ~ ]#ls /backup/
偶像剧.mv 啦啦啦.log 对对对.png
3 、下载data模块数据
[root@rsync-41 ~ ]#ls /data/
对对对.png 那个女孩.mp5 鬼迷心窍.txt
[root@nfs-31 ~ ]#ls
anaconda-ks.cfg
[root@nfs-31 ~ ]#rsync -avzP rsync_user@rsync-41::data /root
receiving incremental file list
./
对对对.png
0 100 % 0.00 kB/s 0 :00 :00 (xfr#1, to-chk=2/4)
那个女孩.mp5
0 100 % 0.00 kB/s 0 :00 :00 (xfr#2, to-chk=1/4)
鬼迷心窍.txt
0 100 % 0.00 kB/s 0 :00 :00 (xfr#3, to-chk=0/4)
sent 88 bytes received 271 bytes 718.00 bytes/sec
total size is 0 speedup is 0.00
[root@nfs-31 ~ ]#ls
anaconda-ks.cfg 对对对.png 那个女孩.mp5 鬼迷心窍.txt
配置文件里指定的www用户
当rsyncd运行后,工作进程会以www用户去执行,降低权限
以及接收到的数据,都会被强转为www用户
如上面上传到backup模块的文件,属主属组都是www,而从data 模块下载了的都是该操作用户的属主属组
[root@rsync-41 ~]#ll /backup/
total 0
-rw-r
-rw-r
-rw-r
[root@rsync-41 ~]#ll /data
total 0
-rw-r
-rw-r
-rw-r
[root@nfs-31 ~]#ll
total 4
-rw
-rw-r
-rw-r
-rw-r
报错演示
password file must not be other-accessible
[root@nfs-31 ~]#rsync -avzP
ERROR : password file must not be other-accessible
rsync error : syntax or usage error (code 1 ) at authenticate.c(196 ) [sender=3.1 .2 ]
错误提示,密码文件不得以其他方式访问
要降低客户端密码文件权限为600
chmod 600 /etc/rsync.mima
No route to host
[root@nfs01 tmp]
rsync: failed to connect to 172.16 .1.41 : No route to host (113 )
rsync error: error in socket IO (code 10 ) at clientserver.c(124 ) [sender=3.0 .6 ]
无法连接到 172.16 .1.41 :没有到主机的路由 (113 ),判断是防火墙的缘故
1 .关闭iptables,或者添加规则
iptables -F
systemctl stop firewalld
2 .关闭selinux
[root@nfs01 ~]
[root@rsync01 ~]
ERROR: The remote path must start with a module name not a /
[root@nfs01 tmp]
ERROR: The remote path must start with a module name not a /
rsync error: error starting client-server protocol (code 5 ) at main.c(1503 ) [sender=3.0 .6 ]
原因:客户端命令敲错了(英文意思远程路径必须以模块名称开头,而不是/模块名)
rsync命令语法理解错误,: :/backup 是错误的语法,应该为: :backup (rsync模块)
@ERROR: auth failed on module backup
[root@nfs01 tmp]
Password:
@ERROR : auth failed on module backup
rsync error: error starting client-server protocol (code 5 ) at main.c(1503 ) [sender=3.0 .6 ]
错误中文意思(模块备份时身份验证失败)
1 .密码文件错误/etc/rsync.password
2 .密码文件参数和实际的密码文件名不一致,检查secrets file = /etc/rsync .password
3 .密码文件权限不对 ll /etc/rsync.password 不是600
4 .检查免密文件,是否手误
[root@rsync01 ~]
rsync_backup: chaoge
5 .rsync客户端的密码文件写错,只需要写入密码即可
[root@nfs01 ~]
chaoge
@ERROR: Unknown module 'backup'
[root@nfs01 tmp]# rsync - avz / etc/ hosts rsync_backup@172 .16 .1 .41 ::backup
@ERROR : Unknown module 'backup'
rsync error: error starting client- server protocol (code 5 ) at main.c(1503 ) [sender= 3.0 .6 ]
错误中文意思(未知模块“备份”)
1 、 / etc/ rsyncd.conf配置文件模块名称书写错误
2 、配置文件中网段限制不对
Permission denied
[root@nfs01 tmp]
Password:
sending incremental file list
hosts
rsync: mkstemp ".hosts.5z3AOA" (in backup) failed: Permission denied (13 )
sent 196 bytes received 27 bytes 63.71 bytes /sec
total size is 349 speedup is 1.57
rsync error: some files/attrs were not transferred (see previous errors) (code 23 ) at main.c(1039 ) [sender=3.0 .6 ]
中文意思(权限被拒绝)
1. 共享目录的属主和属组不正确,不是rsync
2. 共享目录的权限不正确,不是755
3. 注意防火墙,selinux的关闭
chdir failed
[root@nfs01 tmp]
Password:
@ERROR: chdir failed
rsync error: error starting client-server protocol (code 5 ) at main.c(1503 ) [sender=3.0 .6 ]
中文意思(失败)
1 . 备份存储目录没有建立
2 . 建立的备份存储目录和配置文件定义不一致
invalid uid rsync
[root@nfs01 tmp]# rsync -avz /etc/hosts rsync_backup@172.16 .1.41 ::backup
Password:
@ERROR : invalid uid rsync
rsync error : error starting client-server protocol (code 5 ) at main.c(1503 ) [sender=3.0 .6 ]
中文意思(无效的uid用户)
rsync服务对应rsync虚拟用户不存在了
Connection refused (111)
[root@yuchao-muban ~]# rsync -avz /etc/hosts rsync_backup@172.16 .1.41 ::backup
rsync: failed to connect to 172.16 .1.41 : Connection refused (111 )
rsync error : error in socket IO (code 10 ) at clientserver.c(124 ) [sender=3.0 .6 ]
中文意思(连接被拒绝)
1 .检查防火墙,selinux的关闭
2 .检查rsync服务端rsyncd是否开启
Rsync服务端排错思路
1、检查rsync服务端的配置文件路径是否正确:/etc/rsyncd.conf
2、查看配置文件的host allow,host deny允许的ip网段是否允许客户端访问
3、查看配置文件中的path参数路径是否存在,权限是否正确(和配置文件的UUID参数对应)
4、查看rsync服务是否启动,端口、进程是否存活
5、查看iptables防火墙、selinux是否允许rsync服务通过,或是关闭
6、查看服务端rsync配置文件的密码文件,权限是否600,格式,语法是否正确,且和配置文件的secrect files参数对应
7、如果是推送数据,要查看配置rsyncd.conf中的用户对该rsync模块下的文件是否可以读取
Rsync客户端排错
1、查看rsync客户端配置的密码文件权限是否600,密码文件格式是否正确,是否和服务端的密码一致
2、尝试telnet连接rsync服务端的873端口,检测服务是否可以连接
3、客户端执行命令语法要检查,细心
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?