10.28 rsync工具介绍

rsync 数据备份命令 remote sync 远程同步

该工具可以远程同步,还可以本地同步数据。rsync支持增量备份,rsync 不会像其他备份工具如 cp/scp 一样覆盖以前的数据(如果数据已经存在)。它会先判断已经存在的数据和新数据有什么不同,只有不同时才会把不同的部分覆盖掉。

#yum install -y rsync

常见格式

Local: rsync [OPTION...] SRC... [DEST] 备份本地数据

#rsync -av 1.txt /tmp

#rsync -av 1.txt /tmp/2.txt

Access via remote shell:

Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST] 远程同步数据到本地

#rsync -av root@192.168.0.101:/tmp/1.txt /tmp/

Push: rsync [OPTION...] SRC... [USER@]HOST:DEST 远程拷贝数据,本地数据>远程

#rsync -av 123.txt 192.168.0.101:/date/ 不加user@默认是root

#rsync -av 123.txt thinkpad@192.168.1.101:/home/thinkpad

Pull 拉, 远程数据拉向本地

Push 推, 本地数据上传到远端

Access via rsync daemon:

Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST] 相比于远程同步数据到本地,多了个冒号

Push: rsync [OPTION...] SRC... [USER@]HOST::DEST 相比于远程拷贝数据,多了冒号

10.29/10.30 rsync常用选项

rsync -avL 常用

-a 常用

归档模式,表示以递归方式传输文件,保持所有属性,等同于 -rlptgoD,后面可以跟一个 --no-[OPTION] 表示这个关闭 -rlptgoD 中的某一个,例如 -a--no-l 等同于 -rptgoD

-r

对子目录以递归模式处理,主要是针对目录来说的,如果单独传一个文件不需要加-r,但是传输的是目录必须加-r。

-v 常用

打印一些信息出来,比如速率,文件数量等

-I

保留软连接

-L

像对待常规文件一样处理软连接,如果是源机器有软连接文件,则加上该项后 会把软连接指向的目标文件拷贝到目标机器。即在拷贝同步软连接的时候,连软连接指向的文件一起拷贝。

-p (小写)

保持文件权限

-o

保持文件属主信息

-g

保持文件属组信息

-D

保持设备文件信息,很少接触

-t

保持文件时间信息

--delete 常用

删除那些目标中文件有,源目标上面没有的文件,A机器上面有1 2 3 4 5 6 7 8 9 0文件,B机器上也有。A机器经过一段时间,删除了1 2 3文件,那么在同步备份的时候,如果使用这个选项,B机器上的1 2 3 文件也会被删除。强制目标文件和原始文件一致。

--exclude=PATTERN 常用

指定排除不需要传输的文件,等号后面跟文件名,可以使通配符模式,如果有需要排除多个文件,每个文件前面都需要单独使用--exclude,--exclude="abc" --exclude="*.txt"

--progress == -P

在同步的过程中可以看到同步的过程状态,比如统计要同步的文件数量、同步的文件传输速度等等。

-u == --update

这个选项会把DET中比SRC还新的文件排除掉,不会覆盖。源机上和目标机器上都有1.txt,但是在目标机器上做了一些修改,不加-u ,同步数据的时候,较老的源机器上的数据会覆盖目标机器上较新的数据,加上-u,则不会覆盖较新的数据。即目标文件比源文件还要新,则忽略该文件。

    -v, --verbose               increase verbosity-q, --quiet                 suppress non-error messages--no-motd               suppress daemon-mode MOTD (see caveat)-c, --checksum              skip based on checksum, not mod-time & size-a, --archive               archive mode; equals -rlptgoD (no -H,-A,-X)--no-OPTION             turn off an implied OPTION (e.g. --no-D)-r, --recursive             recurse into directories-R, --relative              use relative path names--no-implied-dirs       don’t send implied dirs with --relative-b, --backup                make backups (see --suffix & --backup-dir)--backup-dir=DIR        make backups into hierarchy based in DIR--suffix=SUFFIX         backup suffix (default ~ w/o --backup-dir)-u, --update                skip files that are newer on the receiver--inplace               update destination files in-place--append                append data onto shorter files--append-verify         --append w/old data in file checksum-d, --dirs                  transfer directories without recursing-l, --links                 copy symlinks as symlinks-L, --copy-links            transform symlink into referent file/dir--copy-unsafe-links     only "unsafe" symlinks are transformed--safe-links            ignore symlinks that point outside the tree-k, --copy-dirlinks         transform symlink to dir into referent dir-K, --keep-dirlinks         treat symlinked dir on receiver as dir-H, --hard-links            preserve hard links-p, --perms                 preserve permissions-E, --executability         preserve executability--chmod=CHMOD           affect file and/or directory permissions-A, --acls                  preserve ACLs (implies -p)-X, --xattrs                preserve extended attributes-o, --owner                 preserve owner (super-user only)-g, --group                 preserve group--devices               preserve device files (super-user only)--specials              preserve special file

