博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【CentOS 7笔记47】,rsync文件同步工具#171205
阅读量:7093 次
发布时间:2019-06-28

本文共 6949 字,大约阅读时间需要 23 分钟。

hot3.png

hellopasswd


文件同步工具rsync

[root@localhost ~]# yum install -y rsyncLoaded plugins: fastestmirrorLoading mirror speeds from cached hostfile * epel: mirrors.tongji.edu.cnResolving Dependencies--> Running transaction check---> Package rsync.x86_64 0:3.0.9-18.el7 will be installed--> Finished Dependency ResolutionDependencies Resolved=================================================================================================== Package              Arch                  Version                      Repository           Size===================================================================================================Installing: rsync                x86_64                3.0.9-18.el7                 base                360 kTransaction Summary===================================================================================================Install  1 PackageTotal download size: 360 kInstalled size: 732 kDownloading packages:rsync-3.0.9-18.el7.x86_64.rpm                                               | 360 kB  00:00:01     Running transaction checkRunning transaction testTransaction test succeededRunning transaction  Installing : rsync-3.0.9-18.el7.x86_64                                                       1/1   Verifying  : rsync-3.0.9-18.el7.x86_64                                                       1/1 Installed:  rsync.x86_64 0:3.0.9-18.el7                                                                      Complete!
[root@localhost ~]# rsync -av /etc/passwd /tmp/1.txtsending incremental file listpasswdsent 1506 bytes  received 31 bytes  3074.00 bytes/sectotal size is 1432  speedup is 0.93

rsync常用选项

  • -a包含-rtplgoD
  • -r同步目录时要加上,类似cp时的-r选项
  • -v同步时显示一些信息,让我们知道同步的过程
  • -l保留软链接
  • -L加上该选项后,同步软链接时会把源文件给同步
  • -p保持文件的权限属性
  • -o保持文件的属主
  • -g保持文件的属组
  • -D保持设备文件信息
  • -t保持文件的时间属性
  • --delete删除DEST中SRC没有的文件
  • --exclude过滤指定文件,如--exclude "logs"会把文件名包含logs的文件或目录过滤掉,不同步
  • -P显示同步过程,比如速率,比-v更加详细
  • -u加上该选项后,如果DEST中的文件比SRC新,则不同步
  • -z传输时压缩

-av

[root@localhost ~]# ls 111  anaconda-ks.cfg[root@localhost ~]# rsync -av /root/111/ /tmp/111_dest/sending incremental file listcreated directory /tmp/111_dest./1.txt2.txt3.txtsent 192 bytes  received 72 bytes  528.00 bytes/sectotal size is 0  speedup is 0.00[root@localhost ~]# ls 111  anaconda-ks.cfg[root@localhost ~]# ls /tmp/111_dest  ipt.txt

--delete

[root@localhost ~]# touch /tmp/111_dest/new.txt[root@localhost ~]# rsync -av /root/111/ /tmp/111_dest/sending incremental file list./sent 75 bytes  received 15 bytes  180.00 bytes/sectotal size is 0  speedup is 0.00[root@localhost ~]# ls /tmp/111_dest/1.txt  2.txt  3.txt  new.txt
[root@localhost ~]# touch /tmp/111_dest/new.txt[root@localhost ~]# rsync -av --delete /root/111/ /tmp/111_dest/sending incremental file list./deleting new.txtsent 75 bytes  received 15 bytes  180.00 bytes/sectotal size is 0  speedup is 0.00[root@localhost ~]# ls /tmp/111_dest/1.txt  2.txt  3.txt

--exclude

[root@localhost ~]# rm -rf /tmp/111_dest/*[root@localhost ~]# rsync -av --exclude "1*" /root/111/ /tmp/111_dest/sending incremental file list./2.txt3.txtsent 139 bytes  received 53 bytes  384.00 bytes/sectotal size is 0  speedup is 0.00[root@localhost ~]# ls /tmp/111_dest/2.txt  3.txt

-avP

[root@localhost ~]# rm -rf /tmp/111_dest/*[root@localhost ~]# rsync -avP /root/111/ /tmp/111_dest/sending incremental file list./1.txt          18 100%    0.00kB/s    0:00:00 (xfer#1, to-check=2/4)2.txt          12 100%   11.72kB/s    0:00:00 (xfer#2, to-check=1/4)3.txt           6 100%    5.86kB/s    0:00:00 (xfer#3, to-check=0/4)sent 240 bytes  received 72 bytes  624.00 bytes/sectotal size is 36  speedup is 0.12

-avPu

[root@localhost ~]# echo "hello" > 111/1.txt [root@localhost ~]# rsync -avP /root/111/ /tmp/111_dest/sending incremental file list1.txt           6 100%    0.00kB/s    0:00:00 (xfer#1, to-check=2/4)sent 121 bytes  received 31 bytes  304.00 bytes/sectotal size is 24  speedup is 0.16[root@localhost ~]# cat /tmp/111_dest/1.txt hello
[root@localhost ~]# echo "hello" >> !$echo "hello" >> /tmp/111_dest/1.txt[root@localhost ~]# rsync -avPu /root/111/ /tmp/111_dest/sending incremental file listsent 72 bytes  received 12 bytes  168.00 bytes/sectotal size is 24  speedup is 0.29[root@localhost ~]# cat /tmp/111_dest/1.txt hellohello[root@localhost ~]# cat 111/1.txt hello

