rsync

转载链接:
http://www.ruanyifeng.com/blog/2020/08/rsync.html
https://www.cnblogs.com/f-ck-need-u/p/7220009.html#blog21
https://www.cnblogs.com/0820LL/p/9627047.html

安装

如果本机或者远程计算机没有安装 rsync,可以用下面的命令安装。

# Debian
$ sudo apt-get install rsync

# Red Hat
$ sudo yum install rsync

# Arch Linux
$ sudo pacman -S rsync

基本用法

一、local(本机使用)

1.1 -r 参数

本机使用 rsync 命令时,可以作为cpmv命令的替代方法,将源目录同步到目标目录。

$ rsync -r source destination

上面命令中,-r表示递归,即包含子目录。注意,-r是必须的,否则 rsync 运行不会成功。source目录表示源目录,destination表示目标目录。

如果有多个文件或目录需要同步,可以写成下面这样。

$ rsync -r source1 source2 destination

上面命令中,source1source2都会被同步到destination目录。

1.2 -a 参数

-a参数可以替代-r,除了可以递归同步以外,还可以同步元信息(比如修改时间、权限等)。由于 rsync 默认使用文件大小和修改时间决定文件是否需要更新,所以-a-r更有用。下面的用法才是常见的写法。

$ rsync -a source destination

目标目录destination如果不存在,rsync 会自动创建。执行上面的命令后,源目录source被完整地复制到了目标目录destination下面,即形成了destination/source的目录结构。

如果只想同步源目录source里面的内容到目标目录destination,则需要在源目录后面加上斜杠。

$ rsync -a source/ destination

上面命令执行后,source目录里面的内容,就都被复制到了destination目录里面,并不会在destination下面创建一个source子目录。

1.3 -n 参数

如果不确定 rsync 执行后会产生什么结果,可以先用-n--dry-run参数模拟执行的结果。

$ rsync -anv source/ destination

上面命令中,-n参数模拟命令执行的结果,并不真的执行命令。-v参数则是将结果输出到终端,这样就可以看到哪些内容会被同步。

1.4 --delete 参数

默认情况下,rsync 只确保源目录的所有内容(明确排除的文件除外)都复制到目标目录。它不会使两个目录保持相同,并且不会删除文件。如果要使得目标目录成为源目录的镜像副本,则必须使用--delete参数,这将删除只存在于目标目录、不存在于源目录的文件。

$ rsync -av --delete source/ destination

上面命令中,--delete参数会使得destination成为source的一个镜像。

1.5 --exclude 参数

有时,我们希望同步时排除某些文件或目录,这时可以用--exclude参数指定排除模式。

$ rsync -av --exclude='*.txt' source/ destination
# 或者
$ rsync -av --exclude '*.txt' source/ destination

上面命令排除了所有 TXT 文件。

注意,rsync 会同步以"点"开头的隐藏文件,如果要排除隐藏文件,可以这样写--exclude=".*"

如果要排除某个目录里面的所有文件,但不希望排除目录本身,可以写成下面这样。

$ rsync -av --exclude 'dir1/*' source/ destination

多个排除模式,可以用多个--exclude参数。

$ rsync -av --exclude 'file1.txt' --exclude 'dir1/*' source/ destination

多个排除模式也可以利用 Bash 的大扩号的扩展功能,只用一个--exclude参数。

$ rsync -av --exclude={'file1.txt','dir1/*'} source/ destination

如果排除模式很多,可以将它们写入一个文件,每个模式一行,然后用--exclude-from参数指定这个文件。

$ rsync -av --exclude-from='exclude-file.txt' source/ destination

1.6 --include 参数

--include参数用来指定必须同步的文件模式,往往与--exclude结合使用。

$ rsync -av --include="*.txt" --exclude='*' source/ destination

上面命令指定同步时,排除所有文件,但是会包括 TXT 文件。

二、远程同步

2.1 SSH协议

rsync 除了支持本地两个目录之间的同步,也支持远程同步。它可以将本地内容,同步到远程服务器。

#push(推) 本地内容 同步到远程服务器
$ rsync -av source/ username@remote_host:destination

也可以将远程内容同步到本地。

#pull(拉) 远程内容 同步到本地
$ rsync -av username@remote_host:source/ destination

