Study concluded fifth

1, every 12 hours backup and compression / etc / directory to / backup directory to save the file name format, "etc- years - when - - Month - Day points .tar.gz"

 

crontab -e * / 1 * * * * / usr / bin / tar -zPcf / data / backups / etc-`date + \% F - \% H - \% M`.tar.gz / etc # tar command attention use absolute paths; f parameter tar command to be placed at the end, P parameters can prevent error messages;% use the escape character.

  

2, rpm package management summary and application examples demonstrate.

 

function rpm package : packaged constituent documents compiled application into one or more package files, thus facilitating the realization of the installation packages, upgrade, uninstall, and query functions.

Organization format : (1) the manifest file package (ie, install or uninstall scripts to run), each package are implemented separately

     (2) Database: / usr / lib / rpm / directory, mainly to provide the package name and version, dependency package and the package, as well as the functions of the software file path and the like check information generated by the installation

Way to get the package:

(1) release of the disc system or a file server or official mirror sites, there http: //mirrors.aliyun.com; http: //mirrors.suhu.com; http://mirrors.163.com

Official Site (2) projects: as http.apache.org; www.zabix.org

(3) third-party organizations, such as epel or search tools (pkgs.org, rpmfind.net, rpm.pbone.net)

(4) to produce their own rpm package

rpm package management

rpm package on Centos order management system (install, upgrade, uninstall, query and verification and database maintenance)

installation

Format: rpm [OPTIONS] [Package_file]

                      -i | --install: Installation

[Install-options]: -v: show details of the installation process

        -h: displays a progress bar to #

        --test: Test the installation, do not really install, reports only rely on information and conflicting information

        --nodeps: Ignore dependencies

        --replacepkgs: covering installation

        --force: force the installation

Common combinations of options: -ivh; -ivvh (see more detailed information)

Demo: installation package zsh

~]# rpm  -ivh  zsh-5.0.2-7.e17.x86_64.rpm

upgrade

rpm {-U | --update }[install options] PACKAGE_FILE

      -U: upgrade or install

rpm {-F | --freshen }[install options] PACKAGE_FILE

      -F: Upgrade

Combination: -Uvh; -Fvh

[install options] :

--test: Test the installation, do not really install, reports only rely on information and conflicting information

        --nodeps: Ignore dependencies

        --oldpackages: downgrade to an older version

Note: (1) Do not perform the upgrade of the kernel, linux kernel allows multiple co-exist, so you can directly install several different versions of the kernel

   (2) If, after installing the package configuration file has been modified, upgrade, new versions of the file will not overwrite the old version of the configuration file, but the new version of the configuration file is renamed after saving (suffix .rpmnew)

Uninstall: Remove the installed package

rpm {-e |--erase}  [OPTIONS] PACKAGE_FILE

                              --allmatches: If a package to install multiple versions of the one-time unloading

         --nodeps: Ignore dependencies

         --test: Uninstall test

Query: Check if a package has been installed, and check the installation of all the packages, you can also check information about a specific package

用法:rpm {-q | --query} [selece-options]  [query-options]  

[Selece-options]: choose Options

        rpm -q PACKAGE_NAME: query specified package has been installed and its version

        rpm -qa: query the package has been installed

        rpm -qf / PATH / TO / SMEFILE: query a file which is installed by the packet generation

        rpm -qpi: Query not yet installed package information      

        --whatprovides CAPABILITY: Query CAPABBILITY developed which is generated by the installation package

        --whatrequires CAPABILITY: query is formulated CAPABBILITY depends on which installation package

 [Query-options]: Query Options

(1) - change: Modify the query log of a rpm package

Such as: ~] # rpm -q --change bash

(2) -l | --list: list a list of installation packages generated files

Such as: ~] # rpm -ql bash 

(3) -i: query a brief description of the information package

Such as: rpm -qi bash

(4) -c: a query package installation all profiles generated after completion

