Linux日常运维(四)

10.8 数据备份工具rsync

在linux系统下数据备份的工具很多,但较为实用的是rsync

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

  • 安装rsync:
# yum install -y rsync

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


rsync命令格式

  • rsync命令格式有5种:
1. 本地同步:

rsync [OPTION] … SRC DEST
#[OPTION]表示它的选项;SRC表示源目录;DEST表示目标目录,或者是目标文件

2. 远程同步--推数据:

rsync [OPTION] … SRC [user@]host:DEST  
#拷贝到远程的服务器上去(推数据),user@可省略,那就会默认当前终端的用户

3. 远程同步--拉数据:

rsync [OPTION][user@]host:SRC DEST 
#先写远程的机器/目录,然后拷贝到本地的目录下(拉数据)

4. 远程同步--daemon模式:

rsync [OPTION] … SRC [user@]host::DEST
#这里的两个冒号,可以是目标,可以是源

5. 远程同步--daemon模式:

rsync [OPTION][user@]host::SRC DEST

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选项:
# tree .
.
|-- 1
|   |-- 123.txt -> /root/123.txt
|   |-- 1.txt
|   `-- 2.txt
`-- 2
    |-- 3
    `-- 4

2 directories, 5 files
# rsync -a 1/ 2/
# 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选项一起使用,表示去掉该选项。

# tree .
.
`-- 1
    |-- 123.txt -> /root/123.txt
    |-- 1.txt
    `-- 2.txt

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

2 directories, 5 files

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

  • 使用-L选项:
# tree .
.
`-- 1
    |-- 123.txt -> /root/123.txt
    |-- 1.txt
    `-- 2.txt

1 directory, 3 files
# 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
# tree .
.
|-- 1
|   |-- 123.txt -> /root/123.txt
|   |-- 1.txt
|   `-- 2.txt
`-- 2
    |-- 123.txt
    |-- 1.txt
    `-- 2.txt

2 directories, 6 files

-L选项会把目标目录中软链接文件的指向文件复制到目标目录中,但不会复制软链接文件本身。

  • 使用-u选项:
# 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/的时间都被同步。

# echo "111" > 2/1.txt

# !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
# 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
# !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选项:
# tree .
.
|-- 1
|   |-- 1.txt
|   `-- 2.txt
`-- 2
    |-- 123.txt -> /root/123.txt
    |-- 1.txt
    `-- 2.txt

2 directories, 5 files
# 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
# tree .
.
|-- 1
|   |-- 1.txt
|   `-- 2.txt
`-- 2
    |-- 1.txt
    `-- 2.txt

2 directories, 4 files

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

  • 使用--exclude选项:
# tree .
.
|-- 1
|   |-- 1.txt
|   |-- 2.txt
|   |-- 3.txt
|   `-- 4.txt
`-- 2
    |-- 1.txt
    `-- 2.txt

2 directories, 6 files
# rsync -a --exclude="3.txt" 1/ 2/
# tree .
.
|-- 1
|   |-- 1.txt
|   |-- 2.txt
|   |-- 3.txt
|   `-- 4.txt
`-- 2
    |-- 1.txt
    |-- 2.txt
    `-- 4.txt

2 directories, 7 files

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

# 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
# rsync -a --exclude="*.sh" --exclude="3*" 1/ 2/
# 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)选项:
# 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)
# 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服务关闭,设置为不开机启动,两端都要执行这个步骤。

# systemctl stop firewalld;systemctl disable firewalld
  • 通过ssh的方式:

在之前介绍的5种命令格式中,第2、3种就是通过ssh方式同步数据的,这种方式就是让用户登录到远程机器,然后执行rsync的任务。

# rsync -avL 1/ 192.168.20.130:/tmp/rsync/1/

# vim /etc/ssh/ssh_config

# rsync -avL 1/ 192.168.20.130:/tmp/rsync/1/

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

# 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  

然后再同步

# 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服务器。

首先需要在192.168.20.128上建立并配置rsync的配置文件/etc/rsyncd.conf,一个配置文件可分为全局配置部分和模块配置部分,一个配置文件中可以有多个模块,模块名可以自定义。

# 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,如果你的数据当中有软连接文件,建议你设置成false
max connections                     指定最大的连接数,默认是0,即没有限制
read only ture|false                如果为true,则不能上传到该模块指定的路径下

list                表示当用户查询该服务器上的可用模块时,该模块是否被列出,设定为true则列出,false则隐藏
uid/gid             指定传输文件时以哪个用户/组的身份传输
auth users          指定传输时要使用的用户名
secrets file        指定密码文件,该参数连同上面的参数如果不指定,则不使用密码验证。注意该密码文件的权限一定要是600。格式:用户名:密码
hosts allow         表示被允许连接该模块的主机,可以是IP或者网段,如果是多个,中间用空格隔开

编辑secrets file并保存后要赋予600权限,如果权限不对,无法完成同步。

# vim /etc/rsyncd.passwd

# cat /etc/rsyncd.passwd
123456

# chomd 600 /etc/rsyncd.passwd

启动rsyncd服务,启动就查看日志,并查看端口是否启动。

# 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文件中。

现在开始传输文件:

# 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 usersecrets file这两行)

10.9 Linux系统日志


/var/log/messages

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

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

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

# 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命令可以显示系统的启动信息(主要是硬件)。

# 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的历史消息。

# 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命令用来查看登录失败的用户历史记录。

# 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可以把原来两步或者多步才能完成的任务仅用一步完成。

# echo "11111" > ~/123.txt

# ls 123.txt | xargs cat
11111
# find . -mtime +10 | xargs rm

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

# mkdir test

# cd test

# touch 1.txt 2.txt 3.txt 4.txt

# ls
1.txt  2.txt  3.txt  4.txt

# ls *.txt | xargs -n1 -i{} mv {} {}_bak

# 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出来的文件,后面的\作为;的转义符,否则shell会把分号作为该行命令的结尾。

# ls
1.txt_bak  2.txt_bak  3.txt_bak  4.txt_bak

# find ./*_bak -exec mv {} {}_bak \;

# ls
1.txt_bak_bak  2.txt_bak_bak  3.txt_bak_bak  4.txt_bak_bak

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


10.11 screen


nohup

nohup可以防止进程意外中断。

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

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


screen

screen表示虚拟终端,是一个可以在多个进程之间多路复用一个物理终端的窗口管理器。

用户可以在一个screen会话中创建多个screen窗口,在每一个screen窗口中就像操作一个真实的SHH连接窗口一样。

  • 安装screen:
# yum install -y screen

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

# screen

# 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即终端编号)

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

# screen -r 10194
There is a screen on:
        10194.pts-0.localhost   (Attached)
There is no screen to be resumed matching 10194.

screen -S 终端名用于定义终端名字。

# screen -S zx

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

更多资料参考:

Linux日志文件总管

xargs用法详解

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值