rsync通过ssh同步

  • rsync -av test1/ 192.168.133.132:/tmp/test2/
  • rsync -av -e "ssh -p 22" test1/ 192.168.133.132:/tmp/test2/
  • rsync通过服务的方式同步
  • 要编辑配置文件/etc/rsyncd.conf
  • 启动服务rsync --daemon
  • 格式:rsync -av test1/ 192.168.133.130::module/dir/

Linux主机A:192.168.9.134

[root@localhost ~]# rsync -avP /root/111/ root@192.168.9.233:/root/111_new/The authenticity of host '192.168.9.223 (192.168.9.223)' can't be established.ECDSA key fingerprint is 36:5f:cc:8b:04:f9:7b:2e:0f:8c:39:38:37:c3:d2:3a.Are you sure you want to continue connecting (yes/no)? yPlease type 'yes' or 'no': yes      Warning: Permanently added '192.168.9.223' (ECDSA) to the list of known hosts.root@192.168.9.223's password: bash: rsync: command not foundrsync: 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]

主机B必须也要安装rsync包

[root@localhost ~]# rsync -avP /root/111/ root@192.168.9.233:/root/111_new/root@192.168.9.233's password: sending incremental file listcreated directory /root/111_new./1.txt           6 100%    0.00kB/s    0:00:00 (xfer#1, to-check=2/4)2.txt          12 100%   11.72kB/s    0:00:00 (xfer#2, to-check=1/4)3.txt           6 100%    5.86kB/s    0:00:00 (xfer#3, to-check=0/4)sent 228 bytes  received 72 bytes  120.00 bytes/sectotal size is 24  speedup is 0.08

Linux主机B:192.168.9.223

[root@centos11233 ~]# ls111_new  anaconda-ks.cfg

推文件与拉文件

Linux主机A:192.168.9.134

[root@localhost ~]# rsync -avP root@192.168.9.233:/root/111_new/ /root/111_dest/root@192.168.9.233's password: receiving incremental file listcreated directory /root/111_dest./1.txt           6 100%    5.86kB/s    0:00:00 (xfer#1, to-check=2/4)2.txt          12 100%   11.72kB/s    0:00:00 (xfer#2, to-check=1/4)3.txt           6 100%    5.86kB/s    0:00:00 (xfer#3, to-check=0/4)sent 71 bytes  received 233 bytes  67.56 bytes/sectotal size is 24  speedup is 0.08[root@localhost ~]# ls111  111_dest  anaconda-ks.cfg

指定端口-e "ssh -p 端口号"

[root@localhost ~]# rsync -avP -e "ssh -p 22" /etc/passwd root@192.168.9.233:/root/1.txtroot@192.168.9.233's password: sending incremental file listpasswd        1432 100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/1)sent 1506 bytes  received 31 bytes  614.80 bytes/sectotal size is 1432  speedup is 0.93

扩展

[root@localhost ~]# ssh -p 22 192.168.9.233root@192.168.9.233's password: Last failed login: Wed Dec  6 03:34:39 CST 2017 from 192.168.9.134 on ssh:nottyThere was 1 failed login attempt since the last successful login.Last login: Wed Dec  6 00:33:06 2017 from 192.168.9.1[root@centos11233 ~]# [root@centos11233 ~]# exitlogoutConnection to 192.168.9.233 closed.

修改于 171205

转载于:https://my.oschina.net/hellopasswd/blog/1585347

你可能感兴趣的文章
《12-1-构建之法:现代软件工程-阅读笔记》
查看>>
C#7.0之ref locals and returns (局部变量和引用返回,之前欠大家的,现在补上)
查看>>
【九校3D2T3】世界第一的猛汉王
查看>>
JVM命令-java服务器故障排查
查看>>
c语言第四次作业
查看>>
IntelliJ Idea 常用快捷键列表
查看>>
UGUI案例之 选项卡效果
查看>>
gulp结合Thinkphp配置
查看>>
vim 取消高亮
查看>>
JavaScript 中的相等操作符 ( 详解 [] == []、[] == ![]、{} == !{} )
查看>>
djano-模板层基础知识
查看>>
Linux 下各个目录的作用及内容
查看>>
Angular5学习笔记 - 创建、运行、发布项目(一)
查看>>
探索Java9 模块系统和反应流
查看>>
Use GDB to debug a C++ program called from a shell script
查看>>
java io理解
查看>>
LeetCode 175. Combine Two Tables
查看>>
LeetCode OJ:Majority Element II(主元素II)
查看>>
Qt Creatror使用designer修改了界面但是编译无反应的解决方法
查看>>
数据库索引的作用和优点缺点以及索引的11中用法
查看>>