学习笔记
Linux文件同步工具-rsync
rsybc是linux系统下的数据备份工具。rsync不仅可以远程同步数据,而且可以本地同步数据,和cp类似,但不同的是它不会覆盖以前的数据,只会同步两个文件不同的部分。
安装rsync
yum install -y rsync
rsync同步数据
[root@qklinux-01 ~]# rsync -av /etc/passwd /tmp/1.txt
sending incremental file list
passwd
sent 1,362 bytes received 35 bytes 2,794.00 bytes/sec
total size is 1,270 speedup is 0.91
rsync远程同步数据:rsync -av /etc/passwd root@192.168.18.138:/tmp/1.txt
[root@qklinux-01 ~]# rsync -av /etc/passwd root@192.168.18.138:/tmp/1.txt
The authenticity of host '192.168.18.138 (192.168.18.138)' can't be established.
ECDSA key fingerprint is SHA256:qSoq7MQm5zLlDDn7I+tJN5wcUKslpA4XtPgvhqTFG0M.
ECDSA key fingerprint is MD5:6a:6f:43:0e:35:68:2b:ce:1a:0b:ae:e8:ac:f5:95:42.
rsync格式OPTION是选项 比如-av
rsync[OPTION]… SRC DEST //SRC是源目录 DEST是目标文件或目录
rsync[OPTION]… SRC [USER@]host:DEST // 源拷贝到远程服务器 USER@也可以省掉
默认的是当前终端用户rsync[OPTION]… [USER@]host:SRC DEST 先写远程 后写本地
rsync[OPTION]… SRC [user@]host: :DEST
rsync[OPTION]… [user@]host::SRC DEST
rsync常用选项
-a包含-rtplgoD
-r 同步目录是要加上,类似cp时的-r选项
-v同步时显示一些信息,让我们知道同步的过程
-l 保留软连接
-L 加上该选项后,同步软连接时会把源文件给同步
-p 保持文件的权限属性
-o 保持文件的属主
-g 保持文件的属组
-D 保持设备文件信息
-t 保持文件的时间属性
–delte删除DEST中SRC没有的文件
–exclude过滤指定文件,如—exclude “logs” 会把文件名包含logs的文件或者目录过滤掉,不同步
-P 显示同步过程,比如速率,比-v更加详细
-u加上该选项后,如果DEST中的文件比SRC新 ,则不同步
-z 传输时压缩
实验说明本地同步数据/root/111/同步到/tmp/111_dest/并改名
[root@qklinux-01 ~]# rsync -av /root/111/ /tmp/111_dest/
sending incremental file list
./
.12.txt.swp
.12.txt.swx
12.txt
12_txt.swp
qkailinux.log -> yum.log
yum.log.bak
222/
sent 4,613 bytes received 121 bytes 9,468.00 bytes/sec
total size is 4,168 speedup is 0.88
rsync通过ssh同步
远程同步文件
[root@qklinux-01 ~]# rsync -av /etc/passwd 192.168.18.139:/tmp/qkai.txt
root@192.168.18.139's password:
sending incremental file list
passwd
sent 1,362 bytes received 35 bytes 310.44 bytes/sec
total size is 1,270 speedup is 0.91
在02机器上查看同步文件
[root@qklinux-02 ~]# cat /tmp/qkai.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
也可以反着同步回本机
[root@qklinux-01 ~]# rsync -avP 192.168.18.139:/tmp/qkai.txt /tmp/123.txt
root@192.168.18.139's password:
Permission denied, please try again.
root@192.168.18.139's password:
receiving incremental file list
qkai.txt
1,270 100% 1.21MB/s 0:00:00 (