Such as: ~] #rpm -qc bash

(5) -d: query help documentation specifying the package offered

Such as: ~] #rpm -qd bash

(6) - provides: lists CAPABILITY specified package provided; - whatprovides: Query bash the ability to provide a package which

如:~]#rpm -q  --provides bash  ;      ~]#rpm -q  --whatprovides bash 

(7) - requires: Query a package dependent CABABILITY

如:rpm -q --requires bash

(8) - scripts: the query execution script (script by the class 4) the time of installing or uninstalling a package

Such as: rpm -q --scripts bash

Four types of script: preinstall: script execution before the installation process begins

       postinstall: script execution After the installation process is completed

       Script execution before unloading: preuninstall

       postuninstall: script executed after the uninstall process is completed

Example:

[root @ 192 ~] # rpm -qi bash ######### queries bash brief description of the information 
the Name: bash 
Version: 4.2.46 The 
Release: 30.el7 
Architecture: x86_64 
the Install a Date: Jul 28, 2019 Tuesday 21:34:37 
Group: the System Environment / the Shells 
Size: 3667709 
License: the GPLv3 + 
the Signature: the RSA / the SHA256, 2018 years 04 Wednesday, 25 October 18:54:19, Key ID 24c6a8a7f4a80eb5 
the Source the RPM: the bash-4.2. 46-30.el7.src.rpm 
Build a Date: 2018 Nian 04 Wednesday, 11 October 08 when 55 minutes 22 seconds 
Build Host: x86-01.bsys.centos.org 
Relocations: (not relocatable) 
Packager: CentOS buildsystem <HTTP: //bugs.centos.org> 
Vendor: CentOS  
the URL of: http://www.gnu.org/software/bash
the Summary: the GNU Bourne Again shell at The 
the Description:
The GNU Bourne Again shell (Bash) is a shell or command language
interpreter that is compatible with the Bourne shell (sh). Bash
incorporates useful features from the Korn shell (ksh) and the C shell
(csh). Most sh scripts can be run by bash without modification.
[root@192 ~]# rpm -qc httpd                        ###########查询httpd包安装之后生成的配置文件
/etc/httpd/conf.d/autoindex.conf
/etc/httpd/conf.d/userdir.conf
/etc/httpd/conf.d/welcome.conf
/etc/httpd/conf.modules.d/00-base.conf
/etc/httpd/conf.modules.d/00-dav.conf
/etc/httpd/conf.modules.d/00-lua.conf
/etc/httpd/conf.modules.d/00-mpm.conf
/etc/httpd/conf.modules.d/00-proxy.conf
/etc/httpd/conf.modules.d/00-systemd.conf
/etc/httpd/conf.modules.d/01-cgi.conf
/etc/httpd/conf/httpd.conf
/etc/httpd/conf/magic
/etc/logrotate.d/httpd
/etc/sysconfig/htcacheclean
/etc/sysconfig/httpd

  

Check: whether to generate a query after the installation file has changed

format:rpm {-V | verify} [select-option] [verify-option]

[root@192 ~]# rpm -V httpd

 

3, yum configuration and use yum summarize and create a private warehouse.

 

YUM: Yellowdog Update Modifier, complete RHEL series front-end tool automatically resolve dependencies installed, depending on the presence rpm

YUM configuration file path

