博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
rsync命令基础
阅读量:7116 次
发布时间:2019-06-28

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

hot3.png

10月31日任务
10.28 rsync工具介绍
10.29/10.30 rsync常用选项
10.31 rsync通过ssh同步
 

同步工具rsync介绍

默认未安装,使用yum install -y rsync

拷贝一个目录的数据到另一个目录,拷贝的同时目录的内容处在不断更新中,在这里使用cp命令就可能会有些问题:无法实现增量拷贝,浪费时间;多次全量拷贝,对磁盘的读写压力大;

rsync命令就十分使用于这样的情况,可以实现增量备份

  • 本地同步
[root@localhost system]# rysnc -av /etc/passwd root@192.168.65.133:/tmp/1.txt-bash: rysnc: 未找到命令[root@localhost system]# rsync -av /etc/passwd root@192.168.65.133:/tmp/1.txtThe authenticity of host '192.168.65.133 (192.168.65.133)' can't be established.ECDSA key fingerprint is 42:50:a7:09:91:db:af:77:a5:3a:b3:67:1c:8a:5b:99.Are you sure you want to continue connecting (yes/no)? yes     Warning: Permanently added '192.168.65.133' (ECDSA) to the list of known hosts.root@192.168.65.133's password: sending incremental file listpasswdsent 1156 bytes  received 31 bytes  67.83 bytes/sectotal size is 1082  speedup is 0.91
  • 远程同步
# 指定远程主机的格式可以不指定用户名,默认是指远程主机上的当前用户[root@localhost ~]# rsync -av /etc/passwd root@192.168.65.130:/tmp/1.txtroot@192.168.65.133's password: sending incremental file listsent 31 bytes  received 12 bytes  3.19 bytes/sectotal size is 1128  speedup is 26.23

rsync命令常用选项

  • -a 包含-rtplhoD
  • -v 显示同步的过程
[root@localhost ~]# rsync -av /root/111/ /tmp/111_dest/sending incremental file listcreated directory /tmp/111_dest./file.pytest -> /etc/passwdtry.py222/sent 576 bytes  received 60 bytes  1272.00 bytes/sectotal size is 382  speedup is 0.60[root@localhost ~]# ls -l 111/总用量 8drwxr-xr-x. 2 root root   6 10月 27 19:49 222-rw-r--r--. 1 root root 146 10月 27 19:45 file.pylrwxrwxrwx. 1 root root  11 10月 27 19:49 test -> /etc/passwd-rw-r--r--. 1 root root 225 10月 27 19:45 try.py[root@localhost ~]# ls -l /tmp/111_dest/总用量 8drwxr-xr-x. 2 root root   6 10月 27 19:49 222-rw-r--r--. 1 root root 146 10月 27 19:45 file.pylrwxrwxrwx. 1 root root  11 10月 27 19:49 test -> /etc/passwd-rw-r--r--. 1 root root 225 10月 27 19:45 try.py
  • -L 同步软链接时会把源文件同步 (不会同步软链接文件,而是同步被链接的源文件)
[root@localhost ~]# rsync -avL /root/111/ /tmp/111_dest/sending incremental file listtestsent 1265 bytes  received 32 bytes  2594.00 bytes/sectotal size is 1499  speedup is 1.16[root@localhost ~]# ls -l /tmp/111_dest/总用量 12drwxr-xr-x. 2 root root    6 10月 27 19:49 222-rw-r--r--. 1 root root  146 10月 27 19:45 file.py-rw-r--r--. 1 root root 1128 10月 23 20:22 test-rw-r--r--. 1 root root  225 10月 27 19:45 try.py[root@localhost ~]# ls -l 111/总用量 8drwxr-xr-x. 2 root root   6 10月 27 19:49 222-rw-r--r--. 1 root root 146 10月 27 19:45 file.pylrwxrwxrwx. 1 root root  11 10月 27 19:49 test -> /etc/passwd-rw-r--r--. 1 root root 225 10月 27 19:45 try.py
  • --delete 删除目标中源所没有的文件
[root@localhost ~]# rsync --delete -avL /root/111/ /tmp/111_dest/ sending incremental file list./deleting 1.txtsent 97 bytes  received 16 bytes  226.00 bytes/sectotal size is 1499  speedup is 13.27
  • --exclude 过滤指定文件
