8.3. rsync同一主机不同目录的同步

Date:

2018-10

8.3.1. 本地环境

系统版本

CentOS release 6.6 (Final)

主机名

rsync_bak_01

硬件环境

x86_64

网络配置

eth0(dhcp):192.168.161.137

bind软件

rsync-3.0.6-12.el6.x86_64

8.3.2. rsync本地同步命令

备注

同步是源和目标目录都需要以 / 结尾。如果不以此结尾则会被rsync认为是同步这个目录及目录中的内容。

8.3.2.1. 推送源文件到指定目录

 1[root@rsync_bak_01 ~]# ll
 2total 40
 3-rw-------. 1 root root  1139 Oct 20 22:40 anaconda-ks.cfg
 4-rw-r--r--. 1 root root 21682 Oct 20 22:40 install.log
 5-rw-r--r--. 1 root root  5890 Oct 20 22:39 install.log.syslog
 6[root@rsync_bak_01 ~]# pwd
 7/root
 8[root@rsync_bak_01 ~]# ll /tmp/
 9total 0
10-rw-------. 1 root root 0 Oct 20 22:34 yum.log
11[root@rsync_bak_01 ~]# rsync /root/anaconda-ks.cfg /tmp/
12[root@rsync_bak_01 ~]# ll /tmp/
13total 4
14-rw-------  1 root root 1139 Oct 29 03:42 anaconda-ks.cfg
15-rw-------. 1 root root    0 Oct 20 22:34 yum.log

8.3.2.2. 推送源目录所有内容到指定目录

 1[root@rsync_bak_01 ~]# ll
 2total 40
 3-rw-------. 1 root root  1139 Oct 20 22:40 anaconda-ks.cfg
 4-rw-r--r--. 1 root root 21682 Oct 20 22:40 install.log
 5-rw-r--r--. 1 root root  5890 Oct 20 22:39 install.log.syslog
 6[root@rsync_bak_01 ~]# ll /tmp/
 7total 4
 8-rw-------  1 root root 1139 Oct 29 03:42 anaconda-ks.cfg
 9-rw-------. 1 root root    0 Oct 20 22:34 yum.log
10[root@rsync_bak_01 ~]# rsync -avz /root/ /tmp/
11sending incremental file list
12./
13.bash_history
14.bash_logout
15.bash_profile
16.bashrc
17.cshrc
18.tcshrc
19anaconda-ks.cfg
20install.log
21install.log.syslog
22
23sent 9049 bytes  received 186 bytes  18470.00 bytes/sec
24total size is 33035  speedup is 3.58
25[root@rsync_bak_01 ~]# ll /tmp/
26total 36
27-rw-------  1 root root  1139 Oct 20 22:40 anaconda-ks.cfg
28-rw-r--r--  1 root root 21682 Oct 20 22:40 install.log
29-rw-r--r--  1 root root  5890 Oct 20 22:39 install.log.syslog
30-rw-------. 1 root root     0 Oct 20 22:34 yum.log
31[root@rsync_bak_01 ~]# ll /root/
32total 40
33-rw-------. 1 root root  1139 Oct 20 22:40 anaconda-ks.cfg
34-rw-r--r--. 1 root root 21682 Oct 20 22:40 install.log
35-rw-r--r--. 1 root root  5890 Oct 20 22:39 install.log.syslog

8.3.2.3. 同步时如果源目录没有则删除目标目录对应内容

 1[root@rsync_bak_01 ~]# ll /data
 2total 0
 3[root@rsync_bak_01 ~]# ll /tmp/
 4total 56
 5drwxr-xr-x  2 root root  4096 Oct 29 03:47 a
 6-rw-------  1 root root  1139 Oct 20 22:40 anaconda-ks.cfg
 7drwxr-xr-x  2 root root  4096 Oct 29 03:47 b
 8drwxr-xr-x  2 root root  4096 Oct 29 03:47 c
 9drwxr-xr-x  2 root root  4096 Oct 29 03:47 d
10-rw-r--r--  1 root root 21682 Oct 20 22:40 install.log
11-rw-r--r--  1 root root  5890 Oct 20 22:39 install.log.syslog
12drwxr-xr-x  2 root root  4096 Oct 29 03:47 test
13-rw-------. 1 root root     0 Oct 20 22:34 yum.log
14[root@rsync_bak_01 ~]# rsync -r --delete /data/ /tmp/
15[root@rsync_bak_01 ~]# ll /tmp/
16total 0