上面的需掌握 下面是实验

实验准备

[root@wangbin ~]# mkdir rsync

[root@wangbin ~]# mkdir test1

[root@wangbin ~]# cd test1

[root@wangbin test1]# touch 1 2 3

[root@wangbin test1]# ln -s /root/123.txt ./123.txt

[root@wangbin test1]# ls -l

total 0

-rw-r--r-- 1 root root 0 Feb 18 02:02 1

lrwxrwxrwx 1 root root 13 Feb 18 02:02 123.txt -> /root/123.txt

-rw-r--r-- 1 root root 0 Feb 18 02:02 2

-rw-r--r-- 1 root root 0 Feb 18 02:02 3

1、选项 -a

归档模式,表示以递归方式传输文件,保持所有属性,等同于 -rlptgoD,后面可以跟一个 --no-[OPTION] 表示这个关闭 -rlptgoD 中的某一个,例如 -a--no-l 等同于 -rptgoD

[root@wangbin ~]# rsync -a test1 test2

[root@wangbin ~]# ls test2

test1 目标目录已经存在,则备份成其子目录

[root@wangbin ~]# ls test2/test1/

1 123.txt 2 3 目标目录已经存在,则备份成其子目录

[root@wangbin ~]# rm -rf test2

[root@wangbin ~]# rsync -a test1 test2

[root@wangbin ~]# ls test

test1/ test2/

[root@wangbin ~]# ls test2

test1

[root@wangbin ~]# rm -rf test2

[root@wangbin ~]# rsync -a test1/ test2/ 目标目录必须加符号 /

[root@wangbin ~]# ls test2

1 123.txt 2 3

建议养成加 / 的习惯。-a 选项等于 -rlptgoD,后面可以跟选项 --no-[OPTION] 一并使用。

[root@wangbin ~]# rsync -av --no-l test1/ test2/

sending incremental file list

skipping non-regular file "123.txt" 由此看出,-l的作用是不备份连接文件。

sent 68 bytes received 12 bytes 160.00 bytes/sec

total size is 13 speedup is 0.16

加上-l 会把软连接文件拷贝过去,但是软连接的目标文件没有拷贝过去。

2、-L 选项 如果有软连接,把连接指向的文件也一起传输

向对待常规文件一样处理软连接,如果是SRC中有软连接文件,则加上该项后 会把软连接指向的目标文件拷贝到DST

#rm -rf test2

[root@wangbin ~]# touch 123.txt 若没有目标文件,则不拷贝

[root@wangbin ~]# rsync -avL test1/ test2/

sending incremental file list

created directory test2

./

1

123.txt

2

3

sent 227 bytes received 91 bytes 636.00 bytes/sec

total size is 0 speedup is 0.00

-L 会把 来源机器 中软连接的目标文件拷贝到目标机器目录中,如果没有目标文件,则提示没有参照物,不拷贝连接文件以及目标文件,其他不影响。

3、-u 选项 不去覆盖时间比较新的文件

这个选项会把 目标机器 中比 来源机器中 还新的文件排除掉,不会覆盖。即如果目标机器的文件比较新,则不会覆盖。在目标目录下创建新文件,来源文件下没有的文件,也不会覆盖(已实验成功)。

# ls -l test1/1 test2/1

-rw-r--r-- 1 root root 0 Feb 18 02:02 test1/1 【test1和test2里面的文件1创建时间一致】

-rw-r--r-- 1 root root 0 Feb 18 02:02 test2/1

# touch test2/1        【修改2下面的创建时间】# ll test2/1

-rw-r--r-- 1 root root 0 Feb 18 02:21 test2/1 【时间改变】

# rsync -a test1/1 test2/# !ll

