八周二次课(3月27日)

10.28 rsync工具介绍

10.29/10.30 rsync常用选项

10.31 rsync通过ssh同步



10.28 rsync工具介绍

10.28-10.31 rsync工具介绍及常用选项,rsync通过ssh同步_ssh


Linux文件同步工具-rsync

此工具很重要,以后可能每天都需要用到。要熟悉熟练使用


备份方式

  • 本地备份,把数据从A的C目录传输到A的C目录。类似于cp,但是跟cp不一样。

  • 远程备份,把数据从A传输到B上。

本地备份

rsync -av /etc/passwd /tmp/1.txt

远程备份

rsync -av /tmp/1.txt 192.168.188.128:/tmp/2.txt

rsync格式

  • *SRC源目录,源文件

  • *DEST目标目录,目标文件

  • rsync [OPTION] … SRC   DEST

    #本地与本地拷贝数据

  • rsync [OPTION] … SRC   [user@]host:DEST

    #本地拷贝数据至远程

  • [user@]host:DEST 格式可以对应root@192.168.189.128

  • rsync [OPTION] … [user@]host:SRC   DEST

    #远程目录同步数据到本地。

  • rsync [OPTION] … SRC   [user@]host::DEST

    #与其他验证方式不同。

  • rsync [OPTION] … [user@]host::SRC   DEST

    #与其他验证方式不同。



10.29 rsync常用选项(上)

大纲

10.28-10.31 rsync工具介绍及常用选项,rsync通过ssh同步_rsync_02


Linux文件同步工具-rsync

rsync常用选项

  •  -a 包含-rtplgoD

  •  -r 同步目录时要加上,类似cp时的-r选项

  •  -v 同步时显示一些信息,让我们知道同步的过程,可视化显示进度

  •  -l 保留软连接

  • (加上此选项之后,软链接状态跟源软链接状态是一样的,万一源链接指向的文件被删除了,那么也会跟着失效。-L则能避免此情况发生)

  •  -L 加上该选项后,同步软链接时会把源文件给同步

  •  -p 保持文件的权限属性

  •  -o 保持文件的属主

  •  -g 保持文件的属组

  •  -D 保持设备文件信息

  •  -t 保持文件的时间属性(mtime,ctime,atime)

  •  --delete 删除DEST中SRC没有的文件

    例如B上没有A所没有的文件,A目录有123,B有4,如果此时加了此选项,A->B,会把B的4删掉。

    使其子文件同步。如果想一模一样,就要加--delete,如果不想一模一样,可以不加。

  •  --exclude=PATTERN 过滤指定文件,如--exclude “logs”会把文件名包含logs的文件或者目录过滤掉,不同步。(支持通配)

  •  -P 显示同步过程,比如速率,比-v更加详细

  •  -u 加上该选项后,如果DEST中的文件比SRC新,则不同步。大意就是,把DST中比SRC还新的文件排除掉,不会覆盖。update简写。

  •  -z 传输时压缩zip格式



10.30 rsync常用选项(下)


10.28-10.31 rsync工具介绍及常用选项,rsync通过ssh同步_rsync_03


实验

本地备份

[root@centos7-01 ~]# mkdir 111
[root@centos7-01 ~]# cd 111/
[root@centos7-01 111]# touch 12.tx~ 12.txt 12_txt.swp 4913 aming3
[root@centos7-01 111]# mkdir 222
[root@centos7-01 111]# ls
12.tx~  12.txt  12_txt.swp  222  4913  aming3
[root@centos7-01 111]# rsync -av /root/111/ /tmp/111_dest/
sending incremental file list
created directory /tmp/111_dest
./
12.txt
12.tx~
12_txt.swp
4913
aming3
222/
sent 311 bytes  received 114 bytes  850.00 bytes/sec
total size is 0  speedup is 0.00

--delete 用法 同步效果要与源一样 

[root@centos7-01 111]# touch /tmp/111_dest/new.txt
[root@centos7-01 111]# rsync -av --delete /root/111/ /tmp/111_dest/
sending incremental file list
deleting new.txt
sent 110 bytes  received 13 bytes  246.00 bytes/sec
total size is 0  speedup is 0.00

上述输出结果可以看出new.txt被删除掉。

