rsync命令基础

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

同步工具rsync介绍

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

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

rsync命令就十分使用于这样的状况,能够实现增量备份ssh

  • 本地同步
[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.txt
The 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 list
passwd

sent 1156 bytes  received 31 bytes  67.83 bytes/sec
total size is 1082  speedup is 0.91
  • 远程同步
# 指定远程主机的格式能够不指定用户名,默认是指远程主机上的当前用户
[root@localhost ~]# rsync -av /etc/passwd root@192.168.65.130:/tmp/1.txt
root@192.168.65.133's password: 
sending incremental file list

sent 31 bytes  received 12 bytes  3.19 bytes/sec
total size is 1128  speedup is 26.23

rsync命令经常使用选项

  • -a 包含-rtplhoD
  • -v 显示同步的过程
[root@localhost ~]# rsync -av /root/111/ /tmp/111_dest/
sending incremental file list
created directory /tmp/111_dest
./
file.py
test -> /etc/passwd
try.py
222/

sent 576 bytes  received 60 bytes  1272.00 bytes/sec
total size is 382  speedup is 0.60

[root@localhost ~]# ls -l 111/
总用量 8
drwxr-xr-x. 2 root root   6 10月 27 19:49 222
-rw-r--r--. 1 root root 146 10月 27 19:45 file.py
lrwxrwxrwx. 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/
总用量 8
drwxr-xr-x. 2 root root   6 10月 27 19:49 222
-rw-r--r--. 1 root root 146 10月 27 19:45 file.py
lrwxrwxrwx. 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 list
test

sent 1265 bytes  received 32 bytes  2594.00 bytes/sec
total size is 1499  speedup is 1.16

[root@localhost ~]# ls -l /tmp/111_dest/
总用量 12
drwxr-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/
总用量 8
drwxr-xr-x. 2 root root   6 10月 27 19:49 222
-rw-r--r--. 1 root root 146 10月 27 19:45 file.py
lrwxrwxrwx. 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.txt

sent 97 bytes  received 16 bytes  226.00 bytes/sec
total size is 1499  speedup is 13.27
  • --exclude 过滤指定文件
[root@localhost ~]# rsync -avL --exclude "*.py" /root/111/ /tmp/111_dest/
sending incremental file list
created directory /tmp/111_dest
./
test
222/

sent 1244 bytes  received 38 bytes  2564.00 bytes/sec
total size is 1128  speedup is 0.88
  • -P 显示同步过程,例如速率,比-v更详细
[root@localhost ~]# rsync -aP /root/111/ /tmp/111_dest/
sending incremental file list
created directory /tmp/111_dest
./
file.py
         146 100%    0.00kB/s    0:00:00 (xfer#1, to-check=3/5)
test -> /etc/passwd
try.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/sec
total 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/sec
total size is 382  speedup is 2.96

# 验证
# 目标文件添加一行注释,较源文件更新
[root@localhost ~]# cat /tmp/111_dest/try.py 
#!/usr/bin/env python
# test
try:
    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 python

try:
    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 list
try.py
         225 100%    0.00kB/s    0:00:00 (xfer#1, to-check=1/5)

sent 297 bytes  received 32 bytes  658.00 bytes/sec
total size is 382  speedup is 1.16

其余的几个参数(因为-a默认包括了不少参数,因此几乎使用-a替代了)工具

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

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

rsync经过ssh同步

前提:本机和远程主机都须要安装rsynccode

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

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

[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
相关文章
相关标签/搜索