rsync 默认使用 SSH 进行远程登录和数据传输。

由于早期 rsync 不使用 SSH 协议,需要用-e参数指定协议,后来才改的。所以,下面-e ssh可以省略。

$ rsync -av -e ssh source/ user@remote_host:/destination

但是,如果 ssh 命令有附加的参数,则必须使用-e参数指定所要执行的 SSH 命令。

$ rsync -av -e 'ssh -p 2234' source/ user@remote_host:/destination

上面命令中,-e参数指定 SSH 使用2234端口。

2.2 rsync协议

语法格式:

#pull(拉) 远程内容 同步到本地
$ rsync -av --port=端口号 username@remote_host::module/ destination
或者
$ rsync -av rsync://username@remote_host:port/module/ destination

#push(推) 本地内容 同步到远程服务器
$ rsync -av SRC/ username@remote_host::module/destination
或者
$ rsync -av SRC/ rsync://username@remote_host:port/module/destination

地址中的module并不是实际路径名,而是rsync服务端配置文件 /etc/rsyncd.conf 里面配置的模块名,模块里面会包含一些用户名、密码、路径等认证信息。

2.3 rsync --daemon模式示例

服务器端:只有一个,放置 rysncd.conf 默认的位置 /etc/rsyncd.conf
客户端:有多个,放置密码文件

使用 rsync --daemon 的步骤

第一步 在服务器端编写 rsync.conf,位置任意,默认为 /etc/rsync.conf
第二步 在服务器端编写用户的账号密码文件(即rsyncd.secrets) 权限必须是600
第三步 启动 sudo rsync --daemon
第三步 如果想无密码传输,在客户端编写相应用户的密码文件(即rsync.passwd)  权限必须是600
第四步 在客户端使用 rsync 进行传输 指定模块和密码文件

默认"rsync --daemon"读取的配置文件为 /etc/rsyncd.conf ,有些版本的系统上可能该文件默认不存在。rsyncd.conf的配置见man rsyncd.conf。以下是部分内容:

# /etc/rsyncd: configuration file for rsync daemon mode
 
# See rsyncd.conf man page for more options.
 
# configuration example:
# uid = nobody			#rsync服务的运行用户,默认是nobody,文件传输成功后属主将是这个uid
# gid = nobody			#rsync服务的运行组,默认是nobody,文件传输成功后属组将是这个gid
# use chroot = yes		#rsync daemon在传输前是否切换到指定的path目录下,并将其监禁在内
# max connections = 4	        #指定最大连接数量,0表示没有限制
# pid file = /var/run/rsyncd.pid	#指定rsync daemon的pid文件
# exclude = lost+found/				#
# transfer logging = yes			#
# timeout = 900					#确保rsync服务器不会永远等待一个崩溃的客户端,0表示永远等待
# ignore nonreadable = yes			#
# dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
 						#指定哪些文件不用进行压缩传输
# [ftp1]					#模块ID
#        path = /home/ftp			#指定该模块的路径,该参数必须指定。启动rsync服务前该目录必须存在。rsync请求访问模块本质就是访问该路径。
#        comment = ftp export area	        #

以下是常见的配置项,也算是一个配置示例:

#### 全局配置参数 
port = 888    			# 指定rsync端口。默认873
uid = rsync 			# rsync服务的运行用户,默认是nobody,文件传输成功后属主将是这个uid
gid = rsync 			# rsync服务的运行组,默认是nobody,文件传输成功后属组将是这个gid
use chroot = no 		# rsync daemon在传输前是否切换到指定的path目录下,并将其监禁在内
max connections = 200 	# 指定最大连接数量,0表示没有限制
timeout = 300         	# 确保rsync服务器不会永远等待一个崩溃的客户端,0表示永远等待
motd file = /var/rsyncd/rsync.motd   # 客户端连接过来显示的消息
pid file = /var/run/rsyncd.pid       # 指定rsync daemon的pid文件
lock file = /var/run/rsync.lock      # 指定锁文件
log file = /var/log/rsyncd.log       # 指定rsync的日志文件,而不把日志发送给syslog
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2  # 指定哪些文件不用进行压缩传输
 
