rsync工具介绍、rsync常用选项、rsync通过ssh同步

目录

一、rsync工具介绍
二、rsync常用选项
三、rsync通过ssh同步

一、rsync工具介绍

rsync命令是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件。

rsync不仅可以远程同步数据(类似于scp),而且可以本地同步数据(类似于cp),但不同于cp或scp的一点是,它不会覆盖以前的数据(如果数据已经存在),而是先判断已经存在的数据和新数据的差异,只有数据不同时才会把不相同的部分覆盖。

系统中没有rsync,执行yum install -y rsync命令安装rsync。

  • rsync 的命令格式
    rsync [OPTION]... SRC DEST
    rsync [OPTION]... SRC [USER@]HOST:DEST
    rsync [OPTION]... [USER@]HOST:SRC DEST
    rsync [OPTION]... [USER@]HOST::SRC DEST
    rsync [OPTION]... SRC [USER@]HOST::DEST

举例:
把/etc/passwd同步到本地/tmp/目录下,并改名为a.txt。

[root@minglinux-01 ~]# rsync -av /etc/passwd /tmp/a.txt
sending incremental file list
passwd

sent 1,184 bytes  received 35 bytes  2,438.00 bytes/sec
total size is 1,092  speedup is 0.90
[root@minglinux-01 ~]# ll !$
ll /tmp/a.txt
-rw-r--r-- 1 root root 1092 10月 24 23:26 /tmp/a.txt

把/etc/passwd远程数据备份,形式为:用户名[@IP]:路径,比如root@192.168.162.132:/root/,对方机器也需要安装rsync,示例命令如下:

[root@minglinux-01 ~]# rsync -av /etc/passwd root@192.168.162.132:/root/a.txt
root@192.168.162.132's password: 
sending incremental file list
passwd

sent 1,184 bytes  received 35 bytes  221.64 bytes/sec
total size is 1,092  speedup is 0.90

[root@minglinux-02 ~]# ls    //minglinux-02IP地址即192.168.162.132
anaconda-ks.cfg  a.txt
[root@minglinux-02 ~]# ll a.txt 
-rw-r--r--. 1 root root 1092 10月 24 23:26 a.txt

二、rsync常用选项

rsync命令常用选项:

-a:这是归档模式,表示以递归方式传输文件,并保持所有属性,它等同于-rlptgoD。-a选项后面可以跟一个--no-OPTION,表示关闭-rlptgoD中的某一个,比如-a--no-l等同于-rptgoD。
-r:表示以递归模式处理子目录。它主要是针对目录来说的,如果单独传一个文件不需要加-r选项,但是传输目录时必须加。
-v:表示打印一些信息,比如文件列表、文件数量等。
-l:表示保留软连接。
-L:表示像对待常规文件一样处理软连接。如果是SRC中有软连接文件,则加上该选项后,将会把软连接指向的目标文件复制到DST。
-p:表示保持文件权限。
-o:表示保持文件属主信息。
-g:表示保持文件属组信息。
-D:表示保持设备文件信息。
-t:表示保持文件时间信息。
--delete:表示删除DST中SRC没有的文件。
--exclude=PATTERN:表示指定排除不需要传输的文件,等号后面跟文件名,可以是万用字符模式(如*.txt)。
--progress:表示在同步的过程中可以看到同步的过程状态,比如统计要同步的文件数量、同步的文件传输速度等。
-u:表示把DST中比SRC还新的文件排除掉,不会覆盖。
-z:加上该选项,将会在传输过程中压缩

选项使用练习:

  1. 建立练习用的目录和文件
[root@minglinux-01 ~]# mkdir rsync
[root@minglinux-01 ~]# cd rsync/
[root@minglinux-01 rsync]# mkdir test1
[root@minglinux-01 rsync]# cd test1/
[root@minglinux-01 test1]# touch 1 2 3 /root/123.tzr
[root@minglinux-01 test1]# touch 1 2 3 /root/123.txt
[root@minglinux-01 test1]# ln -s /root/123.txt ./123.txt
[root@minglinux-01 test1]# ll
总用量 0
-rw-r--r-- 1 root root  0 10月 31 22:47 1
lrwxrwxrwx 1 root root 13 10月 31 22:48 123.txt -> /root/123.txt
-rw-r--r-- 1 root root  0 10月 31 22:47 2
-rw-r--r-- 1 root root  0 10月 31 22:47 3
  1. 使用-a选项

使用rsync备份目录时要在源目录和目标目录后面加上斜杠/,否则达不到复制效果。

示例命令如下:

[root@minglinux-01 rsync]# rsync -a test1/ test2/
[root@minglinux-01 rsync]# ll test2/
总用量 0
-rw-r--r-- 1 root root  0 10月 31 22:47 1
lrwxrwxrwx 1 root root 13 10月 31 22:48 123.txt -> /root/123.txt
-rw-r--r-- 1 root root  0 10月 31 22:47 2
-rw-r--r-- 1 root root  0 10月 31 22:47 3

-a选项等同于-rlptgoD,且-a还可以和--no-OPTIN一并使用。
使用--no-l不备份链接文件,示例命令如下:

[root@minglinux-01 rsync]# rm -rf test2 
[root@minglinux-01 rsync]# ls
test1
[root@minglinux-01 rsync]# rsync -av --no-l test1/ test2/
sending incremental file list
created directory test2
skipping non-regular file "123.txt"
./
1
2
3

