linux系统管理技巧-日常基础命令四

linux系统管理技巧-日常基础命令四

数据备份工具rsync

可以远程同步数据,还可以同步本地数据,它不会覆盖以前的数据,而是判断已经存在的数据和新数据的差异,只有数据不同时才会把不相同的部分覆盖。没有rsync先输入命令安装  yum install rsync -y

命令格式:

  • sync {option} SRC DEST
  • sync {option} SRC user@host DEST
  • sync {option} user@host:SRC DEST
  • sync {option} SRC user@host::DEST
  • sync {option} user@host:: SRC DEST
    第一个格式为本机备份格式
    第二个格式为远程数据备份
    第三个格式为远程目录同步数据到本地
    第四与第五个格式使用了2个冒号,这种格式与其他格式验证方式不同

命令演示

本机备份

[root@localhost ~]# rsync -av /tmp/1.txt  1.txt

sending incremental file list

1.txt

sent 95 bytes  received 35 bytes  260.00 bytes/sec

total size is 4  speedup is 0.03

[root@localhost ~]# 

备份 /tmp/1.txt 文档   到家目录的 1.txt



远程数据备份

[root@localhost ~]# rsync -av /tmp/1.txt 192.168.1.180:/root/2.txt

The authenticity of host '192.168.1.180 (192.168.1.180)' can't be established.

ECDSA key fingerprint is SHA256:ZWstwdqqh3EM1x19yZ/8jbQK9OVBfliuTJuEzSHEwYI.

ECDSA key fingerprint is MD5:6f:0f:df:4e:8c:30:97:b0:bd:8c:d9:54:c9:17:a5:b3.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added '192.168.1.180' (ECDSA) to the list of known hosts.

root@192.168.1.180's password: 

sending incremental file list

1.txt

sent 95 bytes  received 35 bytes  13.68 bytes/sec

total size is 4  speedup is 0.03

[root@localhost ~]# 

需要输入yes继续 及 密码  才可以备份

rsync常用选项:

-a:这是归档模式,表示以递归传输文件,并保存所有属性,等同于 -rlptgoD , -a选项后可以跟一个 --no-OPTION,表示关闭 -rlptgoD中的某一个。

-r:表示以递归模式处理子目录。它主要针对目录来说的,单独传输一个文件,不需要加 -r,传输目录时则必须加上

-v:表示打印一些信息,比如文件列表,文件数量等。

-l:表示保留软链接。

-L:表示像对待常规文件一样处理软链接。

-p:表示保持文件权限。

-o:表示保持文件属主信息。

-g:表示保持文件属组信息。

-D:表示保持设备文件信息。

-t:表示保持文件时间信息。

--delete:表示删除DST中SRC没有的文件。

--exclude=PATTERN:表示指定排除不需要传输的文件,等号后面跟随文件名。

--progress:表示在同步的过程中可以看到同步的状态。

-u:表示把DST中比SRC还新的文件排除掉,不会覆盖。

-z:加上该选项,则会在传输过程中压缩。

rsync命令演示:

[root@localhost ~]# rsync -av /tmp/1 /root/rsync/

sending incremental file list

1/

1/1.txt

1/1.txt_log -> 1.txt

sent 159 bytes  received 42 bytes  402.00 bytes/sec

total size is 9  speedup is 0.04

同步目录时,需要在目录后面加一个斜杠/

[root@localhost ~]# rsync -avL /tmp/1 /root/rsync/

sending incremental file list

1/1.txt_log

sent 140 bytes  received 36 bytes  352.00 bytes/sec

total size is 8  speedup is 0.05

[root@localhost rsync2]# rsync -avL /tmp/1 /root/rsync2/

sending incremental file list

1/

1/1.txt

1/1.txt_log

sent 190 bytes  received 58 bytes  496.00 bytes/sec

total size is 8  speedup is 0.03

[root@localhost rsync2]#

在加入参数 -L 后 ,会把参数-l的含义给覆盖掉,-L 会把软链接所指向的源文件给拷贝过去

rsync命令,删除目标中源文件中没有的内容