#### 下面指定模块,并设定模块配置参数,可以创建多个模块
[test1]        # 模块ID
path = /tmp/test1  # 指定该模块的路径,该参数必须指定。启动rsync服务前该目录必须存在。rsync请求访问模块本质就是访问该路径。
ignore errors      # 忽略某些IO错误信息
read only = false  # 指定该模块是否可读写,即能否上传文件,false表示可读写,true表示可读不可写。所有模块默认不可上传
write only = false # 指定该模式是否支持下载,设置为true表示客户端不能下载。所有模块默认可下载
list = false       # 客户端请求显示模块列表时,该模块是否显示出来,设置为false则该模块为隐藏模块。默认true
hosts allow = 10.0.0.0/24 # 指定允许连接到该模块的机器,多个ip用空格隔开或者设置区间
hosts deny = 0.0.0.0/32   # 指定不允许连接到该模块的机器
auth users = rsync_backup # 指定连接到该模块的用户列表,只有列表里的用户才能连接到模块,用户名和对应密码保存在secrts file中,
                          # 这里使用的不是系统用户,而是虚拟用户。不设置时,默认所有用户都能连接,但使用的是匿名连接
secrets file = /etc/rsyncd.passwd # 保存auth users用户列表的用户名和密码,每行包含一个username:passwd。由于"strict modes"
                                  # 默认为true,所以此文件要求非rsync daemon用户不可读写。只有启用了auth users该选项才有效。
[test2]    # 以下定义的是第二个模块
path=/tmp/test2
read only = false
ignore errors
comment = anyone can access

在服务器端启动

$sudo rsync --daemon 		
# 如果指定配置文件(不指定配置文件,默认为/etc/rsync.conf)
$sudo rsync --daemon --config=/path/rsync.conf 

查看 rsync --daemon 的运行

$ps -ef | grep -v grep | grep rsync

在客户端进行同步

$rsync -avzP --port=端口号 ./testFile.txt  rsync@192.16.157.155::test1  --password-file=rsync.passwd  # 将本地客户端的文件推到服务器端 使用test1模块的配置
$rsync -avzP ./testFile.txt  rsync@192.16.157.155::test2  --password-file=rsync.passwd  # 将本地客户端的文件推到服务器端 使用test2模块的配置
$rsync -avzP rsync@192.16.157.155::test1 ./  --password-file=rsync.passwd  # 将服务器端的数据拉到本地客户端

2.4 rsync --daemon模式项目实战

项目介绍:

此项目实战是客户端 pull(拉)方法  
服务端配置rsyncd.conf相关信息
客户端执行命令 把远程内容同步到本地
$ rsync -av --port=端口号 username@remote_host::module/ destination
或者
$ rsync -av rsync://username@remote_host:port/module/ destination

角色分配表

服务器 ip 工具 系统 目录
Server 192.168.2.221 rsync Centos7.2 s_test
Client 192.168.2.222 rsync Centos7.2 c_test

Server端配置

安装rsync

$ yum -y install rsync

更改配置文件

$ vim /etc/rsyncd.conf
uid = root
gid = root
use chroot = no
max connections = 10
strict modes = yes
hosts allow = 192.168.2.222
port = 5699
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log

[test1]
path = /tmp/s_test/
comment = Hello My Dear!
ignore errors
read only = no
write only = no
hosts allow = 192.168.2.222
hosts deny = *
list = false
uid = root
gid = root
auth users = onetest			#设置允许连接服务器的账户,此账户可以是系统中不存在的用户
secrets file = /etc/rsync.password	#密码验证文件名,该文件权限要求为只读,建议为600,仅在设置auth users后有效

建立用户与密码认证文件

  1. 自定义密码为 123456 (此密码并非登录密码,只是一个 rsync 与 client 服务器进行交互的凭证,可自定义)

  2. 创建rsync.password文件,并写入【用户名:密码】,记住此处的密码!

    [root@localhost ~]# vim /etc/rsync.password
    onetest=123456
    
  3. 需要给密码文件 600 权限

    [root@localhost ~]# chmod 600 /etc/rsync.password
    
  4. 启动rsync (以守护进程方式启动)

    [root@localhost ~]#rsync --daemon
    

    注意:修改 rsyncd.conf 配置文件后,记得需要重启 rsync 服务

    [root@localhost ~]#killall rsync
    [root@localhost ~]#rsync --daemon 
    [root@localhost ~]#lsof -i :873 #可查看是否已启动
    

