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

【0420】日常运维——rsync、系统日志、screen_screen

最后的 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)
gakki.jpg.bz2
53,781 100%   51.29MB/s    0:00:00 (xfr#4, to-chk=2/7)
yum.log -> /tmp/yum.log
222/

sent 54,232 bytes  received 102 bytes  108,668.00 bytes/sec
total size is 53,858  speedup is 0.99


rsync -u 让DEST中的文件比SRC更新(新的不会被删除)

[root@arslinux-01 ~]# vim /tmp/111_dest/1912
;alkjsd;fkaj
teixeira
[root@arslinux-01 ~]# rsync -avPu /root/111/ /tmp/111_dest/
sending incremental file list
./

sent 212 bytes  received 20 bytes  464.00 bytes/sec
total size is 53,858  speedup is 232.15
[root@arslinux-01 ~]# cat /tmp/111_dest/1912
;alkjsd;fkaj
teixeira


rsync -z 传输时压缩

[root@arslinux-01 ~]# rsync -avPz /root/111/ /tmp/111_dest/
sending incremental file list
1912
0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=3/7)
sent 245 bytes  received 36 bytes  562.00 bytes/sec
total size is 53,858  speedup is 191.67

没有什么明显效果,如果文件多时,可以节省带宽


10.31 rsync通过ssh同步

rsync -av SRC IP:DEST 通过ssh方式同步

[root@arslinux-01 ~]# ping 192.168.194.132
PING 192.168.194.132 (192.168.194.132) 56(84) bytes of data.
64 bytes from 192.168.194.132: icmp_seq=1 ttl=64 time=0.653 ms
64 bytes from 192.168.194.132: icmp_seq=2 ttl=64 time=0.261 ms
^C
--- 192.168.194.132 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.261/0.457/0.653/0.196 ms
[root@arslinux-01 ~]# rsync /etc/passwd 192.168.194.132:/tmp/arslinux.txt
The authenticity of host '192.168.194.132 (192.168.194.132)' 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.132' (ECDSA) to the list of known hosts.
root@192.168.194.132's password:
bash: rsync: 未找到命令
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: remote command not found (code 127) at io.c(226) [sender=3.1.2]

因为B机器没有安装 rsync,安装完成后重试

[root@arslinux-01 ~]# rsync -av /etc/passwd 192.168.194.132:/tmp/arslinux.txt
root@192.168.194.132's password:
sending incremental file list
passwd

sent 1,371 bytes  received 35 bytes  90.71 bytes/sec
total size is 1,279  speedup is 0.91
[root@arslinux-02 ~]# cat /tmp/arslinux.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
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
arslinux:x:1000:1000::/home/arslinux:/bin/bash
user1:x:1001:1001::/home/user1:/bin/bash
user2:x:1002:1006::/home/user2:/bin/bash
user3:x:1004:1005::/home/user3:/bin/bash
user4:x:1006:1005::/home/arslinux:/sbin/nologin
user5:x:1007:1007::/home/user5:/bin/bash
user6:x:1008:1010::/home/user6:/bin/bash
tcpdump:x:72:72::/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin

rsync -av ip:DEST SRC 反相同步

