Linux日常运维(四)

10.8 数据备份工具rsync

在linux系统下数据备份的工具很多,但较为实用的是rsync。rsync不仅可以远程同步数据,还可以本地同步数据,它不会覆盖之前的数据(如果数据已经存在),而是先判断已经存在的数据和新数据的差异,只有数据不同时才会把不相同的部分覆盖,类似于增量备份。

系统默认没有rsync,需要安装:

[root@localhost~]#yum install -y rsync

如果两端需要同步数据,必须在两端都装上rsync。

rsync命令格式

rsync命令格式有5种:

  • rsync [OPTION] … SRC DEST
    [OPTION]表示它的选项
    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

第1种是本地同步
第2种是远程同步–推数据
第3种是远程同步–拉数据
第4、5种是远程同步,均使用了双冒号,是 –daemon 模式

rsync常用选项

rsync常用选项如下:

-a   包含 -rtplgoD 等选项

-r   同步目录时要加上 -r 选项

-v  同步时显示一些信息,让我们知道同步的过程

-l   保留软链接(能同步软链接文件本身,但软链接指向的文件并没有被同步)

-L  加上该选项后,同步软链接时会把源文件给同步

-p  保持文件的权限属性

-o  保持文件的属主

-g  保持文件的属组

-D  保持设备文件信息

-t  保持文件的时间属性(-ctime、-mtime、-atime)

--delete   删除 目标目录/文件 中 源目录/文件 没有的文件(两端保持完全一样:-a --delete)

--exclude  过滤指定文件,可以多次使用

-P  显示同步过程,比如速率,比 -v更加详细

-u  加上该选项后,如果目标目录/文件 比 源目录/文件  更新,则不同步(不会将新的东西给覆盖)

-z  传输时压缩,在同步文件很多很大的时候可以节省带宽,提升速度

使用举例:
- 使用 -a 选项

[root@localhost/tmp/rsync]#tree .
.
|-- 1
|   |-- 123.txt -> /root/123.txt
|   |-- 1.txt
|   `-- 2.txt
`-- 2
    |-- 3
    `-- 4

2 directories, 5 files

[root@localhost/tmp/rsync]#rsync -a 1/ 2/
[root@localhost/tmp/rsync]#tree .
.
|-- 1
|   |-- 123.txt -> /root/123.txt
|   |-- 1.txt
|   `-- 2.txt
`-- 2
    |-- 123.txt -> /root/123.txt
    |-- 1.txt
    |-- 2.txt
    |-- 3
    `-- 4

2 directories, 8 files

在使用rsync备份目录时,要在目录名后面加上 /
-a 选项等同于 -rlptgoD ,-a还可以与 –no-选项 一并使用,表示去掉该选项

[root@localhost/tmp/rsync]#tree .
.
`-- 1
    |-- 123.txt -> /root/123.txt
    |-- 1.txt
    `-- 2.txt

1 directory, 3 files
[root@localhost/tmp/rsync]#rsync -a --no-l 1/ 2/
skipping non-regular file "123.txt"
[root@localhost/tmp/rsync]#tree .
.
|-- 1
|   |-- 123.txt -> /root/123.txt
|   |-- 1.txt
|   `-- 2.txt
`-- 2
    |-- 1.txt
    `-- 2.txt

2 directories, 5 files

可以看到,上面将软链接文件去掉了,并没有同步

  • 使用 -L 选项
[root@localhost/tmp/rsync]#tree .
.
`-- 1
    |-- 123.txt -> /root/123.txt
    |-- 1.txt
    `-- 2.txt

1 directory, 3 files
[root@localhost/tmp/rsync]#rsync -avL 1/ 2/
sending incremental file list
created directory 2
./
1.txt
123.txt
2.txt

sent 225 bytes  received 100 bytes  650.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost/tmp/rsync]#tree .
.
|-- 1
|   |-- 123.txt -> /root/123.txt
|   |-- 1.txt
|   `-- 2.txt
`-- 2
    |-- 123.txt
    |-- 1.txt
    `-- 2.txt

