10.28 rsync工具介绍
10.29/10.30 rsync常用选项
10.31 rsync通过ssh同步
10.28 rsync工具介绍:
linux文件同步工具rsync rsync很实用,也很重要
像我们从A机器到B机器,我们去传输文件、远程备份一些数据。
也可以从本机传输,像从A目录到B目录,类似于cp,但不一样
假设一个场景,我们要每小时从A目录拷贝到B目录一次,而A目录在不停的增加。这时候,我们用cp就不合适了,因为不方便浪费时间,还浪费磁盘不停的读写。而rsync就适合这种场景,他只同步一些更新的文件,还支持远程的同步(A级器到B机器)
~1. rsync -av /etc/passwd /tmp/123.txt 将passwd文件拷贝到tmp下,并改名叫123.txt
[root@axinlinux-01 system]# rsync -av /etc/passwd /tmp/123.txt
sending incremental file list
passwd 输出文件
sent 1,071 bytes received 35 bytes 2,212.00 bytes/sec
发送了多少个字节 多少字节每秒
total size is 979 speedup is 0.89
一共有多大 速度是多少
~2. rsync /etc/passwd root@192.168.50.136:/tmp/123.txt 远程去拷贝、同步。A机器到B机器之间的同步、拷贝
前面写输出文件路径,后面跟对方机器的用户名,然后在@对方的IP,再:,最后为他输入的路径 然后输入密码
~3. rsync 的格式:
rsync [OPTION(选项)] ... SRC(源目录)DEST(目标目录或目标文件) 本机格式,以上~1.就是这种格式
rsync [OPTION] ...SRC [user@]host:DEST user@是可以省略的,在本机模式中,省略后是以当前用户作为输入用户。host为他的IP 以上~2.就是这种模式
rsync [OPTION] [user@]host:SRC DEST 反拷贝。远程的目录或文件,拷贝到本机的目录下。先写user@IP:跟他的输出文件。后面写输入路径或文件
----------------------------------------------------------------------------------------------------------------------------------------------------
10.29/10.30 rsync常用选项:
~ -a 包含-rtplgoD
-a包含-r t p l g o D选项
~ -r 同步目录是要加上,类似cp时的-r选项
~ -v 同步是显示一些信息,让我们知道同步的过程
~ -l 保留软连接
~ -L 加上该选项后,同步软连接时会把源文件给同步
与-l的区别。如果A传到B,加上l也会同步软连接,但B上没有A机器上的软链接指向的源文件,一样也不能使用软连接。加上-L会防止这样情况发生,会把软连接的源文件一起给同步过去
~ -p(小写) 保持文件的文件的权限属性
比如700、645等等的权限属性,在这是什么权限,在那就是什么权限
~ -o 保持文件的属主
假如在这是www用户,在那就是www用户。如果没有www用户,就会显示puid。
~ -g 保持文件的属组
假如在这是root属组,在那就是root属组
~ -D 保持设备文件信息
了解即可
~ -t 保持文件的时间属性
像mtime、ctime等等保留过去
~ --delete 删除DEST中SRC没有的文件 (很有用)
删除目标文件中源文件所没有的文件。假如,A到B。B文件中有123,A没有。同步过去的时候,为保持一致,会删除B中的123,所以,A和B完全一样
~ --exclude 过滤指定文件,如--exclude “logs”会把文件名包含logs的文件或目录过滤掉,不同步
假如文件中有日志,但是日志对于目标文件没用。加上--exclude后面跟要排除的文件的名字
~ -P(大写) 显示同步过程,比如速率,比-v更加详细
比如文件很大,我们不知道是否卡死,可加上-P
~ -u 加上该选项后,如果DEST中的文件比SRC新,则不同步
假如,我们目标文件的mtime比源文件新。(可理解为,我们之前对目标文件做过更改,目标文件比源文件更有价值。而源文件比较旧。那么则不同步)建立在我们的需要是 以新的为主
~ -z 传输是压缩
zip的意思。为的是传输的时候更快,节省带宽。传输的过程会压一下,传到目标文件的时候,就自动解压了。只不过是为了节省带宽而已
[root@afeilinux-01 ~]# mkdir test2
[root@afeilinux-01 ~]# ls test2
[root@afeilinux-01 ~]# mkdir test1
mkdir: 无法创建目录"test1": 文件已存在
[root@afeilinux-01 ~]# rsync -a test1 test2
[root@afeilinux-01 ~]# ls test2
test1
[root@afeilinux-01 ~]# ls test2/test1/
1.sh
[root@afeilinux-01 ~]# ls test2/test1/
1.sh
避免把test1放到test2当中命令是:
[root@afeilinux-01 ~]# ls test2/test1/
1.sh
[root@afeilinux-01 ~]# rm -rf test2
[root@afeilinux-01 ~]# rsync -a test1/ test2/
[root@afeilinux-01 ~]# ls -l test2
总用量 4
-rw-r--r--. 1 root root 288 9月 3 16:53 1.sh
(10.30 rsync) 实例:-
-av
[root@afeilinux-01 sed]# rsync -av /root/sed/ /tmp/sed_dest/ 凡是同步目录的时候,都要在源目录和目标目录后面加 / 意思就是,把/root/sed/同步到tmp下,并且该名字为sed_dest
sending incremental file list
sending incremental file list
./
1.txt
test.txt
test.txt.bak
sent 231 bytes received 76 bytes 614.00 bytes/sec
total size is 0 speedup is 0.00
-avL
[root@afeilinux-01 ~]# rsync -avL /tmp/11.txt /root/sed/ 在-av后面加L,可把a里面包含的l覆盖掉。L是这样用的
sending incremental file list
11.txt 会提示11.txt这个文件有软连接,一起同步过去
sending incremental file list
11.txt
sent 88 bytes received 35 bytes 246.00 bytes/sec
total size is 0 speedup is 0.00
-delete
[root@afeilinux-01 ~]# touch /tmp/sed_dest/new.txt 我们现在目标目录下touch一个多文件
[root@afeilinux-01 ~]# rsync -av --delete /root/sed/ /tmp/sed_dest 加上-delete
sending incremental file list
deleting new.txt
./
11.txt
sent 173 bytes received 49 bytes 444.00 bytes/sec
total size is 0 speedup is 0.00
sending incremental file list
deleting new.txt 会提示这个多余的文件
./
1.txt
11.txt
sent 352 bytes received 69 bytes 842.00 bytes/sec
total size is 2,011 speedup is 4.78
[root@afeilinux-01 ~]# ls /tmp/sed_dest 我们在ls一下,发现new.txt就没有了
11.txt 1.txt 2 test.txt test.txt.bak x
--exclude (也支持多个--exclude过滤)
[root@afeilinux-01 ~]# rsync -av --exclude "*.txt" /root/sed/ /tmp/sed_dest 过滤掉所有的txt文件
sending incremental file list
./ 在这就可以发现没有txt文件
sending incremental file list
sent 70 bytes received 12 bytes 164.00 bytes/sec
total size is 0 speedup is 0.00
[root@afeilinux-01 ~]# ls !$ ls看一下
ls /tmp/sed_dest
11.txt 1.txt test.txt test.txt.bak
[root@afeilinux-01 ~]# touch /root/sed/axin.txt 我们先touch一个文件,在--exclude
[root@afeilinux-01 ~]# rsync -av --exclude "*.txt" --exclude "axin*" /root/sed/ /tmp/sed_dest 也支持多个--exclude
sending incremental file list
./
sent 78 bytes received 19 bytes 194.00 bytes/sec
total size is 0 speedup is 0.00
[root@afeilinux-01 ~]# ls !$
ls /tmp/sed_dest
11.txt 1.txt test.txt test.txt.bak
-P 保持文件的权限属性
[root@afeilinux-01 ~]# rsync -avP --delete /root/sed/ /tmp/sed_dest
sending incremental file list
axin.txt
0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=2/6)
sent 194 bytes received 35 bytes 458.00 bytes/sec
total size is 0 speedup is 0.00
-u 加上该选项后,如果DEST中的文件比SRC新,则不同步。大意就是,把DST中比SRC还新的文件排除掉,不会覆盖。update简写。
[root@afeilinux-01 ~]# vi /tmp/sed_dest/1.txt 我们先修改一下目标文件1.txt。满足他事假上的一个环境
[root@afeilinux-01 ~]# rsync -avPu --delete /root/sed/ /tmp/sed_dest 加-u试一下
sending incremental file list
./
sent 158 bytes received 19 bytes 354.00 bytes/sec
total size is 0 speedup is 0.00
[root@afeilinux-01 ~]# cat /tmp/sed_dest/1.txt cat一下,发现是修改后的。也就是加-u会保护你修改过的目标文件
tmpttttttttttttttttttttttttttt
----------------------------------------------------------------------------------------------------------------------------------------------------
10.31 rsync通过ssh同步:
rsync通过ssh方式同步(A到B,A和B都要安装rsync)
~1. rsync -av test1/ 192.168.159.130:/tmp/test2/
我们给另一台机器同步数据
[root@afeilinux-01 ~]# rsync -avPu /root/sed/ 192.168.50.136:/tmp/sed_dest 不指定用户的话就是对方的root用户
root@192.168.50.136s password: 要输入密码
sending incremental file list
created directory /tmp/sed_dest
./
1.txt
0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=4/6)
11.txt
0 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=3/6)
axin.txt
0 100% 0.00kB/s 0:00:00 (xfr#3, to-chk=2/6)
test.txt
0 100% 0.00kB/s 0:00:00 (xfr#4, to-chk=1/6)
test.txt.bak
0 100% 0.00kB/s 0:00:00 (xfr#5, to-chk=0/6)
sent 353 bytes received 150 bytes 10.59 bytes/sec
total size is 0 speedup is 0.00
我们B机器上看一下,有没有传过来
[root@afeilinux-02 ~]# ls /tmp/sed_dest 是有的
11.txt 1.txt axin.txt test.txt test.txt.bak
~2. rsync -av -e "ssh -p 22" test1/ 192.168.50.136:/tmp/test2/
我们有时候的端口可能不是默认的22端口,那我们可以给他指定端口。 -e "ssh -p 22"
[root@afeilinux-01 ~]# rsync -avPu -e "ssh -p 22" /root/sed/ /tmp/sed_dest
sending incremental file list
sent 151 bytes received 12 bytes 326.00 bytes/sec
total size is 0 speedup is 0.00
rsync通过服务的方式同步
要编辑配置文件 /etc/rsyncd.conf
启动服务rsync --daemon
格式: rsync -av test1/ 192.168.159.130::module/dir/