[root@localhost ~]# rsync -avL --exclude "*.py" /root/111/ /tmp/111_dest/sending incremental file listcreated directory /tmp/111_dest./test222/sent 1244 bytes  received 38 bytes  2564.00 bytes/sectotal size is 1128  speedup is 0.88
  • -P 显示同步过程,例如速率,比-v更详细
[root@localhost ~]# rsync -aP /root/111/ /tmp/111_dest/sending incremental file listcreated directory /tmp/111_dest./file.py         146 100%    0.00kB/s    0:00:00 (xfer#1, to-check=3/5)test -> /etc/passwdtry.py         225 100%  219.73kB/s    0:00:00 (xfer#2, to-check=1/5)222/sent 576 bytes  received 60 bytes  1272.00 bytes/sectotal size is 382  speedup is 0.60
  • -u 目标中的文件比源中新,则不同步
# 修改目标的文件内容,使目标的文件比源新[root@localhost ~]# vi /tmp/111_dest/try.py# 使用-u参数,防止源中文件同步过来[root@localhost ~]# rsync -avu /root/111/ /tmp/111_dest/sending incremental file list./sent 113 bytes  received 16 bytes  258.00 bytes/sectotal size is 382  speedup is 2.96# 验证# 目标文件添加一行注释,较源文件更新[root@localhost ~]# cat /tmp/111_dest/try.py #!/usr/bin/env python# testtry:    filename = raw_input('Enter file name: ')    fobj = open(filename, 'r')    for eachLine in fobj:        print eachLine,    fobj.close()except IOError as e:    print "file open error:", e    [root@localhost ~]# cat 111/try.py #!/usr/bin/env pythontry:    filename = raw_input('Enter file name: ')    fobj = open(filename, 'r')    for eachLine in fobj:        print eachLine,    fobj.close()except IOError as e:    print "file open error:", e
  • -z 传输时进行压缩(可以节省带宽)
[root@localhost ~]# rsync -avPz /root/111/ /tmp/111_dest/sending incremental file listtry.py         225 100%    0.00kB/s    0:00:00 (xfer#1, to-check=1/5)sent 297 bytes  received 32 bytes  658.00 bytes/sectotal size is 382  speedup is 1.16

其他的几个参数(由于-a默认包括了很多参数,所以几乎使用-a替代了)

  • -r 同步目录时需要
  • -l 保留软链接
  • -p 保持文件的权限
  • -o 保持文件的所有者
  • -g 保持文件的所属组
  • -D 保持设备文件信息
  • -t 保持文件的时间属性

P、L等参数配合-a参数使用,会替代默认的p、l参数功能

rsync通过ssh同步

前提:本机和远程主机都需要安装rsync

通过ssh访问来同步文件/目录的几种写法:

把本地文件推(同步)到远端主机

[root@localhost ~]# rsync -av /etc/passwd 192.168.65.130:/tmp/1.txt

将远端主机的文件拉(同步)到本机

[root@localhost ~]# rsync -av 192.168.65.130:/etc/passwd /tmp/1.txt

指定ssh访问的端口

# 这里假设远端ssh端口为66[root@localhost ~]# rsync -av -e "ssh -p 66" /etc/passwd 192.168.133.130:/tmp/1.txt

转载于:https://my.oschina.net/u/3964535/blog/2353446

你可能感兴趣的文章
为discuz x2.5添加播放附件(mp4)的方法
查看>>
SpringMVC深度探险(一) —— SpringMVC前传
查看>>
面试 框架部分
查看>>
display: flex属性介绍
查看>>
mysql复制表的方法
查看>>
镜像复制+copy命令+镜像复制案例
查看>>
Android APP 中英文切换
查看>>
RabbitMQ学习总结(7)——Spring整合RabbitMQ实例
查看>>
模糊查询,多条件查询
查看>>
java JNI 实现原理 (二) Linux 下如何 load JNILibrary
查看>>
内联函数和函数重载
查看>>
<%%>与<%=%>区别
查看>>
Python 中的 if __name__ == '__main__'
查看>>
C++11 并发指南六(atomic 类型详解一 atomic_flag 介绍)
查看>>
kuangbin专题一 简单搜索
查看>>
第二章 单表查询 T-SQL语言基础(2)
查看>>
chrome总是崩溃
查看>>
POJ-1751 Highways---确定部分边的MST
查看>>
在oracle中如何退出edit模式
查看>>
Java类和对象初始化
查看>>