2 directories, 6 files

-L 选项可以把目标目录中软链接文件的指向文件复制到目标目录中,但没有复制软链接文件本身
- 使用 -u 选项

[root@localhost/tmp/rsync]#ll 1/1.txt 2/1.txt
-rw-r--r-- 1 root root 0 6月  16 13:44 1/1.txt
-rw-r--r-- 1 root root 0 6月  16 13:44 2/1.txt

可以看到,上面同步没有加 -u 选项,所以 1/ 和 2/ 的时间都被同步

[root@localhost/tmp/rsync]#echo "111" > 2/1.txt
[root@localhost/tmp/rsync]#!ll
ll 1/1.txt 2/1.txt
-rw-r--r-- 1 root root 0 6月  16 13:44 1/1.txt
-rw-r--r-- 1 root root 4 6月  16 13:46 2/1.txt
[root@localhost/tmp/rsync]#rsync -avu 1/ 2/
sending incremental file list

sent 120 bytes  received 12 bytes  264.00 bytes/sec
total size is 13  speedup is 0.10
[root@localhost/tmp/rsync]#!ll
ll 1/1.txt 2/1.txt
-rw-r--r-- 1 root root 0 6月  16 13:44 1/1.txt
-rw-r--r-- 1 root root 4 6月  16 13:46 2/1.txt

加上 -u 选项后,目标文件的新的内容和时间不会被源文件同步
- 使用 –delete 选项

[root@localhost/tmp/rsync]#tree .
.
|-- 1
|   |-- 1.txt
|   `-- 2.txt
`-- 2
    |-- 123.txt -> /root/123.txt
    |-- 1.txt
    `-- 2.txt

2 directories, 5 files
[root@localhost/tmp/rsync]#rsync -av --delete 1/ 2/
sending incremental file list
deleting 123.txt
./
1.txt

sent 126 bytes  received 49 bytes  350.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost/tmp/rsync]#tree .
.
|-- 1
|   |-- 1.txt
|   `-- 2.txt
`-- 2
    |-- 1.txt
    `-- 2.txt

2 directories, 4 files

加上 –delete 选项后,同步时会删除目标目录中源目录所没有的文件
- 使用 –exclude 选项

[root@localhost/tmp/rsync]#tree .
.
|-- 1
|   |-- 1.txt
|   |-- 2.txt
|   |-- 3.txt
|   `-- 4.txt
`-- 2
    |-- 1.txt
    `-- 2.txt

2 directories, 6 files
[root@localhost/tmp/rsync]#rsync -a --exclude="3.txt" 1/ 2/
[root@localhost/tmp/rsync]#tree .
.
|-- 1
|   |-- 1.txt
|   |-- 2.txt
|   |-- 3.txt
|   `-- 4.txt
`-- 2
    |-- 1.txt
    |-- 2.txt
    `-- 4.txt

2 directories, 7 files

加上 –exclude 选项后,同步时会过滤指定的内容,不会同步
– exclude 选项还可以与匹配字符*一起使用

[root@localhost/tmp/rsync]#tree .
.
|-- 1
|   |-- 1.sh
|   |-- 1.txt
|   |-- 2.sh
|   |-- 2.txt
|   |-- 3.txt
|   `-- 4.txt
`-- 2
    |-- 1.txt
    |-- 2.txt
    `-- 4.txt

2 directories, 9 files
[root@localhost/tmp/rsync]#rsync -a --exclude="*.sh" --exclude="3*" 1/ 2/
[root@localhost/tmp/rsync]#tree .
.
|-- 1
|   |-- 1.sh
|   |-- 1.txt
|   |-- 2.sh
|   |-- 2.txt
|   |-- 3.txt
|   `-- 4.txt
`-- 2
    |-- 1.txt
    |-- 2.txt
    `-- 4.txt

2 directories, 9 files

–exclude 选项可以使用多次,但不能将过滤的内容写到一起
- 使用 -P(–progress)选项