ll test2/1

-rw-r--r-- 1 root root 0 Feb 18 02:02 test2/1 【不加-u,则重新拷贝过去】

# !touch

touch test2/1

# !ll

ll test2/1

-rw-r--r-- 1 root root 0 Feb 18 02:22 test2/1

 # rsync -avu test1/ test2/     【加上-u选项,排除1文件,因为目标目录下的1文件的时间比来源目录下的文件时间新】

sending incremental file list

./

123.txt -> /root/123.txt

sent 88 bytes received 18 bytes 212.00 bytes/sec

total size is 13 speedup is 0.12

# ls -l test2/

total 0

-rw-r--r-- 1 root root 0 Feb 18 02:22 1

lrwxrwxrwx 1 root root 13 Feb 18 02:02 123.txt -> /root/123.txt

-rw-r--r-- 1 root root 0 Feb 18 02:02 3

4、--delete 选项 强制和来源文件一样

会删除目标目录下有,但是来源目录没有的文件,相当于重新拷贝一份了来源文件并且重命名;不加 --delete ,相当于把sorc下面的文件,拷贝到目标目录下,并不考虑目标目录下是否有其他文件存在。就是加上这个选项,来源文件没有的东西,即便目标这里有,也要删除掉。

# rm -rf test1/123.txt          【删除链接文件123.txt】# rsync -av test1/ test2/            【重新备份文件】

sending incremental file list

./

sent 55 bytes received 15 bytes 140.00 bytes/sec

total size is 0 speedup is 0.00

 # ls test2                      【test2目录下仍然有 123.txt】

1 123.txt 2 3

 # rsync -av --delete test1/ test2/   【增加--delete选项】

sending incremental file list

deleting 123.txt 【删除123.txt】

sent 52 bytes received 12 bytes 128.00 bytes/sec

total size is 0 speedup is 0.00

ls test2

1 2 3 【123.txt被删除 】

如果此时 test2 目录下还有文件4 5 6 7 等等,凡是和source目录下不一致的文件,全部会删除掉。

5、 --exclude 排除指定文件或目录

# touch test1/4# rsync -av --exclude="4" test1/ test2/        【排除来源文件4,也可作用于目录】

sending incremental file list

./

sent 55 bytes received 15 bytes 140.00 bytes/sec

total size is 0 speedup is 0.00

 # ls test2/                     【test2目录下并没有出现文件4】

1 2 3

 # touch test1/a.txt test1/b.txt# rsync -av --exclude="*.txt" test1/ test2/    【可以使用通配符】

sending incremental file list

./

4

sent 108 bytes received 34 bytes 284.00 bytes/sec

total size is 0 speedup is 0.00

6、 --progress 或者 -P 显示进程选项

# rm -rf test2/# rsync -av --progress --exclude="*.txt" test1/ test2/

sending incremental file list

created directory test2

./

1

       0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=3/5)