[root@localhost ~]# ls /tmp/1/

1.txt  1.txt_log

[root@localhost ~]# ls /root/rsync/1

1.txt  1.txt_log

[root@localhost ~]# touch /root/rsync/1/new.txt

[root@localhost ~]# rsync -avL --delete /tmp/1/ /root/rsync/

sending incremental file list

deleting 1/new.txt

deleting 1/1.txt_log

deleting 1/1.txt

deleting 1/

./

1.txt

1.txt_log

sent 180 bytes  received 102 bytes  564.00 bytes/sec

total size is 8  speedup is 0.03

[root@localhost ~]# ls /root/rsync/

1.txt  1.txt_log

[root@localhost ~]# 

–exclude:

过滤所有带.txt结尾的文件。

[root@localhost ~]# rsync -avL --exclude '*.txt' /tmp/1/ /root/rsync/

sending incremental file list

sent 67 bytes  received 12 bytes  158.00 bytes/sec

total size is 4  speedup is 0.05

[root@localhost ~]#

在添加文件后,再次同步,会只同步里面不相同的文件,而相同的文件则不会再次同步 

[root@localhost ~]# rsync -avL  /tmp/1/ /root/rsync/

sending incremental file list

./

2.txt

3.txt

sent 201 bytes  received 57 bytes  516.00 bytes/sec

total size is 8  speedup is 0.03

[root@localhost ~]#    

-P:

在传输过程中,会告诉你传输了多少,传输的速度是多少

[root@localhost ~]# rsync -avLP  /tmp/1/ /root/rsync/

sending incremental file list

./

1.txt

              4 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=3/5)

