八周二次课(1月30日)
10.28 rsync工具介绍
10.29/10.30 rsync常用选项
10.31 rsync通过ssh同步
一、rsync工具介绍
Linux系统下有很多数据备份工具,常用的是rsync,从字面意思理解为remote sync(远程同步)。rsync不仅可以远程同步数据(类似于scp),而且可以本地同步数据(类似于cp),但不同于cp或者scp的一点是,它不会覆盖以前的数据(如果数据已经存在),而是先判断已经存在的数据和新数据的差异,只有数据不同时才会把不相同的部分覆盖。
- 如果没有rsync命令,请使用yum install -y rsync安装. 举例:
[root@localhost ~]# rsync -av /etc/passwd /tmp/1.txt
sending incremental file list
passwd
sent 2465 bytes received 31 bytes 4992.00 bytes/sec
total size is 2391 speedup is 0.96
上例中把/etc/passwd同步到/tmp/目录下,并改名为1.txt,如果要改成远程复制,数据备份的格式是: 用户名@IP:path,比如192.168.188.128:/root/.具体用法如下:
[root@localhost ~]# rsync -av /etc/passwd root@192.168.72.133:/tmp/1.txt
The authenticity of host '192.168.72.133 (192.168.72.133)' can't be established.
ECDSA key fingerprint is 54:1a:44:33:2e:df:c2:58:41:cf:f3:d2:e3:69:87:b7.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.72.133' (ECDSA) to the list of known hosts.
root@192.168.72.133's password:
sending incremental file list
passwd
sent 2465 bytes received 253 bytes 175.35 bytes/sec
total size is 2391 speedup is 0.88
- 这里要求远程的机器也必须安装有rsync,需要验证密码是因为这里没有做两机互联.
rsync是一个功能非常强大的工具,其命令也有很多功能特色选项。
1.命令语法格式:
- [ ] rsync [OPTION]... SRC DEST
- [ ] rsync [OPTION]... SRC [USER@]host:DEST // 第一例不加user@host,默认的就是root
- [ ] rsync [OPTION]... [USER@]HOST:SRC DEST//从远程目录同步数据到本地
- [ ] rsync [OPTION]... [USER@]HOST::SRC DEST
- [ ] rsync [OPTION]... SRC [USER@]HOST::DEST
2.rsync常用选项
- [ ] -a :包含-rtplgoD,a选项后面可以跟--no-OPTION这个表示关闭-rlptgoD中的某一个例如 -a--no-l 等同于-rptgoD
- [ ] -r :同步目录时要加上,类似cp时的-r选项
- [ ] -v :同步时显示-些信息,让我们知道同步的过程
- [ ] -l :保留软连接
- [ ] -L :加上该选项后,同步软连接时会把源文件给同步
- [ ] -p :保持文件的权限属性
- [ ] -o :保持文件的属主
- [ ] -g :保持文件的属组
- [ ] -D :保持设备文件信息
- [ ] -t :保持文件的时间属性
- [ ] --delete :删除DEST中SRC没有的文件
- [ ] --exclude :过滤指定文件,如--exclude “logs”会把文件名含logs的文件或者目录过滤掉,不同步
- [ ] -P :显示同步过程,比如速率,比-v更加详细
- [ ] -u :加上该选项后,如果DEST中的文件比SRC新,则不同步
- [ ] -z :传输时压缩
上述选项笔记多,常用的有-a,-v,-z,--delete和--exclude.
3.测试rsync的选项 为了更换的测试,需要新建目录和文件
[root@localhost ~]# ls
anaconda-ks.cfg grep httpd-2.4.29.tar.gz [root@localhost split_dir
awk httpd-2.4.29 initial-setup-ks.cfg sed xaa
## ls 下没有合适的目录和文件,只能重新建
[root@localhost ~]# mkdir rsync
[root@localhost ~]# cd rsync
[root@localhost rsync]# mkdir 111
[root@localhost rsync]# touch 1 2 3 /root/123.txt
[root@localhost rsync]# ln -s /root/123.txt ./134.txt //建立软链接
[root@localhost rsync]# ls -l
总用量 0
-rw-r--r-- 1 root root 0 1月 31 23:22 1
3.1 -a选项用法
3.2 使用-L的用法
[root@localhost ~]# rsync -avL ./rsync/ ./test2/
sending incremental file list
111/
111/123.txt
sent 158 bytes received 35 bytes 386.00 bytes/sec
total size is 0 speedup is 0.00
[root@localhost ~]# ls -l test2/
总用量 0
-rw-r--r-- 1 root root 0 1月 31 23:22 1
drwxr-xr-x 2 root root 21 1月 31 23:50 111
-rw-r--r-- 1 root root 0 1月 31 23:22 134.txt
-rw-r--r-- 1 root root 0 1月 31 23:22 2
-rw-r--r-- 1 root root 0 1月 31 23:22 3
-rw-r--r-- 1 root root 0 1月 31 23:22 321.txt
3.3 使用delete选项
[root@localhost ~]# rsync -avL --delete ./rsync/ ./test2/
sending incremental file list
sent 116 bytes received 13 bytes 258.00 bytes/sec
total size is 0 speedup is 0.00
[root@localhost ~]# ls
123.txt 556.txt awk httpd-2.4.29 initial-setup-ks.cfg rsync split_dir xaa
321.txt anaconda-ks.cfg grep httpd-2.4.29.tar.gz [root@localhost sed test2
3.4 使用--exclude选项
[root@localhost ~]# mkdir test1
[root@localhost ~]# touch test1/4
[root@localhost ~]# rsync -a --exclude="4" test1/ test2/
[root@localhost ~]# ls test1
4
三、通过ssh方式同步,rsync应用
第一种方法:把文件推出去
在之前介绍的rsync的5种命令格式中,第二种和第三种(一个冒号)就属于通过ssh的方式,备份数据,这种方式其实就是让用户登入到远程机器,然后执行rsync的任务
[root@localhost ~]# rsync -av test2/ 192.168.72.133:/tmp/xavi/
root@192.168.72.133's password:
sending incremental file list
./
1
134.txt
2
3
321.txt
111/
111/123.txt
sent 356 bytes received 133 bytes 88.91 bytes/sec
total size is 0 speedup is 0.00
上述就是前面介绍的第二种方式了,通过ssh复制的数据,需要输入另外以太极的root账户的密码。
当然也可以用第三种方式复制,如下所示:
1.命令语法格式:
- [ ] rsync [OPTION]... SRC DEST
- [ ] rsync [OPTION]... SRC [USER@]host:DEST // 第一例不加user@host,默认的就是root
- [ ] rsync [OPTION]... [USER@]HOST:SRC DEST//从远程目录同步数据到本地
- [ ] rsync [OPTION]... [USER@]HOST::SRC DEST
- [ ] rsync [OPTION]... SRC [USER@]HOST::DEST
- 当然也可以第三种方式复制,如下所示(类似于把远程那边的文件拉过来)
[root@localhost ~]# rsync -avP 192.168.72.133:/tmp/xavi/ /tmp/test1
root@192.168.72.133's password:
receiving incremental file list
created directory /tmp/test1
./
1
0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=6/8)
134.txt
0 100% 0.00kB/s 0:00:00 (xfer#2, to-check=5/8)
2
0 100% 0.00kB/s 0:00:00 (xfer#3, to-check=4/8)
3
0 100% 0.00kB/s 0:00:00 (xfer#4, to-check=3/8)
321.txt
0 100% 0.00kB/s 0:00:00 (xfer#5, to-check=2/8)
111/
111/123.txt
0 100% 0.00kB/s 0:00:00 (xfer#6, to-check=0/8)
sent 132 bytes received 361 bytes 89.64 bytes/sec
total size is 0 speedup is 0.00
[root@localhost ~]# rsync -avP 192.168.72.133:/tmp/xavi/ /tmp/test1
root@192.168.72.133's password:
receiving incremental file list
created directory /tmp/test1
./
1
0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=6/8)
134.txt
0 100% 0.00kB/s 0:00:00 (xfer#2, to-check=5/8)
2
0 100% 0.00kB/s 0:00:00 (xfer#3, to-check=4/8)
3
0 100% 0.00kB/s 0:00:00 (xfer#4, to-check=3/8)
321.txt
0 100% 0.00kB/s 0:00:00 (xfer#5, to-check=2/8)
111/
111/123.txt
0 100% 0.00kB/s 0:00:00 (xfer#6, to-check=0/8)
sent 132 bytes received 361 bytes 89.64 bytes/sec
total size is 0 speedup is 0.00
- 制定端口的连接 -e "ssh -p 22"
[root@localhost ~]# rsync -avP -e "ssh -p 22" /etc/passwd 192.168.72.133:/tmp/xavi.txt
root@192.168.72.133's password:
sending incremental file list
passwd
2391 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1)
sent 2465 bytes received 31 bytes 713.14 bytes/sec
total size is 2391 speedup is 0.96
- ssh切换到远程用户端 ssh -p 22 IP地址