rsync学习笔记

课程目标

1.能够使用rsync实现本地文件同步(cp效果)

2.能够使用rsync实现远程网络文件同步(scp效果)

3.将rsync放入后台运行,实现数据定时同步。

 

一、rsync是什么

Rsync是一款开源的、快速的、多功能的、可实现全量及增量的本地或远程数据同步备份的优秀工具。并且可以不进行改变原有数据的属性信息,实现数据的备份迁移特性。

图解rsync增量备份

 

 

二、rsync语法

rsync命令超详细解释。

复制代码
rsync  [选项]  源数据   目的数据

1.安装
yum install rsync -y


2.命令语法,分几个模式


- 本地模式

rsync 参数   源路径  目标路径

rsync  -xxxxx    /var/log    /tmp



- 远程模式,推送方式,把自己的数据推送到另一台机器上(上传)
语法1 ,rsync默认走ssh协议
rsync 参数  源路径  user@ip:目标路径

rsync  -avzP  /var/log/      root@10.0.0.31:/tmp/



语法2
rsync 参数 源路径  user@ip::目标路径


- 远程模式,拉取方式,拉取别人机器的数据到自己的机器上(下载)
rsync  参数   user@ip:源路径   目标路径
rsync  参数   user@ip::源路径目标路径


rsync -avzP  root@10.0.0.31:/var/log/   /tmp/


参数解释
-v 详细模式输出
-a 归档模式,递归的方式传输文件,并保持文件的属性,等同于 -rlptgoD
-r 递归拷贝目录
-l 保留软链接
-p 保留原有权限
-t 保留原有时间(修改)
-g 保留属组权限
-o 保留属主权限
-D 等于--devices --specials 表示支持b,c,s,p类型的文件
-R 保留相对路径
-H 保留硬链接
-A 保留ACL策略
-e 指定要执行的远程shell命令
-E 保留可执行权限
-X 保留扩展属性信息 a属性


比较常用的组合参数
rsync -avzP
-a 保持文件原有属性
-v 显示传输细节情况
-z 对传输数据压缩传输
-P 显示文件传输的进度信息

 


你在命令行里,执行命令,喜欢看到命令的执行过程
-avzP


脚本里面?
bash xxx.sh
rsync -az


复制代码

 

三、rsync命令使用

1. 本机同步

 

复制代码
注意:
1. 本地数据同步的时候,源目录后面的“/”会影响同步的结果
     # rsync -av /dir1/ /dir3        //只同步目录下面的文件到指定的路径
     # rsync -av /dir1 /dir2        //将当前目录dir1和目录下的所有文件一起同步

2. -R:不管加不加"/",都会将源数据的绝对路径一起同步
    # rsync -avR /dir1/ /dir2/

3. --delete:删除目标目录里多余的文件
    # rsync -avR --delete /dir1/ /dir2/
复制代码

本机同步练习

复制代码
1.本机同步文件
[root@nfs-31 /]$rm -rf text3 [root@nfs-31 /]$rm -rf text4 [root@nfs-31 /]$ [root@nfs-31 /]$mkdir /text1 /text2 [root@nfs-31 /]$touch /text1/haha.txt [root@nfs-31 /]$rsync -avzP /text1/ /text2 sending incremental file list ./ haha.txt 0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=0/2) sent 104 bytes received 38 bytes 284.00 bytes/sec total size is 0 speedup is 0.00 [root@nfs-31 /]$ls text2 haha.txt

2.本机同步文件夹

[root@nfs-31 /]$rsync -avzP /text1 /text2
sending incremental file list
text1/
text1/haha.txt
0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=0/2)


sent 118 bytes received 39 bytes 314.00 bytes/sec
total size is 0 speedup is 0.00
[root@nfs-31 /]$ls text2
haha.txt text1
[root@nfs-31 /]$

复制代码

 远程rsync备份