/etc/yum.conf:为所有yum仓库提供公共配置
/etc/yum.repos.d/*.repo:为仓库的指向提供配置

The main configuration defined /etc/yum.conf

[root@zabbix-mysql ~]# cat /etc/yum.conf
[main]      # 通用主配置段
cachedir=/var/cache/yum/x86_64/$releasever      # 缓存文件文件路径
keepcache=1     # 缓存到本地的文件是否保存 
debuglevel=2    # 调试级别   
logfile=/var/log/yum.log    # 安装的日志文件
exactarch=1     # 精确严格的平台 release 匹配
obsoletes=1
gpgcheck=1      # 检测来源合法性和完整性
plugins=1       # 支持插件机制
installonly_limit=5     同时安装多少程序包
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_report_page.php?category=yum
distroverpkg=centos-release     #当前发行版本号从哪儿获取

[root@zabbix-mysql ~]# ll /var/cache/yum/x86_64/7
total 28
drwxr-xr-x 4 root root 4096 May 21 23:09 aliyun
drwxr-xr-x 4 root root 4096 Nov 21 18:50 base
drwxr-xr-x 4 root root 4096 Apr 16  2018 centos7-yum
drwxr-xr-x 4 root root 4096 Nov 21 18:50 extras
drwxr-xr-x 4 root root  132 Nov 20  2018 mysql57-community
drwxr-xr-x 4 root root  132 Nov 20  2018 mysql-connectors-community
drwxr-xr-x 4 root root  132 Nov 20  2018 mysql-tools-community
-rw-r--r-- 1 root root  183 May 21 23:09 timedhosts
-rw-r--r-- 1 root root    0 May 21 23:09 timedhosts.txt
drwxr-xr-x 4 root root 4096 Nov 21 18:50 updates
drwxr-xr-x 4 root root 4096 Nov 22 18:16 zabbix
drwxr-xr-x 4 root root   73 Nov 22 18:16 zabbix-non-supported

[root@zabbix-mysql ~]# tail /var/log/yum.log
Feb 14 17:33:08 Installed: apr-util-1.5.2-6.el7.x86_64
Feb 14 17:33:08 Installed: httpd-tools-2.4.6-88.el7.centos.x86_64
Mar 28 09:52:05 Installed: 2:nmap-ncat-6.40-16.el7.x86_64
Apr 08 10:55:19 Installed: libcgroup-0.41-20.el7.x86_64
Apr 08 10:55:19 Installed: libcgroup-tools-0.41-20.el7.x86_64
Apr 08 16:00:21 Updated: 14:libpcap-1.5.3-11.el7.x86_64
Apr 08 16:00:21 Installed: 14:tcpdump-4.9.2-3.el7.x86_64
Apr 28 21:52:15 Installed: iotop-0.6-4.el7.noarch
May 08 18:03:45 Installed: sysstat-10.1.5-17.el7.x86_64
May 21 23:09:34 Installed: 2:nmap-6.40-16.el7.x86_64

** yum repository (* .repo) configuration file defines **

[root@zabbix-mysql yum.repos.d]# cat aliyun.repo
[aliyun]        # 仓库ID
name=aliyun     
baseurl=http://mirrors.aliyun.com/centos/7/os/x86_64    # yum 仓库指定的访问路径(阿里源)
enabled=1   # 启用此仓库
gpgcheck=0  # 检查完整性和来源合法性

YUM commonly used commands

  • yum repolist display a list of warehouse
  • yum list display program package
  • yum search Fuzzy Lookup package
  • yum install installation package
  • yum remove uninstalled packages
  • yum update [package1] [package2] [...] upgrade
  • yum downgrade package1 [package2] [...] destaging
  • yum info [package1] [package2] [...] View brief information package
  • yum provides | whatprovides feature1 [feature2] [...] characteristics (can be a file) to view the specified package which is provided by the
  • yum clean <packages | metadata | expire-cache | rpmdb | plugins | all> local cache cleanup
// 查看yum仓库
[root@zabbix-mysql yum.repos.d]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
repo id                                                repo name                                             status
!aliyun                                                aliyun                                                10,019 repolist: 10,019 // 模糊查询安装包 [root@zabbix-mysql yum.repos.d]# yum search ftp Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile ================================================ N/S matched: ftp ================================================= ftp.x86_64 : The standard UNIX FTP (File Transfer Protocol) client lftp-scripts.noarch : Scripts for lftp syslinux-tftpboot.noarch : SYSLINUX modules in /var/lib/tftpboot, available for network booting tftp.x86_64 : The client for the Trivial File Transfer Protocol (TFTP) tftp-server.x86_64 : The server for the Trivial File Transfer Protocol (TFTP) vsftpd.x86_64 : Very Secure Ftp Daemon vsftpd-sysvinit.x86_64 : SysV initscript for vsftpd daemon curl.x86_64 : A utility for getting files from remote servers (FTP, HTTP, and others) lftp.i686 : A sophisticated file transfer program lftp.x86_64 : A sophisticated file transfer program wget.x86_64 : A utility for retrieving files using the HTTP or FTP protocols Name and summary matches only, use "search all" for everything. // 查询已安装与未安装的匹配我包 [root@zabbix-mysql yum.repos.d]# yum list *ftp* Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile Installed Packages ftp.x86_64 0.17-67.el7 @centos7-yum Available Packages lftp.i686 4.4.8-11.el7 aliyun lftp.x86_64 4.4.8-11.el7 aliyun lftp-scripts.noarch 4.4.8-11.el7 aliyun syslinux-tftpboot.noarch 4.05-15.el7 aliyun tftp.x86_64 5.2-22.el7 aliyun tftp-server.x86_64 5.2-22.el7 aliyun vsftpd.x86_64 3.0.2-25.el7 aliyun vsftpd-sysvinit.x86_64 3.0.2-25.el7 aliyun 

Creating a private repository yum

  • The first: release the disc as a yum repository

    • Mount the CD or ISO file to a mount point
    mkdir /mnt/cdrom        
    mount /dev/sr0 /mnt/cdrom
    • Yum backup the original source:
    mkdir /etc/yum.repos.d/bak       
    mv /etc/yum.repos.d/* /etc/yum.repos.d/bak/   
    • Increase yum source of a CD-ROM
    #vi /etc/yum.repos.d/mybase.repo        
    [mybase]        
    name=LocalYum        
    baseurl=file:///mnt/cdrom        
    gpgcheck=0        
    enabled=1
  • Second, using the local network or manually create a rpm package

    • Step 1: Installcreaterepo
    • Step two: determine repository output: two local output network output.
      Local output: a rpm package placed local path
      network output: a file server, configure the ftp service or http services (Note firewall to open the corresponding port)

    ftp service:

    Installation package: vsftpd

    File storage path: / var / ftp / pub

               启动服务:service vsftpd start
    
      http服务:

    Installation package: httpd

    File storage path: / var / ftp / pub

    Start the service: service httpd start

    • The third step: the rpm package file into the prepared catalog, can be copied from the CD-ROM rpm packages in the past.
    • Step Four: Run this directory  createrepo can be ordered, this completes the creation of a local warehouse.
    • yum repository created test is available
  • Create a file ending in /etc/yum.repos.d/ repo directory, for example:

[root@localhost yum.repos.d]# yum info createrepo   //查看已安装的createrepo信息
[root@localhost yum.repos.d]# mkdir -p /yumtest/Packages    
[root@localhost yum.repos.d]# cp /media/cdrom/Packages/* /yumtest/Packages/     //下载所需要的所有文件到本地某个路径
[root@localhost yum.repos.d]# createrepo /yumtest/   //创建关联关系,完成后生成repodata
root@localhost yum.repos.d]# cat Local.repo    //配置yum源文件
[Local]
name=Local Yum
baseurl=file:///yumtest/
#baseurl=http://<repo_host>/Packages     //当为http网络输出时,用此配置。<repo_host>仓库主机IP,Packages为http发布目录下的软件包目录
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
enabled=1

[root@localhost yum.repos.d]# yum repolist       //查看配置好的yum仓库

 

4, write a script to achieve the user menu lists the following:

 (1) disk: show disk info information

 (2) mem: show memory info information

 (3) cpu: show cpu info information

 (*)quit

[root@etcd03 tmp]# cat test1.sh
#!/bin/bash
read -p "Your choice:" choice
case $choice in
disk)
        fdisk -l;;
mem)
        cat /proc/meminfo;;
cpu)
        lscpu;;
*)
        exit;;
esac

   

5, sed usage summary and presentation with examples

       Common options

-n:不输出模式空间中的内容至屏幕;
-e script, --expression=script:多点编辑;
-f  /PATH/TO/SED_SCRIPT_FILE   : 每行一个编辑命令;
-r, --regexp-extended:支持使用扩展正则表达式;
-i[SUFFIX], --in-place[=SUFFIX]:直接编辑原文件 ;
  • Address delimitation
1)空地址:什么都不填写时,表示对全文进行处理
2)单地址
     #:表示对指定的行
   /pattern/:被此模式所匹配到的每一行,其中模式两边的/不能省略
  • Address range (# stands for any number)
#,#:从某行到某行,第二个#大于第一个#
#,+#:从某行开始加多商行 #,/pattern1/:从某行开始到第一次配到模式的行 /pattern1/,/pattern2/:从第一次匹配到的模式1到第二次匹配到哦的模式2 步进:~,#~# 从第几行 1~2:所有奇数行。从第一行开始,每次加两行读取 2~2:所有偶数行。从第二行开始,每次加两行读取
  • Edit command
d:         删除;
p:         显示模式空间中的内容;
a  \text:  在行后面追加文本“text”,支持使用\n实现多行追加;
i  \text:  在行前面插入文本“text”,支持使用\n实现多行插入;
c  \text: 把匹配到的行替换为此处指定的文本“text”; w /PATH/TO/SOMEFILE: 保存模式空间匹配到的行至指定的文件中; r /PATH/FROM/SOMEFILE: 读取指定文件的内容至当前文件被模式匹配到的行后面;文件合并; =: 为模式匹配到的行打印行号; !: 条件取反; s///: 查找替换,其分隔符可自行指定,常用的有s@@@, s###等; 替换标记: g:全局替换; w /PATH/TO/SOMEFILE:将替换成功的结果保存至指定文件中; p:显示替换成功的行;
  • Alternatively string matching pattern
#替换/etc/passwd中的:分割符为- UID:字符
cat /etc/passwd | cut -d : -f1,3 | sed 's/:/ - UID: /'
root - UID: 0 bin - UID: 1
  • Modify data directly replace the original file
sed -i 's/text/replace/g' file

#/3g标记替换第3次出现后的匹配:
echo "thisthisthisthisthis" | sed 's/this/THIS/3g'
thisthisTHISTHISTHIS
  • Matched string tag &
#通过正则表达式\w\+匹配每一个单词,然后用[&]替换它。&对应于之前所匹配到的单词。
echo "this is an example" | sed 's/\w\+/[&]/g'
[this] [is] [an] [example]
  • Substring matching flag (\ 1)
echo "sever EIGHT" | sed 's/\([a-z]\+\) \([A-Z]\+\)/\2 \1/g'
EIGHT sever

 

6, to achieve statistical access log file status code greater than bash with equal number of IP 400 and ordered

 

#!/bin/bash
nginx_log='/usr/local/nginx/logs/host.access.log'
code_400_count=$(awk '($9 >= 400) {print $1}' ${nginx_log} | sort -rn | uniq -c)
echo $code_400_count

   

7, the use of home-made yum source mounting ftp, openssh, curl, wget, tcpdump and other packages

** Other non-self-closing yum source, provided the corresponding file * .repo enabled = 0 **

yum install -y ftp --enablerepo=Local Yum
yum install -y openssh --enablerepo=Local Yum
yum install -y curl --enablerepo=Local Yum
yum install -y wget --enablerepo=Local Yum
yum install -y tcpdump --enablerepo=Local Yum

  

Guess you like

Origin www.cnblogs.com/yurong001122/p/11260920.html