一、Rsync概述
rsync英文称为 remote synchronizetion,rsync具有可使本地和远程两台主机之间的数据快速复制同步镜像、远程备份的功能,功能类似于ssh带的scp命令,优于scp命令的功能,scp每次都是全量拷贝,而rsync可以增量拷贝。
二、实验准备
本实验模拟Rsync的远程模式(ssh隧道模式),模拟过程中,需要两台虚拟机(一台充当服务端,另一台充当客户端)。
三、实验步骤
3.1 基础操作
①安装rsync服务的包
[root@openEuler1 ~]# dnf install -y rsync
[root@openEuler2 ~]# dnf install -y rsync
②push推操作
#先创建一个文件
[root@openEuler1 ~]# echo " iyuyi.xyz@aliyun.com " > /opt/xyz.txt
[root@openEuler1 ~]# ll /opt/
total 4
-rw-r--r-- 1 root root 23 Jan 25 16:17 xyz.txt
#进行push推操作,相当于上传,由于我配置了免密钥登录,所有在此没有要密码
[root@openEuler1 ~]# rsync -avz /opt/xyz.txt 192.168.126.141:/opt/ #-a表示:递归传输数据,保持文件属性和权限;-v表示:显示详细信息;-z表示:传输是进行压缩提高传输效率
Authorized users only. All activities may be monitored and reported.
sending incremental file list
xyz.txt
sent 131 bytes received 35 bytes 110.67 bytes/sec
total size is 23 speedup is 0.14
#查看结果
[root@openEuler2 ~]# ll /opt/
total 4
-rw-r--r-- 1 root root 23 Jan 25 16:17 xyz.txt
③pull拉操作
#与push是相反的操作
[root@openEuler1 ~]# rsync -avz 192.168.126.141:/opt/xyz.txt /root/
Authorized users only. All activities may be monitored and reported.
receiving incremental file list
xyz.txt
sent 43 bytes received 131 bytes 116.00 bytes/sec
total size is 23 speedup is 0.13
#查看结果
[root@openEuler1 ~]# ll /root/
total 12
-rw-------. 1 root root 706 Jan 23 21:05 anaconda-ks.cfg
drwxr-xr-x 3 root root 4096 Jan 23 22:20 findfiles
-rw-r--r-- 1 root root 23 Jan 25 16:17 xyz.txt
3.2 守护进程
3.2.1 服务端配置
①创建需要传输的目录
[root@openEuler1 ~]# mkdir /backup
②配置文件
[root@openEuler1 ~]# vim + /etc/rsyncd.conf
#全局配置
uid = rsync
gid = rsync
use chroot = no
max connections = 100
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
ignore errors
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
hosts allow = 192.168.126.0/24
fake super = yes #这个必须写,不写服务无法启动
auth users = rsync_backup
secrets file = /etc/rsync.passwd
#局部配置
[bak] #共享的目的地名字
comment = my backup #描述信息
path = /backup #这台机器的哪一个目录让它进行传文件
read only = false #权限设置
list = false #权限设置
③创建用户(与配置文件指定的要一致)
[root@openEuler1 ~]# useradd rsync -s /sbin/nologin -M #-s /sbin/nologin 不需要登录 -M没有家目录
[root@openEuler1 ~]# cat > /etc/rsync.passwd << EOF
> rsync_backup:123456
> EOF
④修改文件和目录权限
[root@openEuler1 ~]# chmod 600 /etc/rsync.passwd
[root@openEuler1 ~]# chown -R rsync:rsync /backup
⑤启动服务
[root@openEuler1 ~]# systemctl start rsyncd
3.2.2 客户端测试
[root@openEuler2 ~]# rsync -avz /opt/xyz.txt rsync_backup@192.168.126.140::bak
Password:
sending incremental file list
xyz.txt
sent 125 bytes received 43 bytes 48.00 bytes/sec
total size is 13 speedup is 0.08
3.2.3 客户端免密登录
[root@openEuler2 ~]# rsync -avz /opt/xyz.txt rsync_backup@192.168.126.140::bak --password-file=/etc/rsync.passwd
sending incremental file list
xyz.txt
sent 132 bytes received 49 bytes 362.00 bytes/sec
total size is 20 speedup is 0.11
[root@openEuler2 ~]# more /opt/xyz.txt
娇小赤雅
123456