sent 229 bytes  received 144 bytes  746.00 bytes/sec
total size is 13  speedup is 0.03
  1. 使用-L选项
    加上-L选项就可以把SRC中软连接的目标文件复制到DST。示例命令如下:
[root@minglinux-01 rsync]# rm -rf test2 
[root@minglinux-01 rsync]# rsync -avL test1/ test2/
sending incremental file list
created directory test2
./
1
123.txt
2
3

sent 260 bytes  received 123 bytes  766.00 bytes/sec
total size is 0  speedup is 0.00
[root@minglinux-01 rsync]# ll test2/
总用量 0
-rw-r--r-- 1 root root 0 10月 31 22:47 1
-rw-r--r-- 1 root root 0 10月 31 22:47 123.txt
-rw-r--r-- 1 root root 0 10月 31 22:47 2
-rw-r--r-- 1 root root 0 10月 31 22:47 3
  1. 使用-u选项

首先查看test1/1和test2/1的创建时间是一样的:

[root@minglinux-01 rsync]#  ll test1/1 test2/1 
-rw-r--r-- 1 root root 0 10月 31 22:47 test1/1
-rw-r--r-- 1 root root 0 10月 31 22:47 test2/1

面修改test2/1的创建时间,然后使用rsync不加-u同步:

[root@minglinux-01 rsync]# echo "1" > test2/1
[root@minglinux-01 rsync]# ll !$
ll test2/1
-rw-r--r-- 1 root root 2 10月 31 23:09 test2/1
[root@minglinux-01 rsync]# rsync -a test1/1 test2/
[root@minglinux-01 rsync]# ll test2/1
-rw-r--r-- 1 root root 0 10月 31 22:47 test2/1

上面test2/1的创建时间还是和test1/1一样。下面加上-u选项:

[root@minglinux-01 rsync]# echo "123" > test2/1
[root@minglinux-01 rsync]# ll test2/1
-rw-r--r-- 1 root root 4 10月 31 23:12 test2/1
[root@minglinux-01 rsync]# rsy
rsync                  rsyslogd               rsyslog-recover-qi.pl
[root@minglinux-01 rsync]# rsync -avu test1/ test2/
sending incremental file list
./
123.txt -> /root/123.txt

sent 129 bytes  received 22 bytes  302.00 bytes/sec
total size is 13  speedup is 0.09
[root@minglinux-01 rsync]# ll test1/1 test2/1
-rw-r--r-- 1 root root 0 10月 31 22:47 test1/1
-rw-r--r-- 1 root root 4 10月 31 23:12 test2/1

加上-u选项后,不会再把test1/1同步为test2/1了。

  1. 使用--delete选项

首先删除test1/123.txt,然后把test1/目录同步到test2/目录下:

[root@minglinux-01 rsync]# rm -f test1/123.txt 
[root@minglinux-01 rsync]# ls test1/
1  2  3
[root@minglinux-01 rsync]# rsync -av test1/ test2/
sending incremental file list
./
1

sent 127 bytes  received 38 bytes  330.00 bytes/sec
total size is 0  speedup is 0.00
[root@minglinux-01 rsync]# ls test2/
1  123.txt  2  3

上例中,test2/目录并没有删除test1目录中没有的123.txt。下面加上--delete选项,示例如下:

[root@minglinux-01 rsync]# rsync -av --delete test1/ test2/
sending incremental file list
deleting 123.txt

sent 81 bytes  received 23 bytes  208.00 bytes/sec
total size is 0  speedup is 0.00
[root@minglinux-01 rsync]# ls test2/
1  2  3

这样test2/目录下的123.txt也被删除了。

如果在DST中增加文件了,而SRC当中没有这些文件,同步时加上
--delete选项后同样会删除新增的文件。如下所示:

[root@minglinux-01 rsync]# ls test2/
1  2  3
[root@minglinux-01 rsync]# touch test2/4
[root@minglinux-01 rsync]# ls test1/
1  2  3
[root@minglinux-01 rsync]# ls test2/
1  2  3  4
[root@minglinux-01 rsync]# rsync -a --delete test1/ test2/
[root@minglinux-01 rsync]# ls test1/ test2/
test1/:
1  2  3

test2/:
1  2  3
  1. 使用--exclude选项
[root@minglinux-01 rsync]# touch test1/4
[root@minglinux-01 rsync]# rsync -a --exclude="4" test1/ test2/
[root@minglinux-01 rsync]# ls test1/ test2/
test1/:
1  2  3  4

test2/:
1  2  3

三、rsync通过ssh同步

前面介绍过的rsync的5种命令格式中如下两种就属于通过ssh的方式备份数据。这种方式其实就是让用户登录到远程机器,然后执行rsync的任务。

rsync [OPTION]... SRC [USER@]HOST:DEST
rsync [OPTION]... [USER@]HOST:SRC DEST

第一种方式已经实现过了,下面试试第二种方式:

[root@minglinux-01 rsync]# rsync -avL 192.168.162.132:/root/a.txt ./test3
root@192.168.162.132's password: 
receiving incremental file list
a.txt

sent 43 bytes  received 1,183 bytes  272.44 bytes/sec
total size is 1,092  speedup is 0.89
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,372评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,368评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,415评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,157评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,171评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,125评论 1 297
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,028评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,887评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,310评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,533评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,690评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,411评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,004评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,659评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,812评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,693评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,577评论 2 353

推荐阅读更多精彩内容