10.28 rsync工具介绍linux
10.29/10.30 rsync经常使用选项ssh
10.31 rsync经过ssh同步工具
10.28 rsync工具介绍:ui
linux文件同步工具rsync rsync很实用,也很重要日志
像咱们从A机器到B机器,咱们去传输文件、远程备份一些数据。ip
也能够从本机传输,像从A目录到B目录,相似于cp,但不同rem
假设一个场景,咱们要每小时从A目录拷贝到B目录一次,而A目录在不停的增长。这时候,咱们用cp就不合适了,由于不方便浪费时间,还浪费磁盘不停的读写。而rsync就适合这种场景,他只同步一些更新的文件,还支持远程的同步(A级器到B机器)同步
~1. rsync -av /etc/passwd /tmp/123.txt 将passwd文件拷贝到tmp下,并更名叫123.txttest
[root@axinlinux-01 system]# rsync -av /etc/passwd /tmp/123.txtmodule
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.159.130:/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的意思。为的是传输的时候更快,节省带宽。传输的过程会压一下,传到目标文件的时候,就自动解压了。只不过是为了节省带宽而已
(10.30 rsync) 实例:
-av
[root@axinlinux-01 sed]# rsync -av /root/sed/ /tmp/sed_dest/ 凡是同步目录的时候,都要在源目录和目标目录后面加 / 意思就是,把/root/sed/同步到tmp下,而且该名字为sed_dest
sending incremental file list
./
1.txt
test.txt
test.txt.bak
x
2/
sent 2,288 bytes received 103 bytes 4,782.00 bytes/sec
total size is 1,946 speedup is 0.81
-avL
[root@axinlinux-01 ~]# rsync -avL /tmp/11.txt /root/sed/ 在-av后面加L,可把a里面包含的l覆盖掉。L是这样用的
sending incremental file list
11.txt 会提示11.txt这个文件有软链接,一块儿同步过去
sent 131 bytes received 35 bytes 332.00 bytes/sec
total size is 39 speedup is 0.23
--delete
[root@axinlinux-01 ~]# touch /tmp/sed_dest/new.txt 咱们如今目标目录下touch一个多文件
[root@axinlinux-01 ~]# rsync -av --delete /root/sed/ /tmp/sed_dest 加上-delete
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@axinlinux-01 ~]# ls /tmp/sed_dest 咱们在ls一下,发现new.txt就没有了
11.txt 1.txt 2 test.txt test.txt.bak x
--exclude (也支持多个--exclude过滤)
[root@axinlinux-01 ~]# rsync -av --exclude "*.txt" /root/sed/ /tmp/sed_dest 过滤掉全部的txt文件
sending incremental file list
./ 在这就能够发现没有txt文件
test.txt.bak
x
2/
sent 1,190 bytes received 65 bytes 2,510.00 bytes/sec
total size is 980 speedup is 0.78
[root@axinlinux-01 ~]# ls !$ ls看一下
ls /tmp/sed_dest
2 test.txt.bak x
[root@axinlinux-01 ~]# touch /root/sed/axin.txt 咱们先touch一个文件,在--exclude
[root@axinlinux-01 ~]# rsync -av --exclude "*.txt" --exclude "axin*" /root/sed/ /tmp/sed_dest 也支持多个--exclude
sending incremental file list
./
sent 121 bytes received 20 bytes 282.00 bytes/sec
total size is 980 speedup is 6.95
[root@axinlinux-01 ~]# ls !$
ls /tmp/sed_dest
2 test.txt.bak x
-P
[root@axinlinux-01 ~]# rsync -avP --delete /root/sed/ /tmp/sed_dest
sending incremental file list
./
1.txt
39 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=6/8)
11.txt
39 100% 38.09kB/s 0:00:00 (xfr#2, to-chk=5/8)
axin.txt
0 100% 0.00kB/s 0:00:00 (xfr#3, to-chk=4/8)
test.txt
953 100% 930.66kB/s 0:00:00 (xfr#4, to-chk=3/8)
test.txt.bak
980 100% 957.03kB/s 0:00:00 (xfr#5, to-chk=2/8)
x
0 100% 0.00kB/s 0:00:00 (xfr#6, to-chk=1/8)
2/
sent 2,475 bytes received 141 bytes 5,232.00 bytes/sec
total size is 2,011 speedup is 0.77
-u
[root@axinlinux-01 ~]# vi /tmp/sed_dest/1.txt 咱们先修改一下目标文件1.txt。知足他事假上的一个环境
[root@axinlinux-01 ~]# rsync -avPu --delete /root/sed/ /tmp/sed_dest 加-u试一下
sending incremental file list
./
sent 211 bytes received 20 bytes 462.00 bytes/sec
total size is 2,011 speedup is 8.71
[root@axinlinux-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@axinlinux-01 ~]# rsync -avPu /root/sed/ 192.168.159.130:/tmp/sed_dest 不指定用户的话就是对方的root用户
root@192.168.159.130's password: 要输入密码
sending incremental file list
created directory /tmp/sed_dest
./
1.txt
39 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=6/8)
11.txt
39 100% 38.09kB/s 0:00:00 (xfr#2, to-chk=5/8)
axin.txt
0 100% 0.00kB/s 0:00:00 (xfr#3, to-chk=4/8)
test.txt
953 100% 930.66kB/s 0:00:00 (xfr#4, to-chk=3/8)
test.txt.bak
980 100% 957.03kB/s 0:00:00 (xfr#5, to-chk=2/8)
x
0 100% 0.00kB/s 0:00:00 (xfr#6, to-chk=1/8)
2/
sent 2,475 bytes received 177 bytes 408.00 bytes/sec
total size is 2,011 speedup is 0.76
咱们B机器上看一下,有没有传过来
[root@aminglinux-02 ~]# ls /tmp/sed_dest 是有的
11.txt 1.txt 2 axin.txt test.txt test.txt.bak x
~2. rsync -av -e "ssh -p 22" test1/ 192.168.159.130:/tmp/test2/
咱们有时候的端口可能不是默认的22端口,那咱们能够给他指定端口。 -e "ssh -p 22"
[root@axinlinux-01 ~]# rsync -avPu -e "ssh -p 22" /root/sed/ /tmp/sed_dest
sending incremental file list
./
1.txt
39 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=6/8)
11.txt
39 100% 38.09kB/s 0:00:00 (xfr#2, to-chk=5/8)
axin.txt
0 100% 0.00kB/s 0:00:00 (xfr#3, to-chk=4/8)
test.txt
953 100% 930.66kB/s 0:00:00 (xfr#4, to-chk=3/8)
test.txt.bak
980 100% 957.03kB/s 0:00:00 (xfr#5, to-chk=2/8)
x
0 100% 0.00kB/s 0:00:00 (xfr#6, to-chk=1/8)
2/
sent 2,479 bytes received 141 bytes 5,240.00 bytes/sec
total size is 2,011 speedup is 0.77
rsync经过服务的方式同步
要编辑配置文件 /etc/rsyncd.conf
启动服务rsync --daemon
格式: rsync -av test1/ 192.168.159.130::module/dir/