linux备份与同步工具rsync
版权声明:原创作品,请勿转载!
文章目录
版权声明:原创作品,请勿转载!
实验环境介绍:
1.工具介绍
2.详细介绍
2.1 本地模式(用得少)
2.2 远程模式
2.3 守护进程模式(企业常用)
实验环境介绍:
主机名 | IP | 角色 |
backup | 10.0.0.41/24 172.16.1.41/24 | rsync服务端 |
web01 | 10.0.0.7/24 | rsync客户端 |
1.工具介绍
Rsync(Remote Synchronization)是Linux/Unix系统下的一款增量备份与文件同步工具,仅同步变化的文件部分,支持本地或远程数据同步。
2.详细介绍
2.1 本地模式(用得少)
语法结构:
rsync [OPTION...] SRC... [DEST]
命令 选项参数 源文件 目标位置
实战演示:
[root@backup ~]# touch a.txt
[root@backup ~]# ll a.txt
-rw-r--r-- 1 root root 0 May 15 11:19 a.txt###将a.txt拷贝到/opt目录下
[root@backup ~]# ll /opt
total 0
[root@backup ~]# rsync -avz a.txt /opt
sending incremental file list
a.txtsent 84 bytes received 35 bytes 238.00 bytes/sec
total size is 0 speedup is 0.00
[root@backup ~]# ll /opt
total 0
-rw-r--r-- 1 root root 0 May 15 11:19 a.txt
##再次拷贝则默认的为增量拷贝,不会再进行拷贝
[root@backup ~]# rsync -avz a.txt /opt
sending incremental file listsent 44 bytes received 12 bytes 112.00 bytes/sec
total size is 0 speedup is 0.00注意:只拷贝目录下的内容不拷贝目录本身需要在目录的后面加/
[root@backup ~]# rsync -avz test/ /opt/
sending incremental file list
./
1.txt
2.txt
3.txtsent 247 bytes received 76 bytes 646.00 bytes/sec
total size is 43 speedup is 0.13
[root@backup ~]# ll /opt
total 12
-rw-r--r-- 1 root root 29 Mar 29 2024 1.txt
-rw-r--r-- 1 root root 8 Mar 31 2024 2.txt
-rw-r--r-- 1 root root 6 Mar 31 2024 3.txt
-rw-r--r-- 1 root root 0 May 15 11:19 a.txt不加/就会拷贝目录及目录下的内容
[root@backup ~]# rsync -avz test /opt/
sending incremental file list
test/
test/1.txt
test/2.txt
test/3.txtsent 262 bytes received 77 bytes 678.00 bytes/sec
total size is 43 speedup is 0.13
[root@backup ~]# ll /opt
total 0
-rw-r--r-- 1 root root 0 May 15 11:19 a.txt
drwxr-xr-x 2 root root 45 Mar 31 2024 test
[root@backup ~]# ll /opt/test
total 12
-rw-r--r-- 1 root root 29 Mar 29 2024 1.txt
-rw-r--r-- 1 root root 8 Mar 31 2024 2.txt
-rw-r--r-- 1 root root 6 Mar 31 2024 3.txt
2.2 远程模式
远程模式分为两种,支持push(推送)和pull(拉取)两种同步方式
①pull:rsync [OPTION...] [USER@]HOST:SRC... [DEST]
-avz:参数
USER@:用户
HOST:主机IP地址 域名 主机名称
SRC: 源文件
DEST:目标位置/ 下载到本地的哪个目录
实战演示:
###10.0.0.41拉取10.0.0.7上的/root/nginx-1.26.0.tar.gz到本地的当前目录
[root@backup ~]# rsync -avz root@10.0.0.7:/root/nginx-1.26.0.tar.gz ./
The authenticity of host '10.0.0.7 (10.0.0.7)' can't be established.
ECDSA key fingerprint is SHA256:leUKz+MjwtcFXELjOcmzH4STQrbY83DuaRU6LTWLP9c.
ECDSA key fingerprint is MD5:b0:c5:2d:b4:60:68:2e:f2:b5:36:a8:b0:06:97:67:c8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.7' (ECDSA) to the list of known hosts.
root@10.0.0.7's password:
receiving incremental file list
nginx-1.26.0.tar.gzsent 43 bytes received 1,243,731 bytes 276,394.22 bytes/sec
total size is 1,244,118 speedup is 1.00
[root@backup ~]# ll nginx-1.26.0.tar.gz
-rw-r--r-- 1 root root 1244118 Apr 23 2024 nginx-1.26.0.tar.gz
②PUSH:rsync [OPTION...] SRC... [USER@]HOST:DEST
-avz:参数选项
SRC:文件/文件的位置/目录
USER@ :用户名
HOST:ip地址主机名称 域名
DEST:目标位置
实战演示:
###10.0.0.41将test目录推送到10.0.0.7的/opt目录下
[root@backup ~]# rsync -avz test root@172.16.1.7:/opt/
The authenticity of host '172.16.1.7 (172.16.1.7)' can't be established.
ECDSA key fingerprint is SHA256:leUKz+MjwtcFXELjOcmzH4STQrbY83DuaRU6LTWLP9c.
ECDSA key fingerprint is MD5:b0:c5:2d:b4:60:68:2e:f2:b5:36:a8:b0:06:97:67:c8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.1.7' (ECDSA) to the list of known hosts.
root@172.16.1.7's password:
sending incremental file list
test/
test/1.txt
test/2.txt
test/3.txtsent 262 bytes received 77 bytes 96.86 bytes/sec
total size is 43 speedup is 0.13
[root@web01 ~]# ll -R /opt
/opt:
total 0
drwxr-xr-x 2 root root 45 Mar 31 2024 test/opt/test:
total 12
-rw-r--r-- 1 root root 29 Mar 29 2024 1.txt
-rw-r--r-- 1 root root 8 Mar 31 2024 2.txt
-rw-r--r-- 1 root root 6 Mar 31 2024 3.txt
但是这种传输你可能会发现一个问题,那就是需要输入root的密码,这是十分危险的。那怎么解决这个问题呢?我们看下面的这种方式。
2.3 守护进程模式(企业常用)
1.客户端需要执行的命令格式:
Push(往服务端上传文件): rsync [OPTION...] SRC... [USER@]HOST::DEST # 冒号后边必须是模块名称
Pull(从服务端下载文件): rsync [OPTION...] [USER@]HOST::SRC... [DEST]
2.配置rsync服务
[root@backup ~]# vim /etc/rsyncd.conf
[root@backup ~]# cat /etc/rsyncd.conf
uid = rsync
gid = rsync
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 = this is backup show!
path = /backup
[root@backup ~]# id rsync
id: rsync: no such user
[root@backup ~]# useradd -M -s /sbin/nologin rsync
[root@backup ~]# id rsync
uid=1001(rsync) gid=1001(rsync) groups=1001(rsync)
[root@backup ~]# vim /etc/rsync.passwd
[root@backup ~]# cat /etc/rsync.passwd
rsync_backup:123456
[root@backup ~]# chmod 600 /etc/rsync.passwd
[root@backup ~]# ll /etc/rsync.passwd
-rw------- 1 root root 20 May 9 2024 /etc/rsync.passwd
3.创建备份目录并授权
[root@backup ~]# mkdir /backup
[root@backup ~]# chown rsync.rsync /backup
[root@backup ~]# ll -d /backup
drwxr-xr-x 2 rsync rsync 19 May 9 2024 /backup
4.重启服务并加入开机自启
[root@backup ~]# systemctl restart rsyncd
[root@backup ~]# systemctl enable rsyncd
5.查看端口启动情况,可以看到服务成功部署启动
[root@backup ~]# netstat -tnulp | grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 2358/rsync
tcp6 0 0 :::873 :::* LISTEN 2358/rsync
6.实战演练:
###将10.0.0.7 家目录下的rsync_test目录推送到10.0.0.41服务端的backup模块
[root@web01 ~]# rsync -avz rsync_test rsync_backup@10.0.0.41::backup
Password:
sending incremental file list
rsync_test/
rsync_test/1.txtsent 126 bytes received 47 bytes 69.20 bytes/sec
total size is 0 speedup is 0.00
[root@backup ~]# ll -d /backup/rsync_test/
drwxr-xr-x 2 rsync rsync 19 May 15 2025 /backup/rsync_test/
从上面的实战演练案例中可以看到这种是需要交互的,需要手动输入密码,但往往在生产环境中rsync会搭配定时任务在凌晨执行,我们总不能在凌晨去手动输入密码吧,所以我们需要采用非交互式的方法来往服务器推送文件,那怎么做呢,请看下文
7.配置免交互,可以看到不再需要手动输入密码
1.将密码写入到一个文件中
[root@web01 ~]# echo 123456 > /etc/rsync.pass
[root@web01 ~]# chmod 600 /etc/rsync.pass
2.再次往服务端推送文件
[root@web01 ~]# rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.pass
sending incremental file listsent 48 bytes received 20 bytes 136.00 bytes/sec
total size is 327 speedup is 4.81
好了,rsync服务暂时就先介绍到这里啦~