[root@arslinux-01 ~]# rsync -avP 192.168.194.132:/tmp/arslinux.txt /tmp/12123.txt
root@192.168.194.132's password:
receiving incremental file list
arslinux.txt
1,279 100%    1.22MB/s    0:00:00 (xfr#1, to-chk=0/1)

sent 43 bytes  received 1,377 bytes  315.56 bytes/sec
total size is 1,279  speedup is 0.90
[root@arslinux-01 ~]# cat /tmp/12123.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
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
arslinux:x:1000:1000::/home/arslinux:/bin/bash
user1:x:1001:1001::/home/user1:/bin/bash
user2:x:1002:1006::/home/user2:/bin/bash
user3:x:1004:1005::/home/user3:/bin/bash
user4:x:1006:1005::/home/arslinux:/sbin/nologin
user5:x:1007:1007::/home/user5:/bin/bash
user6:x:1008:1010::/home/user6:/bin/bash
tcpdump:x:72:72::/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin

rsync -e "ssh -p 端口号" 指定连接的端口

[root@arslinux-01 ~]# rsync -avP -e "ssh -p 22" /etc/passwd 192.168.194.132:/tmp/arslinux.txt
root@192.168.194.132's password:
sending incremental file list

sent 45 bytes  received 12 bytes  16.29 bytes/sec
total size is 1,279  speedup is 22.44

-e 后面接命令,ssh -p 连接远程端口


10.32/10.33 rsync通过服务同步

编辑配置文件 /etc/rsyncd.conf

[root@arslinux-01 ~]# vim /etc/rsyncd.conf
port=873
log file=/var/log/rsync.log
pid file=/var/run/rsyncd.pid
address=192.168.194.130
[test]
path=/tmp/rsync
use chroot=true
max connections=4
read only=no
list=true
uid=root
gid=root
auth users=test
secrets file=/etc/rsyncd.passwd
hosts allow=192.168.194.132

rsync --daemon 启动服务

[root@arslinux-01 ~]# rsync --daemon
[root@arslinux-01 ~]# ps aux |grep rsync
root       7865  0.0  0.0 114744   568 ?        Ss   17:40   0:00 rsync --daemon
root       7867  0.0  0.0 112724   988 pts/0    S+   17:41   0:00 grep --color=auto rsync
[root@arslinux-01 ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 192.168.194.130:873     0.0.0.0:*               LISTEN      7865/rsync
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      7463/sshd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      7598/master
tcp6       0      0 :::22                   :::*                    LISTEN      7463/sshd
tcp6       0      0 ::1:25                  :::*                    LISTEN      7598/master

(--config=文件,如果rsync.conf不是保存在默认目录下的话,需要--config=文件来配置)


新建一个 777权限的 /tmp/rsync/ 目录

[root@arslinux-01 ~]# mkdir /tmp/rsync
[root@arslinux-01 ~]# chmod 777 /tmp/rsync/


格式:

rsync -avP SRC IP::module/dir/

[root@arslinux-02 ~]# rsync -avP /tmp/arslinux.txt 192.168.194.130::test/arslinux-02.txt
rsync: failed to connect to 192.168.194.130 (192.168.194.130): No route to host (113)
rsync error: error in socket IO (code 10) at clientserver.c(125) [sender=3.1.2]
[root@arslinux-02 ~]# ping 192.168.194.130
PING 192.168.194.130 (192.168.194.130) 56(84) bytes of data.
64 bytes from 192.168.194.130: icmp_seq=1 ttl=64 time=0.360 ms
64 bytes from 192.168.194.130: icmp_seq=2 ttl=64 time=0.262 ms
^C
--- 192.168.194.130 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 0.262/0.311/0.360/0.049 ms
[root@arslinux-02 ~]# telnet 192.168.194.130 873
Trying 192.168.194.130...
telnet: connect to address 192.168.194.130: No route to host
[root@arslinux-02 ~]# systemctl stop firewalld
[root@arslinux-01 ~]# systemctl stop firewalld        //关闭两台主机的firewalld
[root@arslinux-02 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target     prot opt in     out     source               destination

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target     prot opt in     out     source               destination

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target     prot opt in     out     source               destination
[root@arslinux-02 ~]# telnet 192.168.194.130 873
Trying 192.168.194.130...
Connected to 192.168.194.130.
Escape character is '^]'.
@RSYNCD: 31.0
^]
telnet> quit
Connection closed.
[root@arslinux-02 ~]# rsync -avP /tmp/arslinux.txt 192.168.194.130::test/arslinux-02.txt
sending incremental file list
arslinux.txt
1,279 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=0/1)

sent 1,377 bytes  received 35 bytes  2,824.00 bytes/sec
total size is 1,279  speedup is 0.91
[root@arslinux-01 ~]# ls /tmp/rsync/
arslinux-02.txt


拉文件:

[root@arslinux-02 ~]# rsync -avP 192.168.194.130::test/arslinux-02.txt /tmp/arslinux-03.txt
receiving incremental file list
arslinux-02.txt
1,279 100%    1.22MB/s    0:00:00 (xfr#1, to-chk=0/1)
sent 43 bytes  received 1,380 bytes  135.52 bytes/sec
total size is 1,279  speedup is 0.90
[root@arslinux-02 ~]# ls /tmp/
arslinux-03.txt                                                          vmware-root_6175-1983325619
arslinux.txt                                                             vmware-root_6377-1949639484
systemd-private-fcd061e82c0d4913b58336d608d0455d-chronyd.service-ma0udd


telnet ip地址 端口 检查端口是否通

[root@arslinux-02 ~]# telnet 192.168.194.130 873
Trying 192.168.194.130...
Connected to 192.168.194.130.
Escape character is '^]'.
@RSYNCD: 31.0
^]                            //ctrl + ],再 quit 退出
telnet> quit
Connection closed.


[root@arslinux-02 ~]# rsync -avP /tmp/arslinux.txt 192.168.194.130::test/arslinux-02.txt
sending incremental file list
arslinux.txt
1,279 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=0/1)