1.txt_log

              4 100%    3.91kB/s    0:00:00 (xfr#2, to-chk=2/5)

2.txt

              0 100%    0.00kB/s    0:00:00 (xfr#3, to-chk=1/5)

3.txt

              0 100%    0.00kB/s    0:00:00 (xfr#4, to-chk=0/5)

sent 295 bytes  received 95 bytes  780.00 bytes/sec

total size is 8  speedup is 0.02

[root@localhost ~]# 

-u:

[root@localhost ~]# cd rsync

[root@localhost rsync]# ls

1.txt  1.txt_log  2.txt  3.txt

[root@localhost rsync]# echo '123' >> 3.txt 

[root@localhost rsync]# cat 3.txt 

123

[root@localhost rsync]# rsync -avPu /tmp/1/ /root/rsync/

sending incremental file list

1.txt_log -> 1.txt

sent 140 bytes  received 19 bytes  318.00 bytes/sec

total size is 9  speedup is 0.06

[root@localhost rsync]# cat 3.txt 

123

[root@localhost rsync]# cat /tmp/1/3.txt 

[root@localhost rsync]# 

-z:

在远程传输很多文件的时候,加上-z 参数,可以节省带宽,增加速度的

[root@localhost rsync]# rsync -avPz /tmp/1/ /root/rsync2/

sending incremental file list

./

1.txt

              4 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=3/5)

1.txt_log -> 1.txt

2.txt

              0 100%    0.00kB/s    0:00:00 (xfr#2, to-chk=1/5)

3.txt

              0 100%    0.00kB/s    0:00:00 (xfr#3, to-chk=0/5)

sent 259 bytes  received 79 bytes  676.00 bytes/sec

total size is 9  speedup is 0.03

[root@localhost rsync]# 

rsync 通过服务的方式同步

  • 要编辑配置文件/etc/rsyncd.conf
  • 启动服务rsync –daemon
  • 格式:rsync -av test1/ 192.168.202.130::module/dir/

rsynd.conf配置文件:

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或者网段,如果是多个,中间用空格隔开。  当设置了auth users和secrets file后,客户端连服务端也需要用用户名密码了,若想在命令行中带上密码,可以设定一个密码文件 rsync -avL test@192.168.133.130::test/test1/  /tmp/test8/ --password-file=/etc/pass  其中/etc/pass内容就是一个密码,权限要改为600 

Linux系统日志

/var/log/messages 它是核心系统的日志文件,包含系统启动时的引导消息,以及系统运行时的其他状态消息等。

linux系统中有一个logrotate服务,会自动切割日志,防止无限制的增加

[root@localhost rsync]# ls /var/log/messages*

/var/log/messages  /var/log/messages-20180530

[root@localhost rsync]# 

查看日志文件:

[root@localhost ~]# cat /etc/logrotate.conf

see "man logrotate" for details        //会告诉你查看logrotate的帮助文档

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        //是否需要压缩,压缩成 .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.

[root@localhost ~]#

dmesg

[root@localhost rsync]# 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.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version   4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) ) #1 SMP Tue Aug 22 21:09:27 UTC 2017

[    0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-3.10.0-693.el7.x86_64     root=UUID=b36cc2ee-689c-497a-b5f3-cb9814c8facd ro crashkernel=auto rhgb quiet   LANG=zh_CN.UTF-8

[    0.000000] Disabled fast string operations

[    0.000000] e820: BIOS-provided physical RAM map:

[    0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009efff] usable

[    0.000000] BIOS-e820: [mem 0x000000000009f000-0x000000000009ffff] reserved

[    0.000000] BIOS-e820: [mem 0x00000000000ca000-0x00000000000cbfff] reserved

[root@localhost rsync]# 

dmesg命令,会把系统硬件相关的日志列出来 ,这个日志是保存在内存中的,并不是一个文件

假如你的网卡有问题了,硬盘损坏了,都会记录在这个日志中

last

[root@localhost rsync]# last | head

root     pts/1        192.168.1.150    Fri Jun  1 10:05   still logged in   

root     pts/1        192.168.1.150    Fri Jun  1 06:11 - 10:03  (03:52)    

root     pts/1        192.168.1.150    Fri Jun  1 03:22 - 06:10  (02:48)    

root     pts/1        192.168.1.150    Fri Jun  1 00:15 - 03:21  (03:06)    

root     pts/1        192.168.1.150    Thu May 31 21:44 - 00:14  (02:30)    

root     pts/1        192.168.1.150    Thu May 31 20:48 - 21:39  (00:50)    

root     pts/1        192.168.1.150    Thu May 31 19:24 - 20:46  (01:22)    

root     pts/1        192.168.1.150    Thu May 31 16:54 - 19:22  (02:28)    

root     pts/1        192.168.1.150    Thu May 31 12:32 - 16:54  (04:21)    

root     pts/1        192.168.1.150    Thu May 31 11:27 - 12:32  (01:05)    

[root@localhost rsync]# 

查看登录linux的历史信息。

从左至右依次:账户名称,登录终端,登录客户端IP,登陆日期及时长。

xargs

[root@localhost rsync]# ls 3.txt | xargs cat

123

[root@localhost rsync]# 

将管道符前面得输出,作为xargs后面的命令的输入。

常与find命令一起使用。find过滤搜索,xargs 在根据结果再做操作。

exec

效果与xargs相同

批量改文件名

[root@localhost rsync]# ls

1.txt  1.txt_log  2.txt  3.txt

[root@localhost rsync]# find ./*.txt -exec mv {} {}_bak \;

[root@localhost rsync]# ls

1.txt_bak  1.txt_log  2.txt_bak  3.txt_bak

[root@localhost rsync]# 

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

screen

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

首先安装 yun install screen -y

直接敲screen命令,回车,会进入到screen,进入到一个窗口,这个窗口就是一个虚拟终端

在虚拟终端,执行vmstat 1命令,然后 ctrl+a键 同时按,随后再按 d键 ,就把screen丢到后台了

查看已经打开的screen会话:

[root@localhost ~]# screen -ls

There is a screen on:

        13283.pts-1.localhost   (Attached)

1 Socket in /var/run/screen/S-root.

[root@localhost ~]# 

执行screen -r 再加查看到的id 命令,再回到虚拟终端

[root@localhost ~]# screen -r 13283

这时若不需要screen了,直接杀死,按 exit 即可

[root@localhost ~]# screen -ls       

screen No Sockets found in /var/run/screen/S-root. 

[root@localhost ~]# 

扩展

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值