[root@localhost/tmp/rsync]#rsync -a --progress 1/ 2/
sending incremental file list
1.sh
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=5/7)
2.sh
              0 100%    0.00kB/s    0:00:00 (xfr#2, to-chk=3/7)
3.txt
              0 100%    0.00kB/s    0:00:00 (xfr#3, to-chk=1/7)
[root@localhost/tmp/rsync]#tree .
.
|-- 1
|   |-- 1.sh
|   |-- 1.txt
|   |-- 2.sh
|   |-- 2.txt
|   |-- 3.txt
|   `-- 4.txt
`-- 2
    |-- 1.sh
    |-- 1.txt
    |-- 2.sh
    |-- 2.txt
    |-- 3.txt
    `-- 4.txt

2 directories, 12 files

-P 选项和 –progress 选项作用相同,都是查看同步过程的进度

rsync应用

上面讲的都是本地同步,接下来我们谈谈远程同步,在进行远程同步之前,我们需要把firewalld服务关闭,设置为不开机启动,两端都要执行这个步骤。

  • 通过ssh的方式
    在之前介绍的5种命令格式中,第2、3种就是通过ssh方式同步数据的,这种方式就是让用户登录到远程机器,然后执行rsync的任务。
[root@localhost/tmp/rsync]#systemctl stop firewalld;systemctl disable firewalld
[root@localhost/tmp/rsync]#rsync -avL 1/ 192.168.20.130:/tmp/rsync/1/
The authenticity of host '192.168.20.130 (192.168.20.130)' can't be established.
ECDSA key fingerprint is 2c:71:18:54:33:fb:39:a4:17:96:07:3a:c1:bc:27:8a.
Are you sure you want to continue connecting (yes/no)? ^Crsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(638) [sender=3.1.2]
[root@localhost/tmp/rsync]# vim /etc/ssh/ssh_config
[root@localhost/tmp/rsync]#rsync -avL 1/ 192.168.20.130:/tmp/rsync/1/
Warning: Permanently added '192.168.20.130' (ECDSA) to the list of known hosts.
root@192.168.20.130's password: 
sending incremental file list
rsync: change_dir "/root//1" failed: No such file or directory (2)
created directory /tmp/2

sent 20 bytes  received 41 bytes  7.18 bytes/sec
total size is 0  speedup is 0.00
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1178) [sender=3.1.2]

第一次ssh遇到问题,这里提示

[root@localhost/tmp/rsync]#rsync -avL 1/ 192.168.20.130:/tmp/rsync/1/
The authenticity of host '192.168.20.130 (192.168.20.130)' can't be established.
ECDSA key fingerprint is 2c:71:18:54:33:fb:39:a4:17:96:07:3a:c1:bc:27:8a.
Are you sure you want to continue connecting (yes/no)? 

需要给两端都编辑ssh的配置文件 /etc/ssh/ssh_config ,在最后面加上

StrictHostKeyChecking no  
UserKnownHostsFile /dev/null  

然后在运行最先的命令

[root@localhost/tmp/rsync]#rsync -avL 1/ 192.168.20.130:/tmp/rsync/1/
Warning: Permanently added '192.168.20.130' (ECDSA) to the list of known hosts.
root@192.168.20.130's password: 
sending incremental file list
./
1.txt
123.txt
2.txt

sent 225 bytes  received 76 bytes  40.13 bytes/sec
total size is 0  speedup is 0.00

可以看到,ssh同步文件到目标主机上面,不过需要输入192.168.20.130那台主机root账户的密码,显然较为不便且不太安全

  • 通过后台服务的方式
    在远程主机上建立一个rsync的服务器,在服务器上配置好rsync的各种应用,然后将本机作为rsync的一个客户端连接远程的rsync服务器
    • 首先需要在128主机上建立并配置rsync的配置文件 /etc/rsyncd.conf , 一个配置文件可分为全局配置部分和模块配置部分,一个配置文件中可以有多个模块,模块名可以自定义。
[root@localhost/tmp/rsync]#vim /etc/rsyncd.conf
port=873   //监听端口默认为873,也可以是别的端口
log file=/var/log/rsync.log   //指定日志
pid file=/var/run/rsyncd.log   //指定pid
address=192.168.20.128   //可以定义绑定的ip

[test]   //为模块名,自定义
path=/root/rsync   // 指定该模块对应在哪个目录下
use chroot=false   //是否限定在该目录下,默认为true,当有软连接时,需要改为fasle,如果为true就限定为模块默认目录
max connections=4   //指定最大可以连接的客户端数
read only=no   //如果为true,则不能上传到该模块指定的路径中
list=true   //是否可以列出模块名
uid=root   //以哪个用户的身份来传输
gid=root   //以哪个组的身份来传输
auth users=admin   //指定验证用户名,可以不设置,不设置默认不用密码,设置的话安全性更高点
secrets file=/etc/rsyncd.passwd   //指定密码文件,如果设定验证用户,这一项必须设置,设定密码权限为600
hosts allow=192.168.20.130   //设置可以允许访问的主机,可以是网段,多个Ip地址用空格隔开

上面各种参数的含义:

 port:指定在哪个端口启动rsyncd服务,默认是873端口。
 log file:指定日志文件。
 pid file:指定pid文件,这个文件的作用涉及服务的启动、停止等进程管理操作。
 address:指定启动rsyncd服务的IP。假如你的机器有多个IP,就可以指定由其中一个启动rsyncd服务,如果不指定该参数,默认是在全部IP上启动。
 []:指定模块名,里面内容自定义。
 path:指定数据存放的路径。
 use chroot true|false:表示在传输文件前首先chroot到path参数所指定的目录下。这样做的原因是实现额外的安全防护,但缺点是需要以roots权限,并且不能备份指向外部的符号连接所指向的目录文件。默认情况下chroot值为true,如果你的数据当中有软连接文件,建议你设置成falsemax connections:指定最大的连接数,默认是0,即没有限制。
 read only ture|false:如果为true,则不能上传到该模块指定的路径下。
 list:表示当用户查询该服务器上的可用模块时,该模块是否被列出,设定为true则列出,false则隐藏。
 uid/gid:指定传输文件时以哪个用户/组的身份传输。
 auth users:指定传输时要使用的用户名。
 secrets file:指定密码文件,该参数连同上面的参数如果不指定,则不使用密码验证。注意该密码文件的权限一定要是600。格式:用户名:密码
 hosts allow:表示被允许连接该模块的主机,可以是IP或者网段,如果是多个,中间用空格隔开。
    • 再编辑 secrets file 并保存后要赋予600权限,如果权限不对,无法完成同步
 [root@localhost/tmp/rsync]#vim /etc/rsyncd.passwd
 [root@localhost/tmp/rsync]#cat /etc/rsyncd.passwd
123456
[root@localhost/tmp/rsync]#chomd 600 /etc/rsyncd.passwd
    • 启动rsyncd服务,启动就查看日志,并查看端口是否启动
[root@localhost/var/log]#rsync --daemon --config=/etc/rsyncd.conf
2018/06/16 15:59:54 [6718] rsyncd version 3.1.2 starting, listening on port 873
2018/06/16 15:59:54 [6718] bind() failed: Address already in use (address-family 2)
2018/06/16 15:59:54 [6718] unable to bind any inbound sockets on port 873
2018/06/16 15:59:54 [6718] rsync error: error in socket IO (code 10) at socket.c(555) [Receiver=3.1.2]

上面,rsyncd服务已经启动,而且在监听873端口。如果想开机启动rsyncd服务,可以把 /usr/bin/rsync –daemon –config=/etc/rsyncd.conf 写入 /etc/rc.d/rc.local 文件
- - 传输文件

[root@hf-02 ~]# rsync -avP /tmp/rsync/1.txt 192.168.20.128::test/tmp/2.txt
Password: 
sending incremental file list
hanfeng.txt
          50 100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/1)

sent 125 bytes  received 27 bytes  304.00 bytes/sec
total size is 50  speedup is 0.33

可以看到,文件传输成功

有两种方式可以让我们在同步时不需要输入密码:
1. 指定密码文件
在同步时,使用 –passwd 选项指定密码文件,密码文件的权限必须设为600
2. 在rsync服务器端不知道用户
在 /etc/rsyncd.conf 配置文件中删除关于认证用户的配置项(auth user 和 secrets file 这两行)


10.9 Linux系统日志

/var/log/messages

/var/log/messages 是核心系统日志文件,包含了系统启动时的1引导消息,以及系统运行时的其他状态消息,I/O错误、网络错误和其他系统错误都会被记录到这个文件中。

通常情况下,/var/log/messages 是做故障诊断时首先要查看的文件

日志不可能无限增长,所以有对应的日志切割工具:logrotate工具,它的配置文件是 /etc/logrotate.conf

[root@localhost~]#cat /etc/logrotate.conf
# see "man logrotate" for details
# rotate log files weekly
weekly

# keep 4 weeks worth of backlogs
rotate 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

# 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.

/var/log/messages 是由 rsyslogd这个守护进程产生的,如果停止这个服务则系统不会产生 /var/log/messages ,所以该服务不能停止。rsyslogd 服务的配置文件为 /etc/rsyslog.conf 这个文件定义了日志的级别。

dmesg

dmesg命令可以显示系统的启动信息(主要是硬件)

[root@localhost~]#dmesg |head
[    0.000000] Initializing cgroup subsys cpuset
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Initializing cgroup subsys cpuacct
[    0.000000] Linux version 3.10.0-693.17.1.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) ) #1 SMP Thu Jan 25 20:13:58 UTC 2018
[    0.000000] Command line: BOOT_IMAGE=/vmlinuz-3.10.0-693.17.1.el7.x86_64 root=UUID=7e6406a4-522a-424e-93ef-7f2688953f1e ro crashkernel=auto rhgb quiet LANG=zh_CN.utf8
[    0.000000] Disabled fast string operations
[    0.000000] e820: BIOS-provided physical RAM map:
[    0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009ebff] usable
[    0.000000] BIOS-e820: [mem 0x000000000009ec00-0x000000000009ffff] reserved
[    0.000000] BIOS-e820: [mem 0x00000000000dc000-0x00000000000fffff] reserved

这个日志文件保存在内存中,即使使用 dmesg -c 清空日志信息,重启机器后又可以显示出来

last、lastb

  • last命令用来查看正确登录linux的历史消息
[root@localhost~]#last |head
admin    :0           :0               Sat Jun 16 16:14   still logged in   
root     pts/0        192.168.20.1     Sat Jun 16 08:57   still logged in   
reboot   system boot  3.10.0-693.17.1. Sat Jun 16 08:47 - 17:28  (08:40)    
root     pts/0        192.168.20.1     Thu Jun 14 13:50 - crash (1+18:57)   
reboot   system boot  3.10.0-693.17.1. Thu Jun 14 13:44 - 17:28 (2+03:44)   
root     pts/1        192.168.20.1     Wed Jun 13 16:01 - crash  (21:42)    
root     pts/0        192.168.20.1     Wed Jun 13 15:30 - crash  (22:13)    
reboot   system boot  3.10.0-693.17.1. Wed Jun 13 15:20 - 17:28 (3+02:08)   
root     pts/0        192.168.20.1     Wed Jun 13 12:27 - down   (02:46)    
root     tty2                          Wed Jun 13 12:27 - 15:14  (02:46)  

从左至右依次为:
账户名称、登录终端、登录客户端、登录客户端IP、登录日期及时长
last的日志文件是 /var/log/wtmp

  • lastb命令用来查看登录失败的用户历史记录
[root@localhost~]#lastb
root     ssh:notty    192.168.83.1     Thu Jun  7 08:43 - 08:43  (00:00)    
root     tty2                          Thu Feb 22 10:02 - 10:02  (00:00)    

btmp begins Thu Feb 22 10:02:01 2018

lastb的日志文件是 /var/log/btmp

  • 另外,系统的安全日志是 /var/log/secure ,保存系统被暴力破解的记录

在后面工作和学习中,最好养成多看日志的习惯。

10.10 xargs 和 exec

xargs

xargs 可以把原来两步或者多步才能完成的任务仅用一步完成

[root@localhost~]#echo "11111" > ~/123.txt
[root@localhost~]#ls 123.txt |xargs cat
11111
[root@localhost~]#find . -mtime +10 |xargs rm

xargs后面的rm也可以加选项,例如-r

[root@localhost~]#mkdir test
[root@localhost~]#cd test
[root@localhost~/test]#touch 1.txt 2.txt 3.txt 4.txt
[root@localhost~/test]#ls
1.txt  2.txt  3.txt  4.txt
[root@localhost~/test]#ls *.txt |xargs -n1 -i{} mv {} {}_bak
[root@localhost~/test]#ls
1.txt_bak  2.txt_bak  3.txt_bak  4.txt_bak

xargs -n1 -i{ }类似于for循环,-n1表示逐个对象进行处理,-i{ }表示用{ }取代前面的对象,mv { } { }_bak相当于mv 1.txt 1.txt_bak

exec

使用find命令时,使用-exec选项,可以达到和xargs相同的效果

find . -mtime +10 -exec rm -rf {} \;

用{ }替代前面find出来的文件,后面的\作为;的转义符,否则zeshell会把分号作为该行命令的结尾

[root@localhost~/test]#ls
1.txt_bak  2.txt_bak  3.txt_bak  4.txt_bak
[root@localhost~/test]#find ./*_bak -exec mv {} {}_bak \;
[root@localhost~/test]#ls
1.txt_bak_bak  2.txt_bak_bak  3.txt_bak_bak  4.txt_bak_bak

-exec 同样可以实现批量更改文件名的需求

10.11 screen

nohup

nohup可以防止进程意外中断

[root@localhost~]#nohup sh /usr/local/sbin/sleep.sh &
[2] 10032
[root@localhost~]#nohup: 忽略输入并把输出追加到"nohup.out"

nohup加在命令前面,执行后会在当前目录下生成一个nohuo的文件,并把输出信息记录到nohup文件中

screen

screen表示虚拟终端,是一个可以在多个进程之间多路复用一个物理终端的窗口管理器,用户可以在一个screen会话中创建多个screen窗口,在每一个screen窗口中就像操作一个真实的SHH连接窗口一样。

系统默认没有screen命令,需要安装:

[root@localhost~]#yum install -y screen

screen 直接回车进入虚拟终端
screen -ls 查看虚拟终端列表

[root@localhost~]#screen

[root@localhost ~]# screen -ls
There is a screen on:
        10116.pts-0.localhost   (Attached)
1 Socket in /var/run/screen/S-root.

Ctrl+a 组合键再按d退出虚拟终端,只是临时退出(真正结束时Ctrl+D或exit)

screen -r id/终端名 进入指定的终端(id即终端编号)

[root@localhost ~]# screen -ls
There are screens on:
        10197.zx        (Attached)
        10194.pts-0.localhost   (Attached)
        10116.pts-0.localhost   (Detached)
3 Sockets in /var/run/screen/S-root.
[root@localhost ~]# screen -r 10194
There is a screen on:
        10194.pts-0.localhost   (Attached)
There is no screen to be resumed matching 10194.

screen -S 终端名 定义终端名字

[root@localhost~]#screen -S zx

[root@localhost ~]# screen -ls
There are screens on:
        10180.zx        (Attached)
        10116.pts-0.localhost   (Detached)
2 Sockets in /var/run/screen/S-root.

更多资料参考:
Linux日志文件总管
xargs用法详解

猜你喜欢

转载自blog.csdn.net/miss1181248983/article/details/80715498