复制代码
生成测试数据
[root@nfs-31 /]$dd bs=200M count=10 if=/dev/zero of=/text 1/2g.log 10+0 records in 10+0 records out 2097152000 bytes (2.1 GB) copied, 2.88561 s, 727 MB/s [root@nfs-31 /]$

另外一台机器上创建文件

[root@rsync-41 /]$mkdir /text3 /text4
[root@rsync-41 /]$

推送数据

[root@nfs-31 /]$rsync -avzP /text1/ root@rsync-41:/text3
The authenticity of host 'rsync-41 (10.0.0.41)' can't be established.
ECDSA key fingerprint is SHA256:iSKUuRM4GkuUBlCpq2MZHDK5hG126KG3lzafo4cOOA4.
ECDSA key fingerprint is MD5:d7:69:65:64:bb:92:37:81:74:96:69:6a:6d:13:76:32.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'rsync-41' (ECDSA) to the list of known hosts.
root@rsync-41's password:
sending incremental file list
./
2g.log
2,097,152,000 100% 162.43MB/s 0:00:12 (xfr#1, to-chk=1/3)
haha.txt
0 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=0/3)

sent 2,039,634 bytes received 57 bytes 99,497.12 bytes/sec
total size is 2,097,152,000 speedup is 1,028.17
[root@nfs-31 /]$

验证接收

[root@rsync-41 /]$ls /text3
2g.log haha.txt
[root@rsync-41 /]$

限制传输io占用

[root@nfs-31 /]$rsync -avzP --bwlimit=20 /text1/ root@rsync-41:/text4
root@rsync-41's password:
sending incremental file list
./
2g.log
2,097,152,000 100% 20.67MB/s 0:01:36 (xfr#1, to-chk=1/3)
haha.txt
0 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=0/3)

sent 2,039,634 bytes received 57 bytes 19,518.57 bytes/sec
total size is 2,097,152,000 speedup is 1,028.17

[root@rsync-41 /]$ls /text4
2g.log haha.txt

 

拉取数据的模式

[root@rsync-41 /]$mkdir text5
[root@rsync-41 /]$rsync -avzP root@nfs-31:/text1/ /text5
root@nfs-31's password:
receiving incremental file list
./
2g.log
2,097,152,000 100% 166.51MB/s 0:00:12 (xfr#1, to-chk=1/3)
haha.txt
0 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=0/3)

sent 65 bytes received 2,039,634 bytes 123,618.12 bytes/sec
total size is 2,097,152,000 speedup is 1,028.17
[root@rsync-41 /]$ls /text5
2g.log haha.txt
[root@rsync-41 /]$

 

无差异化同步

[root@rsync-41 /]$mkdir /text3
[root@rsync-41 /]$touch /text3/wudi.php
[root@rsync-41 /]$ls text3/
wudi.php
[root@rsync-41 /]$

[root@nfs-31 /]$rsync -avzP --delete /text1/ root@rsync-41:/text3
root@rsync-41's password:
sending incremental file list
deleting wudi.php
./
2g.log
2,097,152,000 100% 161.54MB/s 0:00:12 (xfr#1, to-chk=1/3)
haha.txt
0 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=0/3)

sent 2,039,638 bytes received 69 bytes 116,554.69 bytes/sec
total size is 2,097,152,000 speedup is 1,028.16
[root@nfs-31 /]$

[root@rsync-41 /]$ls text3/
wudi.php
[root@rsync-41 /]$ls text3/
2g.log haha.txt
[root@rsync-41 /]$

 

复制代码

搭建rsync服务模式

Rsync 借助 SSH 协议同步数据存在的缺陷:

1.使用系统用户(不安全) /etc/passwd

2.使用普通用户(会导致权限不足情况)

3.守护进程传输方式: rsync 自身非常重要的功能(不使用系统用户,更加安全)

需要先进行配置文件参数设置

  1. rsync配置文件解读
