学习linux第三十七天

rsync

 

[root@hanlinxy tmp]# rsync -av /etc/passwd /tmp/xy.txt (本地增量复制)
sending incremental file list
passwd

sent 2209 bytes received 31 bytes 4480.00 bytes/sec
total size is 2135 speedup is 0.95

 

[root@hanlinxy ~]# rsync -av /etc/passwd root@192.168.3.137:/tmp/1.txt (增量复制passwd到远程主机1.txt中,-a表示递归,-v表示可视化,不写root@表示默认以当前终端用户登入对方机器)
root@192.168.3.137's password: 
sending incremental file list
passwd

sent 2209 bytes received 31 bytes 407.27 bytes/sec
total size is 2135 speedup is 0.95
[root@hanlin2 ~]# vim /etc/ssh/ssh_config 

错误是:The authenticity of host xxx.xxx.xxx.xxx can't be established。

执行ssh  -o StrictHostKeyChecking=no  yyy.yyy.yyy.yyy,就可以了。yyy.yyy.yyy.yyy是本地机器的IP地址。

或者在ssh_config文件中添加 

StrictHostKeyChecking=no 保存退出即可

 

 

 

rsync基本命令

-a, --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD。
-r, --recursive 对子目录以递归模式处理。
-v, --verbose 详细模式输出。
-l, --links 保留软链结。 -L, --copy-links 想对待常规文件一样处理软链结。同步软连接时会把原文件给也给同步 
-p, --perms 保持文件权限。 
-e, --rsh=command 指定使用rsh、ssh方式进行数据同步。
-o, --owner 保持文件属主信息。 -g, --group 保持文件属组信息。
-D, --devices 保持设备文件信息。 -t, --times 保持文件时间信息。
--delete 删除那些DST中SRC没有的文件。 
--exclude=PATTERN 指定排除不需要传输的文件模式
-P 等同于 --partial。 --progress 显示备份过程 比-V更详细 
-z, --compress 对备份的文件在传输时进行压缩处理。
-u, --update 仅仅进行更新,也就是跳过所有已经存在于DST,并且文件时间晚于要备份的文件, 不覆盖更新的文件。也就是DEST中的文件比SRC新,不同步



[root@hanlinxy ~]# cd /root
[root@hanlinxy ~]# ls
[root@hanlinxy ~]# mkdir xy
[root@hanlinxy ~]# cd xy

[root@hanlinxy xy]# touch 1.txt 2.jpg 3.docx
[root@hanlinxy xy]# ls
1.txt 2.jpg 3.docx
[root@hanlinxy xy]# cd /tmp
[root@hanlinxy tmp]# ln -s /etc/passwd ./
[root@hanlinxy tmp]# ls
passwd xy.txt
[root@hanlinxy tmp]# ls -l 
总用量 4
lrwxrwxrwx. 1 root root 11 6月 28 00:13 passwd -> /etc/passwd
-rw-r--r--. 1 root root 8 6月 28 00:13 xy.txt
[root@hanlinxy tmp]# rm -rf xy.txt 
[root@hanlinxy tmp]# ls
passwd
[root@hanlinxy tmp]# ls -l
总用量 0
lrwxrwxrwx. 1 root root 11 6月 28 00:13 passwd -> /etc/passwd
[root@hanlinxy tmp]# cd -
/root/xy
[root@hanlinxy xy]# ls -l
总用量 0
-rw-r--r--. 1 root root 0 6月 28 00:12 1.txt
-rw-r--r--. 1 root root 0 6月 28 00:12 2.jpg
-rw-r--r--. 1 root root 0 6月 28 00:12 3.docx

 

[root@hanlinxy xy]# rsync -avL /tmp/ /root/xy (-L表示同步软连接的时候会直接把软连接的原文件拷贝过去 ps:软连接不支持对目录做,ln -s 源 目的)

 

 
sending incremental file list
./
.X0-lock
passwd
.ICE-unix/
.ICE-unix/1295
.Test-unix/
.X11-unix/
.X11-unix/X0
.XIM-unix/
.font-unix/

 