sent 1,377 bytes  received 35 bytes  2,824.00 bytes/sec
total size is 1,279  speedup is 0.91


/etc/rsync.conf 配置文件详解:

  • port:指定在哪个端口启动rsyncd服务,默认是873端口。

    如果端口不是873,而是8730,那么同步时需要 --port 8730

    例:rsync -avP --port 8730 192.168.65.128::test/ /tmp/test/

  • log file:指定日志文件。

  • pid file:指定pid文件,这个文件的作用涉及服务的启动、停止等进程管理操作。

  • address:指定启动rsyncd服务的IP。假如你的机器有多个IP,就可以指定由其中一个启动rsyncd服务,如果不指定该参数,默认是在全部IP上启动。

  • [ ]:指定模块(module)名,里面内容自定义。

  • path:指定数据存放的路径。

  • use chroot true|false:表示在传输文件前首先 chroot 到 path 参数所指定的目录下。这样做的原因是实现额外的安全防护,但缺点是需要以 roots 权限,并且不能备份指向外部的符号连接所指向的目录文件。默认情况下 chroot 值为 true,如果你的数据当中有软连接文件,建议设置成 false。

  • max connections:指定最大的连接数,默认是0,即没有限制。

  • read only ture|false:如果为true,则不能上传到该模块指定的路径下。

    (给服务端写服务时,服务端定义为true,则不可写)

  • list:表示当用户查询该服务器上的可用模块时,该模块是否被列出,设定为true则列出,false则隐藏。

  • uid/gid:指定传输文件时以哪个用户/组的身份传输。

  • auth users:指定传输时要使用的用户名。

  • secrets file:指定密码文件,该参数连同上面的参数如果不指定,则不使用密码验证。注意该密码文件的权限一定要是600。格式:用户名:密码

  • hosts allow:表示被允许连接该模块的主机,可以是IP或者网段,如果是多个,中间用空格隔开。


实际应用:

use chroot

·use charoot true

