rsync(一):基本命令和用法_rsync include不生效

本文详细介绍了rsync的基本命令用法,包括基础示例、排除规则(exclude和include)及其生效时机。重点讨论了`--exclude`规则的正确编写方法,以及`--delete`选项的解释,强调了`--exclude`和`--delete`配合使用时,排除的文件不会被删除的原因。此外,还探讨了rsync守护进程(daemon)模式,包括其配置文件`rsyncd.conf`的设置和意义。最后,提到了配置rsync daemon的身份验证和启动方法。
摘要由CSDN通过智能技术生成

2.4.1 基础示例

以下是几个本地同步示例和通过远程shell实现的同步示例,示例中没有使用"-a"选项,目的是为了更清晰地说明各选项的作用。

(1).将/etc/fstab拷贝到/tmp目录下。

[root@xuexi ~]# rsync /etc/fstab /tmp

(2).将/etc/cron.d目录拷贝到/tmp下。

[root@xuexi ~]# rsync -r /etc/cron.d /tmp

该命令会在目标主机上创建/tmp/cron.d目录,并将/etc/cron.d/中的文件放入到/tmp/cron.d/目录中,也就是说默认情况下,是不会在目录路径下创建上层目录/etc的。

(3).将/etc/cron.d目录拷贝到/tmp下,但要求在/tmp下也生成etc子目录。

[root@xuexi ~]# rsync -R -r /etc/cron.d /tmp

其中"-R"选项表示使用相对路径,此相对路径是以目标目录为根的。对于上面的示例,表示在目标上的/tmp下创建etc/cron.d目录,即/tmp/etc/cron.d,etc/cron.d的根"/"代表的就是目标/tmp。

如果要拷贝的源路径较长,但只想在目标主机上保留一部分目录结构,例如要拷贝/var/log/anaconda/*到/tmp下,但只想在/tmp下保留从log开始的目录,如何操作?使用一个点代表相对路径的起始位置即可,也就是将长目录进行划分。

[root@xuexi ~]# rsync -R -r /var/./log/anaconda /tmp

这样,从点开始的目录都是相对路径,其相对根目录为目标路径。所以对于上面的示例,将在目标上创建/tmp/log/anaconda/*。

(4).对远程目录下已存在文件做一个备份。

[root@xuexi ~]# rsync -R -r --backup /var/./log/anaconda /tmp

这样在目标目录下,已存在的文件就被做一个备份,备份文件默认使用"~“做后缀,可以使用”–suffix"指定备份后缀。

[root@xuexi tmp]# ll log/anaconda/
total 3112
-rw------- 1 root root    6668 Jul 14 12:45 anaconda.log
-rw------- 1 root root    6668 Jul 14 11:44 anaconda.log~
-rw------- 1 root root    3826 Jul 14 12:45 ifcfg.log
-rw------- 1 root root    3826 Jul 14 11:44 ifcfg.log~
-rw------- 1 root root 1102699 Jul 14 12:45 journal.log
-rw------- 1 root root 1102699 Jul 14 11:44 journal.log~
-rw------- 1 root root       0 Jul 14 12:45 ks-script-1uLekR.log
-rw------- 1 root root       0 Jul 14 11:44 ks-script-1uLekR.log~
-rw------- 1 root root       0 Jul 14 12:45 ks-script-iGpl4q.log
-rw------- 1 root root       0 Jul 14 11:44 ks-script-iGpl4q.log~
-rw------- 1 root root  160420 Jul 14 12:45 packaging.log
-rw------- 1 root root  160420 Jul 14 11:44 packaging.log~
-rw------- 1 root root   27906 Jul 14 12:45 program.log
-rw------- 1 root root   27906 Jul 14 11:44 program.log~
-rw------- 1 root root   78001 Jul 14 12:45 storage.log
-rw------- 1 root root   78001 Jul 14 11:44 storage.log~
-rw------- 1 root root  197961 Jul 14 12:45 syslog
-rw------- 1 root root  197961 Jul 14 11:44 syslog~

可以使用"–backup-dir"指定备份文件保存路径,但要求保存路径必须存在。

[root@xuexi ~]# mkdir /tmp/log_back

[root@xuexi ~]# rsync -R -r --backup --backup-dir=/tmp/log_back /var/./log/anaconda /tmp

指定备份路径后,默认将不会加备份后缀,除非使用"–suffix"显式指定后缀,如"–suffix=~"。

[root@xuexi tmp]# tree /tmp/log_back/
/tmp/log_back/
└── log
    └── anaconda
        ├── anaconda.log
        ├── ifcfg.log
        ├── journal.log
        ├── ks-script-1uLekR.log
        ├── ks-script-iGpl4q.log
        ├── packaging.log
        ├── program.log
        ├── storage.log
        └── syslog

(5).指定ssh连接参数,如端口、连接的用户、ssh选项等。

[root@xuexi tmp]# >~/.ssh/known_hosts   # 先清空host key以便下面的测试

[root@xuexi tmp]# rsync -e "ssh -p 22 -o StrictHostKeyChecking=no" /etc/fstab 172.16.10.5:/tmp
Warning: Permanently added '172.16.10.5' (RSA) to the list of known hosts.
root@172.16.10.5's password:

可见直接指定ssh参数是生效的。

(6).“–existing"和”–ignore-existing"

"–existing"是只更新目标端已存在的文件。

目前/tmp/{a,b}目录中内容如下,bashrc在a目录中,crontab在b目录中,且a目录中多了一个c子目录。

[root@xuexi ~]# tree /tmp/{a,b}
/tmp/a
├── bashrc
├── c
│   └── find
├── fstab
├── profile
└── rc.local
/tmp/b
├── crontab
├── fstab
├── profile
└── rc.local
 
1 directory, 9 files

使用"–existing"选项使得只更新目标端已存在的文件。

[root@xuexi ~]# rsync -r -v --existing /tmp/a/ /tmp/b           
sending incremental file list
fstab
profile
rc.local
 
sent 2972 bytes  received 70 bytes  6084.00 bytes/sec
total size is 204755  speedup is 67.31

结果只有3个目标上已存在的文件被更新了,由于目标上没有c目录,所以c目录中的文件也没有进行传输。

而"–ignore-existing"是更新目标端不存在的文件。

[root@xuexi ~]# rsync -r -v --ignore-existing /tmp/a/ /tmp/b
sending incremental file list
bashrc
c/
c/find
 
sent 202271 bytes  received 54 bytes  404650.00 bytes/sec
total size is 204755  speedup is 1.01

“–existing"和”–ignore-existing"结合使用时,有个特殊功效,当它们结合"–delete"使用的时候,文件不会传输,但会删除receiver端额外多出的文件。

**$ mkdir a b
$ touch a/{1..4}.txt
$ touch b/****a.log**

**$ rsync** **-nrv --delete a/ b/**
sending incremental file list
deleting a.log
1.txt
2.txt
3.txt
4.txt
 
sent 118 bytes  received 33 bytes  302.00 bytes/sec
total size is 0  spee
  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值