[root@hanlinxy tmp]# ls -l
总用量 8
-rw-r--r--. 1 root root 0 6月 28 00:12 1.txt
-rw-r--r--. 1 root root 0 6月 28 00:12 2.jpg
-rw-r--r--. 1 root root 0 6月 28 00:12 3.docx
-rw-r--r--. 1 root root 2135 5月 30 13:57 passwd
-rw-r--r--. 1 root root 36 6月 28 00:22 xy.txt
[root@hanlinxy tmp]# cd -
/root/xy
[root@hanlinxy xy]# ls -l
总用量 4
-rw-r--r--. 1 root root 0 6月 28 00:12 1.txt
-rw-r--r--. 1 root root 0 6月 28 00:12 2.jpg
-rw-r--r--. 1 root root 0 6月 28 00:12 3.docx
-rw-r--r--. 1 root root 2135 5月 30 13:57 passwd
[root@hanlinxy xy]# ls -l
总用量 4
-rw-r--r--. 1 root root 0 6月 28 00:12 1.txt
-rw-r--r--. 1 root root 0 6月 28 00:12 2.jpg
-rw-r--r--. 1 root root 0 6月 28 00:12 3.docx
-rw-r--r--. 1 root root 2135 5月 30 13:57 passwd
[root@hanlinxy xy]# cat passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin

[root@hanlinxy xy]# rsync -av --delete /root/xy/ /tmp/ (--delete 删除dest里面SRT没有的文件)
sending incremental file list
deleting xy.txt

 

sent 284 bytes received 17 bytes 602.00 bytes/sec
total size is 2146 speedup is 7.13
[root@hanlinxy xy]# cd -
/tmp
[root@hanlinxy tmp]# ls -l
总用量 4
-rw-r--r--. 1 root root 0 6月 28 00:12 1.txt
-rw-r--r--. 1 root root 0 6月 28 00:12 2.jpg
-rw-r--r--. 1 root root 0 6月 28 00:12 3.docx
-rw-r--r--. 1 root root 2135 5月 30 13:57 passwd

 

[root@hanlinxy tmp]# rsync -av --exclude "*txt" /root/xy/ /tmp/ (排除exclude选项的文件不同步)
sending incremental file list
./
2.jpg
3.docx
passwd

sent 2533 bytes received 77 bytes 5220.00 bytes/sec
total size is 2146 speedup is 0.82
[root@hanlinxy tmp]# ls -l
总用量 4
-rw-r--r--. 1 root root 0 6月 28 00:12 2.jpg
-rw-r--r--. 1 root root 0 6月 28 00:12 3.docx
-rw-r--r--. 1 root root 2135 5月 30 13:57 passwd

-u

[root@hanlinxy tmp]# echo "11223233fgsgdg" > 1.txt
[root@hanlinxy tmp]# rsync -avPu /root/xy/ /tmp/ (加了-u如果DEST文件比较新,则不同步这个文件)
sending incremental file list
./

sent 287 bytes received 20 bytes 614.00 bytes/sec
total size is 2146 speedup is 6.99
[root@hanlinxy tmp]# cat 1.txt 
11223233fgsgdg
[root@hanlinxy tmp]# cd -
/root/xy
[root@hanlinxy xy]# cat 1.txt
[root@hanlinxy xy]# 

 

 



[root@hanlinxy tmp]# rsync -avP /root/xy/ 192.168.3.137:/tmp/ (同步文件到远程主机,通过ssh 22端口,也可以指定端口比如rsync -avP /root/xy/ -e "ssh -p 22" 192.168.3.137:/tmp/)
root@192.168.3.137's password: 
sending incremental file list
./
1.txt
0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=11/14)
2.jpg

[root@hanlinxy tmp]# rsync -avP /tmp/xy.xy 192.168.3.137::test/xy2.xy
sending incremental file list
xy.xy
0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=0/1)

 

sent 81 bytes received 34 bytes 230.00 bytes/sec
total size is 0 speedup is 0.00

 

转载于:https://my.oschina.net/u/3867255/blog/1859081

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值