rsync(1) rsync(1)
NAME
rsync ? a fast, versatile, remote (and local) file-copying tool //rsync 介绍
SYNOPSIS
Local: rsync [OPTION...] SRC... [DEST]
Access via remote shell:
Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]
Push: rsync [OPTION...] SRC... [USER@]HOST:DEST
//shell远程访问(命令模式)
Access via rsync daemon:
Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST]
rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]
Push: rsync [OPTION...] SRC... [USER@]HOST::DEST
rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST
//通过后台程序访问(作为服务)
Usages with just one SRC arg and no DEST arg will list the source
files instead of copying.
-v 详细模式输出
-a 归档模式,递归的方式传输文件,并保持文件的属性,equals -rlptgoD
-r 递归拷贝目录
-l 保留软链接
-p 保留原有权限
-t 保留原有时间(修改)
-g 保留属组权限
-o 保留属主权限
-D 等于--devices --specials 表示支持b,c,s,p类型的文件
-R 保留相对路径
-H 保留硬链接
-A 保留ACL策略
-e 指定要执行的远程shell命令
-E 保留可执行权限
-X 保留扩展属性信息 a属性
[root@review1 ~]# mkdir dir1
[root@review1 ~]# mkdir dir2
[root@review1 ~]# touch dir1/file{1..5}
[root@review1 ~]# ls dir1/
file1 file2 file3 file4 file5
[root@review1 ~]# ls dir2
[root@review1 ~]#
//检测dir1中有5个文件,dir2中没有文件
//同步方式一:
[root@review1 ~]# rsync -va /root/dir1 /root/dir2
sending incremental file list
dir1/
dir1/file1
dir1/file2
dir1/file3
dir1/file4
dir1/file5
sent 279 bytes received 111 bytes 780.00 bytes/sec
total size is 0 speedup is 0.00
[root@review1 ~]#
[root@review1 ~]# ls dir2
dir1
//同步dir1到dir2中成功
//方式二:
[root@review1 ~]# rsync -va /root/dir1/ /root/dir2
sending incremental file list
./
file1
file2
file3
file4
file5
sent 266 bytes received 110 bytes 752.00 bytes/sec
total size is 0 speedup is 0.00
[root@review1 ~]# ls dir2
dir1 file1 file2 file3 file4 file5
总结:
1. 本地数据同步的时候,源目录后面的“/”会影响同步的结果
2. # rsync -av /dir1/ /dir3 //只同步目录下面的文件到指定的路径
3. # rsync -av /dir1 /dir2 //将当前目录dir1和目录下的所有文件一起同步
-R:不管加不加”/”,都会将源数据的绝对路径一起同步
需求1:将本地(192.168.221.129)/root/dir1
文件同步到远端(192.168.226.128)/root/dir
中:
[root@review1 ~]# rsync -av /root/dir1 [email protected]:/root/dir/ //同步的命令
The authenticity of host '192.168.226.128 (192.168.226.128)' can't be establis
RSA key fingerprint is 24:36:34:69:1f:6e:b7:60:b0:2a:ae:90:46:aa:86:c5.
Are you sure you want to continue connecting (yes/no)? yes //key授权
Warning: Permanently added '192.168.226.128' (RSA) to the list of known hosts.
[email protected]'s password: //远端用户密码
sending incremental file list
dir1/
dir1/file1
dir1/file2
dir1/file3
dir1/file4
dir1/file5
sent 279 bytes received 111 bytes 26.90 bytes/sec
total size is 0 speedup is 0.00
[root@review1 ~]#
//远端:192.168.226.128
[root@min1 dir]# pwd
/root/dir
[root@min1 dir]# ls
dir1
[root@min1 dir]#
//同步成功
将本地(192.168.221.129)/root/dir文件同步到远端(192.168.226.128)/root/中:
[root@review1 dir2]# rsync -ave 'ssh -lroot' /root/dir 192.168.226.128:/root/
root@192.168.226.128's password:
sending incremental file list
dir/
dir/dir1/
dir/dir1/file1
dir/dir1/file2
dir/dir1/file3
dir/dir1/file4
dir/dir1/file5
sent 300 bytes received 115 bytes 26.77 bytes/sec
total size is 0 speedup is 0.00
主机192.168.226.128
[root@min1 ~]# pwd
/root
[root@min1 ~]# ls
anaconda-ks.cfg install.log software
dir install.log.syslog testdir
[root@min1 ~]# cd dir
[root@min1 dir]# ls
dir1
[root@min1 dir]#
//同步成功
需求2:将远程数据/root/dir
同步到本地/root
中:
[root@review1 ~]# rsync -av root@192.168.226.128:/root/dir /root
root@192.168.226.128's password:
receiving incremental file list
dir/
dir/dir1/
dir/dir1/file1
dir/dir1/file2
dir/dir1/file3
dir/dir1/file4
dir/dir1/file5
sent 114 bytes received 305 bytes 25.39 bytes/sec
total size is 0 speedup is 0.00
[root@review1 ~]# ls /root
anaconda-ks.cfg dir dir1 dir2 install.log install.log.syslog
[root@review1 ~]#
//同步成功!
注1:
rsync 并不是单纯的复制文件,它主要功能是进行文件同步!例如:
//主机:192.168.226.128 中testdir目录下有5个文件
[root@min1 testdir]# pwd
/root/testdir
[root@min1 testdir]# ls
file1 file2 file3 file4 file5
[root@min1 testdir]#
//本地主机:192.168.221.129 中/root目录
[root@review1 ~]# ls
anaconda-ks.cfg install.log install.log.syslog
//将远程testdir同步到本地
[root@review1 ~]# rsync -avR [email protected]:testdir /root/
[email protected]'s password:
receiving incremental file list
testdir/
testdir/file1
testdir/file2
testdir/file3
testdir/file4
testdir/file5
sent 110 bytes received 287 bytes 27.38 bytes/sec
total size is 0 speedup is 0.00
[root@review1 ~]# ls
anaconda-ks.cfg install.log install.log.syslog testdir
[root@review1 ~]# cd testdir/
[root@review1 testdir]# ls
file1 file2 file3 file4 file5
//远端删除file1-file3
[root@min1 testdir]# ls
file1 file2 file3 file4 file5
[root@min1 testdir]# rm file{2..4}
rm: remove regular empty file `file2'? y
rm: remove regular empty file `file3'? y
rm: remove regular empty file `file4'? y
[root@min1 testdir]# ls
file1 file5
//本地同步
[root@review1 testdir]# rsync -avR --delete [email protected]:testdir /root/
[email protected]'s password:
receiving incremental file list
deleting testdir/file4
deleting testdir/file3
deleting testdir/file2
testdir/
sent 15 bytes received 75 bytes 3.16 bytes/sec
total size is 0 speedup is 0.00
[root@review1 testdir]# ls
file1 file5
[root@review1 testdir]#
//加上--delete参数,源文件删除,本地也删除
总结:
rsync是一个同步命令(服务),它不仅可以用来复制、备份,最大的作用在于同步,即保持两端一直,所以远端文件被删除后,同步后,本地文件也可以删除,要注意rsync的灵活用法。
注2:
rsync同步参数-R是会同步绝对路径的。例:
[root@review1 ~]# rsync -avR [email protected]:/root/testdir /root/
root@192.168.226.128's password:
Permission denied, please try again.
root@192.168.226.128's password:
receiving incremental file list
root/
root/testdir/
root/testdir/file1
root/testdir/file2
root/testdir/file3
root/testdir/file4
root/testdir/file5
sent 114 bytes received 315 bytes 15.05 bytes/sec
total size is 0 speedup is 0.00
[root@review1 ~]# ls
anaconda-ks.cfg install.log install.log.syslog root
[root@review1 ~]# cd root/
[root@review1 root]# ls
testdir
//此处同步将192.168.226.128中的root也创建了一个,同步的是绝对路径,没有的文件夹自动帮你创建。
[root@review1 root]# rsync -avR [email protected]:testdir /root/
root@192.168.226.128's password:
receiving incremental file list
testdir/
testdir/file1
testdir/file2
testdir/file3
testdir/file4
testdir/file5
sent 110 bytes received 287 bytes 29.41 bytes/sec
total size is 0 speedup is 0.00
[root@review1 root]# ls
testdir
[root@review1 root]# cd
[root@review1 ~]# ls
anaconda-ks.cfg install.log install.log.syslog root testdir
//将root从目录中去掉就不会再创建root,直接在本地的/root 中直接创建了testdir
总结:
rsync -R会同步绝对路径,没有则自动创建,所以在写命令时一定要注意。
rsync作为服务是托管给xinetd服务管理的,有以下特点:①进程在后台运行,不受终端影响(关终端不会关闭服务,除非杀死相关进程)②可以用相关参数实现一些功能,比如:日志记录,访问控制,验证登录等
/etc/xinetd.d/rsync
1 # default: off
2 # description: The rsync server is a good addition to an ftp server, as it \
3 # allows crc checksumming etc.
4 service rsync
5 {
6 disable = no //打开rsync服务
7 socket_type = stream
8 wait = no
9 user = root
10 server = /usr/bin/rsync
11 server_args = --daemon
12 log_on_failure += USERID
13 }
[root@review1 etc]# pwd
/etc
[root@review1 etc]# ls |grep rsync.conf
[root@review1 etc]#
创建主配置文件/etc/rsyncd.conf
(注意,这里是rsyncd.conf 不是rsync.conf,配置文件名写错会报错,无法远程同步。)
全局参数:可以指定默认端口、pid文件、ip地址等
例如:
motd file=/etc/rsyncd.welcome 欢迎文件,路径自定义[可选]
局部参数
例如:
[notes] 共享给客户端看到的名字,名字自定义,命令中写这个名字
path=/share/dir/ 实际共享的服务器路径,名字必须是你要共享的实际路径
启动xinetd服务
[root@review1 etc]# service xinetd restart
Stopping xinetd: [ OK ]
Starting xinetd: [ OK ]
[root@review1 etc]# netstat -ntpl |grep 873
tcp 0 0 :::873 :::* LISTEN 1745/xinetd
//查看远端服务器192.168.221.128共享文件
[root@review1 etc]# rsync -a 192.168.221.128::
share //共享成功(显示的是标签名)
//检查192.168.221.128的共享文件
[root@min2 ~]# cat /etc/rsyncd.conf
[share]
path=/root/dirtest
[root@min2 ~]# cd dirtest/
[root@min2 dirtest]# ls
123
[root@review1 ~]# rsync -avR 192.168.221.128::share /root/ //
receiving incremental file list
sent 27 bytes received 67 bytes 188.00 bytes/sec
total size is 0 speedup is 0.00
[root@review1 ~]# ls
123 anaconda-ks.cfg dir install.log install.log.syslog testdir
注:若要把本地文件同步到远程主机上,需要更改主配置文件
[root@min2 dirtest]# vim /etc/rsyncd.conf
1 [share]
2 path=/root/dirtest/
3 read only = false
4 uid = root
5 gid = root
[root@min2 ~]# chmod 755 dirtest/
[root@min2 ~]# ll
total 32
-rw-------. 1 root root 1128 May 17 19:56 anaconda-ks.cfg
drwxr-xr-x. 2 root root 4096 May 23 12:30 dirtest
//要想远程同步需要注意三点:①rsync权限:共享文件夹必须设为可读②共享文件夹权限设为755③uid和gid设为root
[root@review1 ~]# rsync -av /root/dir/ [email protected]::share
sending incremental file list
./
sent 25 bytes received 11 bytes 72.00 bytes/sec
total size is 0 speedup is 0.00
//同步成功
[root@min2 etc]# vim /etc/rsyncd.conf
1 [share]
2 path=/root/dirtest/
3 read only = false
4 uid = root
5 gid = root
6 auth users = root //有权限访问的用户,这里没有的用户没有权限访问rsync服务
7 secrets file = /etc/rsyncd.secrets //指明密码文件路径
[root@min2 etc]# vim /etc/rsyncd.secrets //写密码文件中内容:可以访问的用户及其密码
1 root:123456
[root@min2 etc]# chmod 600 /etc/rsyncd.secrets //更改密码文件权限
[root@min2 etc]# ll |grep rsyncd.secrets
-rw-------. 1 root root 12 May 23 14:36 rsyncd.secrets
[root@min2 etc]# service xinetd restart
Stopping xinetd: [ OK ]
Starting xinetd: [ OK ]
[root@review1 ~]# rsync -av root@192.168.221.128::share /root/dir/
Password:
receiving incremental file list
123
sent 74 bytes received 149 bytes 49.56 bytes/sec
total size is 0 speedup is 0.00
[root@review1 ~]# ls /root/dir/
123 //访问成功
特别注意:
①配置文件中auth users 写了哪个用户,哪个用户就可以访问,没写的就不能访问,密码访问一旦开启,所有人都要输入密码,所以没有在auth users中指定的用户是无法访问的。
②密码文件的属主必须是rsync服务的运行者,权限必须是600。例如:root运行rsync –daemon,则secrets file的owner也必须是root;secrets file权限必须是600。
[root@review1 ~]# man rsync //man一下,了解rsync日志语法
--log-file=FILE override the "log file" setting //日志文件路径定义
--log-file-format=FMT override the "log format" setting //日志格式定义
--log-file=FILE
This option causes rsync to log what it is doing to a file. This is similar to
the logging that a daemon does, but can be requested for the client side and/or
the server side of a non-daemon transfer. If specified as a client option,
transfer logging will be enabled with a default format of “%i %n%L”. See the
--log-file-format option if you wish to override this.
Here’s a example command that requests the remote side to log what is happen-
ing:
rsync -av --rsync-path="rsync --log-file=/tmp/rlog" src/ dest/
This is very useful if you need to debug why a connection is closing unexpect-
edly.
--log-file-format=FORMAT
This allows you to specify exactly what per-update logging is put into the file
specified by the --log-file option (which must also be specified for this
option to have any effect). If you specify an empty string, updated files will
not be mentioned in the log file. For a list of the possible escape charac-
ters, see the “log format” setting in the rsyncd.conf manpage.
The default FORMAT used if --log-file is specified and this option is not is
’%i %n%L’.
[root@review1 ~]# vim /etc/rsyncd.conf
1 log file=/var/log/rsyncd.log
2
3 [notes]
4 path=/root/testdir
注意:
配置文件是全局变量,必须写在标签外面!
[root@review1 log]# cd /var/log/
[root@review1 log]# ls
anaconda.ifcfg.log boot.log messages
anaconda.log btmp ntpstats
anaconda.program.log cron secure
anaconda.storage.log dmesg spooler
anaconda.syslog dmesg.old tallylog
anaconda.xlog dracut.log wtmp
anaconda.yum.log lastlog yum.log
audit maillog
//rsync.log 不存在
[root@mysql ~]# rsync [email protected]::
notes
//远端访问一下
[root@review1 log]# ls
anaconda.ifcfg.log boot.log messages
anaconda.log btmp ntpstats
anaconda.program.log cron rsyncd.log
anaconda.storage.log dmesg secure
anaconda.syslog dmesg.old spooler
anaconda.xlog dracut.log tallylog
anaconda.yum.log lastlog wtmp
audit maillog yum.log
//再次查看,rsync.log文件已经生成。
[root@review1 log]# vim rsyncd.log
1 018/05/24 16:46:14 [7459] name lookup failed for 192.168.221.130: Name or service not known
2 2018/05/24 16:46:14 [7459] connect from UNKNOWN (192.168.221.130)
3 2018/05/24 16:46:14 [7459] module-list request from UNKNOWN (192.168.2 21.130)
//日志生成成功!
[root@review1 software]# ls
inotify-tools-3.13.tar.gz
[root@review1 software]# tar zxvf inotify-tools-3.13.tar.gz -C /usr/src/
[root@review1 software]# cd /usr/src/inotify-tools-3.13/
[root@review1 inotify-tools-3.13]# ls
aclocal.m4 config.h.in COPYING libinotifytools man src
AUTHORS config.sub depcomp ltmain.sh missing
ChangeLog configure INSTALL Makefile.am NEWS
config.guess configure.ac install-sh Makefile.in README
[root@review1 inotify-tools-3.13]# ./configure --prefix=/usr/local/inotify
//编译、安装
[root@review1 inotify-tools-3.13]# make && make install
[root@review1 inotify-tools-3.13]# cd /usr/local/inotify/
[root@review1 inotify]# ls
bin include lib share
[root@review1 inotify]# cd bin/
[root@review1 bin]# ls
inotifywait inotifywatch
[root@review1 bin]# cd ../include/
[root@review1 include]# ls
inotifytools
[root@review1 include]# cd ../lib/
[root@review1 lib]# ls
libinotifytools.a libinotifytools.so libinotifytools.so.0.4.1
libinotifytools.la libinotifytools.so.0
[root@review1 lib]# cd ../share/
[root@review1 share]# ls
doc man
1 #!/bin/bash
2 /usr/local/inotify/bin/inotifywait -mrq -e modify,delete,create, attrib,move /dir |while read events
3 do
4 rsync -a --delete /dir/ /dir1/ ; //同步dir到dir1
5 echo "`date +%F\ %T`出现事件$events" >> /var/log /rsync.log 2>&1;
6 done
7
8 # chmod +x 1.sh
注:①/usr/local/inotify/bin/inotifywait inotify监测模块命令所在路径
②/dir 监测目录
③/dir1 备份目录
④这里的rsync也可以远程同步到其他主机,和rsync的远程同步写法一样。
[root@review1 bin]# cd /dir
[root@review1 dir]# ls
file1 file2 file3
[root@review1 dir]# cd /dir1
[root@review1 dir1]# ls
[root@review1 dir1]#
[root@review1 dir1]# ls
[root@review1 dir1]# cd /dir
[root@review1 dir]# mkdir test
[root@review1 dir]# cd /dir1
[root@review1 dir1]# ls
file1 file2 file3 test
//同步成功
//日志
tail -f /var/log/rsync.log
2018-05-25 09:19:23出现事件/dir/ CREATE,ISDIR test
/usr/local/inotify/bin/inotifywait -mrq -e modify,delete,create, attrib,move /root/dir |while read events
//注意这里的监测目录是/root/dir
//报错
[root@review1 bin]# ./inotify.sh
Couldn't watch /root/dir: File name too long
结论:
这里的File name too long是因为/root/dir/目录内部递归了太多的目录,目录太多,inotify就无法监测了!