[root@arslinux-02 ~]# rsync -avP 192.168.194.130::test/ /tmp/test/
receiving incremental file list
created directory /tmp/test
./
12.txt -> /etc/passwd
arslinux-02.txt
1,279 100%    1.22MB/s    0:00:00 (xfr#1, to-chk=0/3)

sent 49 bytes  received 1,443 bytes  142.10 bytes/sec
total size is 1,290  speedup is 0.86
[root@arslinux-02 ~]# ls /tmp/test/
12.txt  arslinux-02.txt
[root@arslinux-02 ~]# ll /tmp/test/
总用量 4
lrwxrwxrwx. 1 root root   11 4月  20 19:29 12.txt -> /etc/passwd
-rw-r--r--. 1 root root 1279 4月  14 21:41 arslinux-02.txt
[root@arslinux-02 ~]# rm -rf /tmp/test/
[root@arslinux-02 ~]# rsync -avPL 192.168.194.130::test/ /tmp/test/
receiving incremental file list
symlink has no referent: "/12.txt" (in test)
created directory /tmp/test
./
arslinux-02.txt
1,279 100%    1.22MB/s    0:00:00 (xfr#1, to-chk=0/2)

sent 46 bytes  received 1,454 bytes  1,000.00 bytes/sec
total size is 1,279  speedup is 0.85
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1650) [generator=3.1.2]
[root@arslinux-02 ~]# ll /tmp/test/
总用量 4
-rw-r--r--. 1 root root 1279 4月  14 21:41 arslinux-02.txt

·use chroot false

在机器A上,/etc/rsyncd.conf 里,use chroot true 改为 false

再重新在B机器上同步

[root@arslinux-02 ~]# rsync -avPL 192.168.194.130::test/ /tmp/test/
receiving incremental file list
12.txt
1,279 100%    1.22MB/s    0:00:00 (xfr#1, to-chk=1/3)

sent 43 bytes  received 1,417 bytes  139.05 bytes/sec
total size is 2,558  speedup is 1.75
[root@arslinux-02 ~]# ll /tmp/test/
总用量 8
-rw-r--r--. 1 root root 1279 4月  14 21:41 12.txt
-rw-r--r--. 1 root root 1279 4月  14 21:41 arslinux-02.txt


默认端口不是873

1.修改端口号为 port=8730

2.重启 rsync 服务

[root@arslinux-01 rsync]# killall rsync
[root@arslinux-01 rsync]# ps aux |grep rsync
root       8141  0.0  0.0 112724   988 pts/0    R+   19:54   0:00 grep --color=auto rsync
[root@arslinux-01 rsync]# rsync --daemon
[root@arslinux-01 rsync]# ps aux |grep rsync
root       8143  0.0  0.0 114744   568 ?        Ss   19:54   0:00 rsync --daemon
root       8146  0.0  0.0 112724   984 pts/0    S+   19:54   0:00 grep --color=auto rsync
[root@arslinux-02 ~]# rsync -avP 192.168.194.130::test/ /tmp/test/
rsync: failed to connect to 192.168.194.130 (192.168.194.130): Connection refused (111)
rsync error: error in socket IO (code 10) at clientserver.c(125) [Receiver=3.1.2]
[root@arslinux-02 ~]# rsync -avP --port=8730 192.168.194.130::test/ /tmp/test/
receiving incremental file list
12.txt -> /etc/passwd
sent 27 bytes  received 118 bytes  290.00 bytes/sec
total size is 1,290  speedup is 8.90

list

list=true

[root@arslinux-02 ~]# rsync --port=8730 192.168.194.130::
test

list=false

[root@arslinux-02 ~]# rsync --port=8730 192.168.194.130::
[root@arslinux-02 ~]#


·auth users、sectets file

配置文件 /etc/rsyncd.conf

auth users=test
secrets file=/etc/rsyncd.passwd

当设置了auth users和secrets file后,客户端连服务端也需要用用户名密码了,若想在命令行中带上密码,可以设定一个密码文件

[root@arslinux-02 ~]# touch /tmp/test/1.txt
[root@arslinux-02 ~]# rsync -avP /tmp/test/ test@192.168.194.130::test/
Password:
sending incremental file list
./
1.txt
0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=2/4)

sent 181 bytes  received 38 bytes  48.67 bytes/sec
total size is 1,290  speedup is 5.89
[root@arslinux-02 ~]#

可以将密码写入到客户端里文件中,名称可自定义,再用--password-file来定义,权限600

[root@arslinux-02 ~]# rsync -avP /tmp/test/ --password-file=/etc/rsync_pass.txt test@192.168.194.130::test/

[root@arslinux-02 ~]# vim /etc/rsync_pass.txt
arslinux
[root@arslinux-02 ~]# chmod 600 /etc/rsync_pass.txt
[root@arslinux-02 ~]# rsync -avP /tmp/test/ --password-file=/etc/rsync_pass.txt test@192.168.194.130::test/
sending incremental file list

sent 135 bytes  received 12 bytes  14.00 bytes/sec
total size is 1,290  speedup is 8.78

·一个ip可以直接写,多个可以写192.168.65.129 1.1.1.1 2.2.2.2 或者IP段 192.168.65.0/24


10.34 linux系统日志

系统日志:/var/log/messages

如果命令没有定义单独的日志,那么日志就记录在这里

日志增长到一定级别,为防止文件过大,会自动切割(logrotate)

[root@arslinux-02 ~]# ls /var/log/messages*
/var/log/messages  /var/log/messages-20190317  /var/log/messages-20190420


logrotate

/etc/logrotate.conf 日志切割配置文件

# see "man logrotate" for details
# rotate log files weekly//每周轮换一次日志文件
weekly                    //每周切割一次

# keep 4 weeks worth of backlogs//保持4周的积压
rotate 4                //4周一次轮换

# create new (empty) log files after rotating old ones //在轮换就得日志文件后创建新的空的日志文件
create                //创建新的

# use date as a suffix of the rotated file//使用日期作为轮换文件的后缀
dateext                //以之为后缀名

# uncomment this if you want your log files compressed //如果你想压缩日志文件,请取消注释
#compress            //是否需要压缩,压缩成 .tar.gz

# RPM packages drop log rotation information into this directory
include /etc/logrotate.d

# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
monthly
create 0664 root utmp
minsize 1M
rotate 1
}