下面 ls /tmp/111_dest/可以看到new.txt已经被删掉

[root@centos7-01 111]#  ls /tmp/111_dest/
12.tx~  12.txt  12_txt.swp  222  4913  aming3

--exclude用法

先删掉/111_dest/下的所有文件,重新同步过/root/111/

[root@centos7-01 111]# rm -rf /tmp/111_dest/*
[root@centos7-01 111]# rsync -avL --exclude "*.txt" /root/111/ /tmp/111_dest/
sending incremental file list
./
12.tx~
12_txt.swp
4913
aming3
222/
sent 265 bytes  received 95 bytes  720.00 bytes/sec
total size is 0  speedup is 0.00

可以看到txt都被过滤掉了,剩余的文件都被同步到了/root/111/里


--exclude支持多个在同一条命令上,命令示意如下。

[root@centos7-01 111]# rsync -avL --exclude "*.txt" --exclude "aming*" /root/111/ /tmp/111_dest/
sending incremental file list
#-P用法
#先删掉/tmp/111_dest/
[root@centos7-01 111]# !rm
rm -rf /tmp/111_dest/*
[root@centos7-01 111]# rsync -avP /root/111/ /tmp/111_dest/ 
sending incremental file list
./
12.txt
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=5/7)
12.tx~
           0 100%    0.00kB/s    0:00:00 (xfer#2, to-check=4/7)
12_txt.swp
           0 100%    0.00kB/s    0:00:00 (xfer#3, to-check=3/7)
4913
           0 100%    0.00kB/s    0:00:00 (xfer#4, to-check=2/7)
aming3
           0 100%    0.00kB/s    0:00:00 (xfer#5, to-check=1/7)
222/
sent 311 bytes  received 114 bytes  850.00 bytes/sec
total size is 0  speedup is 0.00

-u用法

需求:让目标文件更加新,这时候需要用到-u

#cd /tmp/111_dest/
#vi 4913 添加文字内容
1112321321oijiojoiffdl
sdlkasjklwjqewq
1111
333hnjkhk
jk

添加-u选项,

[root@centos7-01 111_dest]# rsync -avPu /root/111/ /tmp/111_dest/
sending incremental file list
./
sent 113 bytes  received 16 bytes  258.00 bytes/sec
total size is 0  speedup is 0.00
[root@centos7-01 111_dest]# cat 4913 
1112321321oijiojoiffdl
sdlkasjklwjqewq
1111
333hnjkhk
jk

cat完之后,发现目标文件4913编辑的内容没被覆盖掉,



10.31 rsync通过ssh同步

大纲

10.28-10.31 rsync工具介绍及常用选项,rsync通过ssh同步_ssh_04

Linux文件同步工具-rsync

  • 打开两台虚拟机操作

  • 两台虚拟机能互相通信,分别是1和2号

把1的/etc/passwd 同步到2上,/tmp/aming.txt

[root@centos7-01 111_dest]# rsync /etc/passwd 192.168.189.129:/tmp/aming.txt
root@192.168.189.129's password: 
bash: rsync: 未找到命令
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: remote command not found (code 127) at io.c(605) [sender=3.0.9]
[root@centos7-01 111_dest]# rsync /etc/passwd 192.168.189.129:/tmp/aming.txt

出现以上报错消息,此报错意思是因为2上面没有安装rsync命令,此时在2号安装即可。

[root@centos7-01 111_dest]# rsync -av /etc/passwd 192.168.189.129:/tmp/aming.txt
root@192.168.189.129's password: 
sending incremental file list
passwd
sent 1176 bytes  received 31 bytes  268.22 bytes/sec
total size is 1102  speedup is 0.91

上面属于推文件动作

反向操作,把2号的数据,同步到1上

[root@centos7-01 111_dest]# rsync -avP 192.168.189.129:/tmp/aming.txt /tmp/123.txt
root@192.168.189.129's password: 
receiving incremental file list
aming.txt
        1102 100%    1.05MB/s    0:00:00 (xfer#1, to-check=0/1)
sent 30 bytes  received 1184 bytes  161.87 bytes/sec
total size is 1102  speedup is 0.91

这属于拉文件动作。

指定端口

rsync -av -e "ssh -p 22" test1/ 192.168.189.129:/tmp/aming.txt