配置参数参数说明
uid = rsync 指定rsync服务运行的时候,向磁盘进行读取和写入操作的操作者
gid = rsync 指定rsync服务运行的时候,向磁盘进行读取和写入操作的操作者
use chroot = no 进行数据同步存储时,安全相关参数,默认内网进行数据同步,可以关闭
max connections = 200 定义向备份服务器进行数据存储的并发连接数
timeout = 300 定义与备份服务器建立的网络连接,在多长时间没有数据传输时,就释放连接
pid file = /var/run/rsyncd.pid 服务程序运行时,会将进程的pid信息存储到一个指定的pid文件中
lock file = /var/run/rsync.lock 定义锁文件,主要用于配合max connections 参数,当达到最大连接就禁止继续访问
配置参数参数说明
log file = /var/log/rsyncd.log 定义服务的日志文件保存路径信息
[backup] 指定备份目录的模块名称信息
path = /backup 指定数据进行备份的目录信息
ignore errors 在进行数据备份传输过程过程中,忽略一些I/O产生的传输错误
read only = false 设置对备份的目录的具有读写权限,即将只读模式进行关闭
list = false 确认是否可以将服务配置的模块信息,在客户端可以查看显示
hosts allow = 172.16.1.0/24 设置备份目录允许进行网络数据备份的主机地址或网段信息,即设置白名单
配置参数参数说明
hosts deny = 0.0.0.0/32 设置备份目录禁止进行网络数据备份的主机地址或网段信息,即设置黑名单
auth users = rsync_backup 指定访问备份数据目录的认证用户信息,为虚拟定义的用户,不需要进行创建
secrets file = /etc/rsync.password 设置访问备份数据目录进行认证用户的密码文件信息,会在文件中设置认证用户密码信息
配置参数参数说明
[backup] 指定模块名称,便于日后维护
path=/backup 在当前模块中,Daemon使用的文件系统或目录,注意目录权限和配置文件权限一直,防止读写出问题
#exclude= 排除文件或目录,相对路径
[chaoge] 还可以添加其他模块

 

复制代码
cat >> /etc/rsyncd.conf << 'EOF'
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_backup
secrets file = /etc/rsync.passwd 
log file = /var/log/rsyncd.log
#####################################
[backup]
comment = yuchaoit.cn about rsync
path = /backup

[data]
comment = this is secord backup dir,to website data..
path = /data
EOF
复制代码

3.创建用户以及数据目录

复制代码
根据你的配置文件中定义的信息,创建对应的用户,备份的目录
该无法登录的用户,只是用于运行进程的账户
useradd -u 1000 -M -s /sbin/nologin www

创建配置文件中定义的2个备份目录
mkdir -p /data/   /backup

修改备份目录的权限

[root@rsync-41 /]$useradd -u 1000 -M -s /sbin/nologin www
[root@rsync-41 /]$mkdir -p /data/ /backup
[root@rsync-41 /]$chown -R www:www /data/
[root@rsync-41 /]$chown -R www:www /backup/
[root@rsync-41 /]$ll -d /data /backup/
drwxr-xr-x 2 www www 6 Apr 20 17:18 /backup/
drwxr-xr-x 2 www www 6 Apr 20 17:18 /data
[root@rsync-41 /]$\\

 

 
复制代码

4.创建rsync专用的账户密码

复制代码
1.创建密码文件,写入账户和密码,用于和客户端连接时候的认证
vim /etc/rsync.passwd
2.写入账户密码

[root@rsync-41 /]$cat /etc/rsync.passwd
rsync_backup:123123


3.待会客户端向carsync服务器推送数据,就得用这个账号密码!!!!



4.这一步,非常重要,rsync要求降低密码文件的权限,且必须是600


[root@rsync-41 /]$chmod 600 /etc/rsync.passwd
[root@rsync-41 /]$ll /etc/rsync.passw
ls: cannot access /etc/rsync.passw: No such file or directory
[root@rsync-41 /]$ll /etc/rsync.passwd
-rw------- 1 root root 20 Apr 20 17:29 /etc/rsync.passwd
[root@rsync-41 /]$

 
复制代码

 

 

 

4.mysqldump备份

 

posted @   虎躯常震  阅读(216)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示