【9.30】日常运维——rsync、系统日志
10.28 rsync 工具介绍
-
rsync传输数据,备份到远程,类似于 cp
-
rsync不仅可以实现 A 机器到 B 机器,也可以实现从本机 A 目录到 B 目录的数据传输
-
cp 的话,如果是不断写入的文件,cp 过去,覆盖文件,文件过大的话,既浪费时间,又占磁盘 IO,此时使用 rsync
-
rsync可以实现增量拷贝,只增加变更过的文件
-
rsync 拷贝文件
[root@arslinux-01 ~]# rsync -av /etc/passwd /tmp/1.txt
sending incremental file list
passwd
sent 1,371 bytes received 35 bytes 2,812.00 bytes/sec
total size is 1,279 speedup is 0.91
- rsync -av 文件 用户名@IP地址:目标目录/文件名 远程同步、拷贝
[root@arslinux-01 ~]# rsync -av /etc/passwd root@192.168.194.130:/tmp/1.txt
The authenticity of host '192.168.194.130 (192.168.194.130)' can't be established.
ECDSA key fingerprint is SHA256:56XmV3ETdeyOoI3O4uQmBzBston1io6oJGzG3tzxR3I.
ECDSA key fingerprint is MD5:70:fe:fe:67:05:ab:b9:25:88:67:98:5f:b5:c3:04:36.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.194.130' (ECDSA) to the list of known hosts.
root@192.168.194.130's password:
sending incremental file list
sent 45 bytes received 12 bytes 1.75 bytes/sec
total size is 1,279 speedup is 22.44
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
扩展资料:
rsync命令:http://man.linuxde.net/rsync
10.29/10.30 rsync 常用选项
- rsync常用选项:
| 选项 | 含义 |
|–|--|
| -a | 包含 -rtplgoD |
| -r | 同步目录时要加上,类似 cp 时的 -r 选项 |
| -v | 同步时显示一些信息,让我们知道同步的过程 |
| -l | 保留软连接 |
| -L | 加上该选项后,同步软链接时会把源文件给同步(防止软链接失效) |
| -p | 保持文件的权限属性 |
| -o | 保持文件的属主 |
| -g | 保持文件的属组 |
| -D | 保持设备文件信息 |
| -t | 保持文件的时间属性 |
| –delete | 删除DEST中SRC没有的文件(使DEST和SRC目录完全一样,为了安全可以不加) |
| –exclude | 过滤指定文件,如–exclude “logs”会把文件名包含logs的文件或目录过滤,不同步 |
| -P | 显示同步过程,比如速率,比-v更加详细 |
| -u | 加上该选项后,如果DEST中的文件比SRC新,则不同步 |
| -z | 传输时压缩(目的:快,节省带宽,传到DEST后自动解压) |
实际操作:
rsync -av(rtplogDv)
[root@arslinux-01 ~]# rsync -av /root/111/ /tmp/111_dest/
sending incremental file list
created directory /tmp/111_dest
./
11.txt
12.txt
1912
gakki.jpg.bz2
yum.log -> /tmp/yum.log
222/
sent 54,232 bytes received 138 bytes 108,740.00 bytes/sec
total size is 53,858 speedup is 0.99
- rsync -avL 同步软链接时会把源文件给同步
[root@arslinux-01 111_dest]# rsync -avL /root/111/ /tmp/111_dest/
sending incremental file list
yum.log
sent 230 bytes received 36 bytes 532.00 bytes/sec
total size is 53,846 speedup is 202.43
[root@arslinux-01 ~]# ls /tmp/111_dest/
11.txt 12.txt 1912 222 gakki.jpg.bz2 yum.log
最后的 yum.log 从软链接变成了正常文件
给源文件增加一些内容,那么rsync后会把内容同步
[root@arslinux-01 ~]# echo "12343teixeira">/tmp/yum.log
[root@arslinux-01 ~]# rsync -avL /root/111/ /tmp/111_dest/
sending incremental file list
yum.log
sent 249 bytes received 36 bytes 570.00 bytes/sec
total size is 53,860 speedup is 188.98
[root@arslinux-01 ~]# cat /tmp/111_dest/yum.log
12343teixeira
- rsync --delete 删除 DEST 中 SRC 没有的文件
在DEST下创建新文件 new.txt,rsync --delete 后查看效果
[root@arslinux-01 ~]# touch /tmp/111_dest/new.txt
[root@arslinux-01 ~]# ls /tmp/111_dest/
11.txt 12.txt 1912 222 gakki.jpg.bz2 new.txt yum.log
[root@arslinux-01 ~]# ls /root/111/
11.txt 12.txt 1912 222 gakki.jpg.bz2 yum.log
[root@arslinux-01 ~]# rsync -av --delete /root/111/ /tmp/111_dest/
sending incremental file list
deleting new.txt
./
yum.log -> /tmp/yum.log
sent 215 bytes received 34 bytes 498.00 bytes/sec
total size is 53,858 speedup is 216.30
[root@arslinux-01 ~]# ls /tmp/111_dest/
11.txt 12.txt 1912 222 gakki.jpg.bz2 yum.log
- rsync --exclude 过滤掉 SRC 中的文件
[root@arslinux-01 ~]# ls /root/111/
11.txt 12.txt 1912 222 gakki.jpg.bz2 yum.log
[root@arslinux-01 ~]# rm -rf /tmp/111_dest/*
[root@arslinux-01 ~]# ls !$
ls /tmp/111_dest/*
ls: 无法访问/tmp/111_dest/*: 没有那个文件或目录
[root@arslinux-01 ~]# ls /tmp/
111_dest ipt.txt vmware-root_6193-1950294914
1.cap systemd-private-e6db7f1f46634f0298e12de6f5b50b2d-chronyd.service-XdBGzl vmware-root_6201-1983718796
1.sh vmware-root_5779-1681267576 vmware-root_6233-1714755028
1.txt vmware-root_6075-1723604046 vmware-root_6248-734169122
2.txt vmware-root_6105-1983196564 vmware-root_6378-994294112
3.txt vmware-root_6130-994685438 yum.log
a.txt vmware-root_6148-961265649
[root@arslinux-01 ~]# rsync -avL --exclude "*.txt" /root/111/ /tmp/111_dest/
sending incremental file list
./
1912
gakki.jpg.bz2
yum.log
222/
sent 54,076 bytes received 80 bytes 108,312.00 bytes/sec
total size is 53,795 speedup is 0.99
[root@arslinux-01 ~]# ls /tmp/111_dest/
1912 222 gakki.jpg.bz2 yum.log
-
支持多个 --exclude来 过滤多种文件:rsync --exclude --exclude
-
rsync -P 显示同步过程(比如:速率,比 -v 更详细)
[root@arslinux-01 ~]# rsync -avP /root/111/ /tmp/111_dest/
sending incremental file list
./
11.txt
65 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=5/7)
12.txt
0 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=4/7)
1912
0 100% 0.00kB/s 0:00:00 (xfr#3, to-chk=3/7)