/var/log/btmp {            //切割该文件,指定权限,属主,属组
missingok
monthly
create 0600 root utmp
rotate 1
}

# system-specific logs may be also be configured here.        //系统特定的日志也可以在这配置
/etc/logrotate.d/syslog
[root@arslinux-02 ~]# cat /etc/logrotate.d/syslog
/var/log/cron
/var/log/maillog
/var/log/messages
/var/log/secure
/var/log/spooler
{

missingok
sharedscripts
postrotate
/bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
endscript
}

linux写文件是根据文件句柄写的,而不是根据文件名


dmesg 命令 列出系统硬件相关日志

dmesg -c 清空日志(重启后自动生成)


/var/log/dmesg 系统启动日志,记录的信息,和dmesg命令不一样


last 命令,调用的文件 /var/log/wtmp(二进制文件),查看正确的登录历史

lastb 命令,调用的文件/var/log/btmp,查看登陆失败的历史


/var/log/secure 安全日志

tail -f 不断刷新查看末行

参考:https://my.oschina.net/u/2000675/blog/908189


10.35 screen工具

screen 虚拟终端

执行脚本,时间很长,会输出一些信息,因此脚本不可中途终端

为了保证脚本不中断,网络不能出差错


把任务丢后台,加日志输出

nohup 命令 &


安装screen

[root@arslinux-01 ~]# yum install -y screen


screen 直接进入虚拟终端

ctrl + a 组合键再按 d 退出虚拟终端,但不是结束(screen 后,在虚拟终端中操作)

[root@arslinux-01 ~]# screen
[detached from 8506.pts-0.arslinux-01]

screen -ls      查看虚拟终端列表

[root@arslinux-01 ~]# screen -ls
There is a screen on:
    8506.pts-0.arslinux-01(Detached)
1 Socket in /var/run/screen/S-root.

screen -r ID号      进入指定的终端

退出screen,exit退出

screen 可以同时进入多个虚拟终端(指定id可以进入)

[root@arslinux-01 ~]# screen
[detached from 8506.pts-0.arslinux-01]
[root@arslinux-01 ~]# screen
[detached from 8524.pts-0.arslinux-01]
[root@arslinux-01 ~]# screen
[detached from 8561.pts-0.arslinux-01]
[root@arslinux-01 ~]# screen -ls
There are screens on:
    8561.pts-0.arslinux-01(Detached)
    8524.pts-0.arslinux-01(Detached)
    8506.pts-0.arslinux-01(Detached)
3 Sockets in /var/run/screen/S-root.

但是虚拟终端多了之后,时间长了就无法分辨每个虚拟终端里运行的是什么

因此可以自定义虚拟终端的名称


screen -S 名称 进入虚拟终端时自定义虚拟终端名称

[root@arslinux-01 ~]# screen -S test_screen
[detached from 8583.test_screen]
[root@arslinux-01 ~]# screen -ls
There are screens on:
    8583.test_screen(Detached)
    8561.pts-0.arslinux-01(Detached)
    8524.pts-0.arslinux-01(Detached)
    8506.pts-0.arslinux-01(Detached)
4 Sockets in /var/run/screen/S-root.


进入虚拟终端时,可以 screen -r 加ID号,也可以加自定义的名称

[root@arslinux-01 ~]# screen -r 8583
[detached from 8583.test_screen]
[root@arslinux-01 ~]# screen -r test_screen
[detached from 8583.test_screen]




课堂笔记


扩展:

1. Linux日志文件总管logrotate  http://linux.cn/article-4126-1.html

2. xargs用法详解http://blog.csdn.net/zhangfn2011/article/details/6776925


xargs: https://blog.csdn.net/gb4215287/article/details/78037520

xargs -a

xargs -E '关键词'        作为停止的flag