2

       0 100%    0.00kB/s    0:00:00 (xfer#2, to-check=2/5)

3

       0 100%    0.00kB/s    0:00:00 (xfer#3, to-check=1/5)

4

       0 100%    0.00kB/s    0:00:00 (xfer#4, to-check=0/5)

sent 225 bytes received 91 bytes 632.00 bytes/sec

total size is 0 speedup is 0.00

--progress 会把当前进程的进度在屏幕上显示。对小型文件没有意义,对大型文件有用,可以看到拷贝的进程。

7、-z 压缩

#rsync -avz 。。。。。。

先对文件进行压缩,再进行传输,本地传输无需使用-z选项,在进程远程传输的时候,为了节省带宽,可以使用-z。但是压缩的过程管理员是没有办法感知的,传输完成后也无需解压,在目标机器上得到的文件,和不加-z的效果是一样的。

10.31 rsync通过ssh同步

参考 rsync 格式里面的格式 (一个冒号)

#rsync option... SRC [USER@]HOST:DEST 远程拷贝数据,本地数据>远程

#rsync -av 123.txt 192.168.0.101:/date/ 不加user@默认是root

#rsync -av 123.txt thinkpad@192.168.1.101:/home/thinkpad

#rsync option... [USER@] HOST:SRC DEST 远程同步数据到本地

如果不知道host,可以通过 #hostname 查看

一、将本地数据拷贝到远端

#yum install -y openssh-clients 【两台机器都安装 openssh-clients 工具】

[root@wangbin ~]# ls test1/

1 2 3 4 a.txt b.txt

[root@wangbin ~]# rsync -avL test1/ root@192.168.247.132:/tmp/test2

The authenticity of host '192.168.247.132 (192.168.247.132)' can't be established. RSA key fingerprint is 61:5f:aa:60:e7:a6:69:97:a1:24:f6:7d:09:bb:08:17. Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added '192.168.247.132' (RSA) to the list of known hosts.

root@192.168.247.132's password:

sending incremental file list 【发送数据到远端】

created directory /tmp/test2

./

1

2

3

4

a.txt

b.txt

sent 327 bytes received 129 bytes 17.88 bytes/sec

total size is 0 speedup is 0.00

[root@thinkpad ~]# ls /tmp/test2/

1 2 3 4 a.txt b.txt

二、拷贝远端数据到本地

[root@wangbin ~]# rsync -avL root@192.168.247.132:/tmp/test2/ ./test3/

root@192.168.247.132's password:

receiving incremental file list 【接收,上面是发送 sending】

created directory ./test3

./

1

2

3

4

a.txt

b.txt

sent 128 bytes received 332 bytes 48.42 bytes/sec

total size is 0 speedup is 0.00

[root@wangbin ~]# ls test3

1 2 3 4 a.txt b.txt

NOTE:若对方机器的端口不是22,因为ssh默认走的是22端口,如果没有22端口,那么就没办法进行传输。此时查看对方机器的信息,netstat -lnp 查看可用的端口,查找可用的ssh端口,假设端口号是10022。 -e 指定端口

#rsync -av -e "ssh -p 10022" root@192.168.247.132:/tmp/test2/ ./test3/

测试在/etc/ssh/sshd_config 增加10022端口

/etc/init.d/sshd restart

三、使用密钥连同两个机器,相互传输数据不需要输入密码。

[root@wangbin ~]# ssh-keygen

Your identification has been saved in /root/.ssh/id_rsa.

Your public key has been saved in /root/.ssh/id_rsa.pub.

[root@wangbin ~]# cat .ssh/id_rsa.pub

ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA3U0c26r3HSRnhkloK50BNZvYceRTcDU4nU5aJqQBTuQo+Cn36jPd9i/vzGt8FYe6Tal5OfXFaaPEC+poAleF6E7zPgfamHXk9SriZRgLRR6nDPZ6wI9mhCwdQ0DVKQ6/C4oHqrTxpf9E2gIlzs4cm1FK4cpBdNpG7ucDlWkHi7rv6RjtbgKY2a9jDiNEUp8bqJOc2if0kWRtC21ArmtM9rYIHrWjbltvGSjXxsc0xp0H+pUnnX2/pWJhQfOLaVseo0E2lLfpUEYF9LW7FwXnfqda+gfVmOEhUTqJ4d+UrHSBhoC+xCansjvpq4lQ4oZwp4unbLaKQzg41r2z34aoNQ== root@wangbin

复制上面的公钥,粘贴到另外一个Linux机器上

[root@thinkpad ~]# mkdir -p /home/user1/.ssh 【创建.ssh目录】

[root@thinkpad ~]# chmod 600 !$ 【修改.ssh目录的权限】

chmod 600 /home/user1/.ssh

[root@thinkpad ~]# vi /home/user1/.ssh/authorized_keys 【讲上面复制的信息粘贴到里面】

返回生成密钥对的机器

[root@wangbin ~]# ssh user1@192.168.247.132

user1@192.168.247.132's password: 【这里显示仍然需要密码,上述操作有问题,见下方】

问题在于存放的aothorized_keys的目录不对,应该是存放在B机器的/root/.ssh/authorized_keys

转载于:https://my.oschina.net/u/3461300/blog/1585201

2017.12.5 八周第二次课相关推荐

  1. 三周第二次课(12月26)

    三周第二次课(12月26)  3.4 usermod命令 usermod 更改用户属性 usermod -u 111 username 更改用户uid usermod -g 123(grp2) use ...

  2. html输入时从本文框的顶格输入,浙江省绍兴市越城区2017—2018学年八年级第二学期期末语文试卷(15页)-原创力文档...

    浙 江 期 末 绍兴市越城区2017-2018学年第二学期期末试卷 (满分100分,含书写分5分,考试时间120分钟) [徜徉·诗画旅途] 1. 下列语段与<平凡的世界>有关,阅读并完成( ...

  3. Linux学习笔记第八周七次课(4月3日)

    复习(今日无答疑,笔记可以不写) 八.Linux shell基础知识 8.1 shell介绍 每个用户都有自己的shell: Bourne人名,为了纪念他: 搜索zsh命令,#yum list | g ...

  4. 第八周毛概课学习心得

    这一周的毛概课,我们听了同学们的ppt汇报展示,同时上完了整本书上的内容知识点,老师也带我们进行了全书的复习.希望自己课后能多巩固,争取期末考试考个好成绩.

  5. 一周第二次课(3月20日)1.6/1.7 配置IP 1.8 网络问题排查

    2019独角兽企业重金招聘Python工程师标准>>> 1.6/1.7 配置IP 配置IP的作用:1.使虚拟机可以和外部通信,通过远程连接虚拟机 2.使虚拟机可以上网 配置IP的步骤 ...

  6. 针对第八周新生研讨课问题的个人看法

    互联网公司应该收到怎样的监管? 互联网公司联系着当下至关重要的行业,无论是工业,农业,以及人们的日常生活都与物联网公司所掌管的技术与数据息息相关,物联网公司的监管严格与否将涉及绝大多数人的生活,因此, ...

  7. 五周第二次课(4月19日)

    7.6 yum更换国内源 更换yum国内源 cd /etc/yum.repos.d/ rm -f dvd.repo CentOS 7 wget -O /etc/yum.repos.d/CentOS-B ...

  8. 2018.3.20 一周第二次课

    1.6/1.7 配置IP 因为是最小化安装,是没有图形界面的. 用户名:root 密码:ABC123, 如果不可以使用ifconfig,那就需要安装一个包 yum install -y net-too ...

  9. 课下作业(选做)第八周

    课下作业(选做)第八周 课上内容补做: 由于我的电脑之前始终不能连接上数据库,无法通过http://localhost来进入,总是显示服务器被拒绝,导致当时我没能做出.后来,查阅了许多资料并在王老师的 ...

最新文章

  1. 【css】padding 和 margin的区别
  2. Paper Reading: Papers in Frontiers of NLP 2018 collection
  3. SAP Commerce Cloud 产品主数据读取的单步调试
  4. 鸿蒙os编码_如何看待鸿蒙OS代码示例?
  5. 中国科学院数学与系统科学研究院关于2019年招收硕士研究生复试规程
  6. 使用计算机过程存在的问题,中小学教师计算机应用过程中存在的问题及解决方法...
  7. 470款日系文艺LR预设电影质感Lightroom预设PR/PS/AE/FCPX/LUT预设
  8. 易语言使用超级模块 全局热键
  9. 倍福---绝对值编码器位置保存
  10. 分类网络 resnet
  11. 2021年全球与中国孕妇防辐射服行业市场规模及发展前景分析
  12. UWP笔记-消息弹窗自动淡出
  13. linux 查看 man 路径配置文件 man.config,linux中的man(zz)
  14. 计算机术语横幅迎新,内蒙古师范大学用代码写迎新条幅,还有哪些搞笑的迎新标语?...
  15. HDU1870 愚人节的礼物【堆栈+输入输出】
  16. office 文档 在线查看
  17. 图像风格迁移cvpr2020_CVPR 2020 论文大盘点-文本图像篇
  18. vue——echarts更换主题
  19. 四旋翼定高篇之惯导加速度+速度+位置三阶互补融合方案
  20. 为C盘爆满提供的一些解决方式

热门文章

  1. 苹果app商品定价_苹果将调整应用商店定价:中国区应用最低价涨至8元
  2. phpstorm设置 打开文件所在目录_在根目录中配置文件夹
  3. OSChina 周一乱弹 —— 为什么人类和人工智能定要一战
  4. 程序员如何恢复被清空的回收站——记录一下
  5. fiddler everywhere新功能简单说明
  6. 非洲秃鹫优化算法:求解全局优化问题的一种新的自然启发元启发式算法(Matlab代码实现)
  7. android无法接收短信广播,Android BroadcastReceiver接收收到短信的广播
  8. QT 样式表属性完整版
  9. UVA1335 Beijing Guards
  10. CVPR读书笔记[7]:PCA的理解