Clinet配置

服务器IP 192.168.2.222 (只需安装,配置文件也无需修改,不需要启动)

[root@localhost ~]#yum -y install rsync

注意:客户端只需要写密码,服务端是用户加密码

[root@localhost ~]#vim /etc/rsync.password
123456

执行命令

[root@localhost ~]#rsync -avz --port 5699 --password-file=/etc/rsync.password onetest@192.168.2.221::test /c_test

上面的命令是从 服务端 192.168.2.221服务器上/tmp/s_test/ 目录里的内容拉取到 客服端 /c_test 目录里

注意事项:

rsync --daemon 开启需要管理员权限

端口是否开启,默认是873,使用 netstat 命令查看端口信息

防火墙问题,使用 iptables 修改防火墙允许的访问端口

rsync 加密传输

使用 ssh -c 参数指定加密算法

rsync -azrP -e "ssh -c aes256-ctr -p 2020" filename unserName@128.52.16.22:/home/leo/

2.5 rsync + inotify

项目介绍:


角色分配表

服务器 ip 工具 系统 目录
Server 192.168.2.221 rsync Centos7.2 s_test
Client 192.168.2.222 rsync,inotify-tools Centos7.2 c_test

Server端配置

安装rsync

$ yum -y install rsync

更改配置文件

$ vim /etc/rsyncd.conf
uid = root
gid = root
use chroot = no
max connections = 10
strict modes = yes
hosts allow = 192.168.2.222
port = 5699
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log

[inotify]
path = /tmp/s_test/
comment = Hello My Dear!
ignore errors
read only = no
write only = no
hosts allow = 192.168.2.222
hosts deny = *
list = false
uid = root
gid = root
auth users = onetest			#设置允许连接服务器的账户,此账户可以是系统中不存在的用户
secrets file = /etc/rsync.password	#密码验证文件名,该文件权限要求为只读,建议为600,仅在设置auth users后有效

建立用户与密码认证文件

  1. 自定义密码为 123456 (此密码并非登录密码,只是一个 rsync 与 client 服务器进行交互的凭证,可自定义)

  2. 创建rsync.password文件,并写入【用户名:密码】,记住此处的密码!

    [root@localhost ~]# vim /etc/rsync.password
    onetest=123456
    
  3. 需要给密码文件 600 权限

    [root@localhost ~]# chmod 600 /etc/rsync.password
    
  4. 启动rsync (以守护进程方式启动)

    [root@localhost ~]#rsync --daemon
    

    注意:修改 rsyncd.conf 配置文件后,记得需要重启 rsync 服务

    [root@localhost ~]#killall rsync
    [root@localhost ~]#rsync --daemon 
    [root@localhost ~]#lsof -i :873 #可查看是否已启动
    

Clinet配置

服务器IP 192.168.2.222 (只需安装,配置文件也无需修改,不需要启动)

[root@localhost ~]#yum -y install rsync

注意:客户端只需要写密码,服务端是用户加密码

[root@localhost ~]#vim /etc/rsync.password
123456

安装inotify-tools

[root@localhost ~]#yum -y install inotify-tools

脚本

[root@localhost ~]#vim inotify.sh
#!/bin/bash
host=192.168.2.221    					#server的ip(备份服务器)
src=/c_test/ 						#所要监控的备份目录(此处可以自定义,但是要保证存在)
des=inotify   						#自定义的模块名,需要与server端定义的一致
password=/etc/rsync.password  			        #密码文件
user=onetest    					#用户名           
inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src 
| while read files
do
rsync -avzP --delete  --timeout=100 --password-file=${password} $src $user@$host::$des
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done

小结

双冒号 :: 是用在 rsync 协议里面的,

冒号 : 一般用在ssh协议里面,这两种用法各有千秋:

rsync协议你需要在rsync服务端配置模块,这增加了运维工作量,但是安全,因为不需要对客户公开服务器帐号密码。

ssh协议方便,不需配置,拿

到服务器帐号密码即可开工,但是对客户是暴露的,有安全风险。

还需要注意的是用rsync协议认证的时候,后面跟的是模块名,而不是路径,这点要注意。

原文地址:https://www.cnblogs.com/51fly/p/14169440.html