Recurring schedule, package management, sed, and shell programming foundation

Recurring schedule, package management, sed, and shell programming foundation

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"
[root @ localhost /] # crontab - E
0 /12 is / usr / bin / -zcf the tar / Data / Backup / etc - $. (DATE '+ M-%%% Y-D - H-%% M') the tar.gz / ECT / *
2 .rpm package management summary and application examples demonstrate
rpm is a very powerful package management system which allows to work under Linux becomes install, upgrade and remove software packages in easily, and has to install, upgrade, uninstall, and query checking, database maintenance functions.
RPM command: RPM [the OPTIONS] [PACKAGE_FILE]
(. 1) installed:

rpm {-i|--install} [install-options] PACKAGE_FILE ...
rpm  -ivh  PACKAGE_FILE ...
GENERAL OPTIONS:
 -v:verbose,详细信息
 -vv:更详细的输出
[install-options]:
 -h:hash marks输出进度条;每个#表示2%的进度;
 --test:测试安装,检查并报告依赖关系及冲突消息等;
--nodeps:忽略依赖关系;不建议;
--replacepkgs:重新安装
注意:rpm可以自带脚本;
四类:--noscripts
preinstall:安装过程开始之前运行的脚本,%pre , --nopre
postinstall:安装过程完成之后运行的脚本,%post , --nopost
preuninstall:卸载过程真正开始执行之前运行的脚本,%preun, --nopreun
postuninstall:卸载过程完成之后运行的脚本,%postun , --nopostun
--nosignature:不检查包签名信息,不检查来源合法性;
--nodigest:不检查包完整性信息;

(2) upgrade:

rpm {-U|--upgrade} [install-options] PACKAGE_FILE ...
rpm {-F|--freshen} [install-options] PACKAGE_FILE ...
 -U:升级或安装;
 -F:升级
rpm  -Uvh PACKAGE_FILE ...
rpm  -Fvh PACKAGE_FILE ...
 --oldpackage:降级;
 --force:强制升级;
注意:(1) 不要对内核做升级操作;Linux支持多内核版本并存,因此,直接安装新版本内核;
(2) 如果某原程序包的配置文件安装后曾被修改过,升级时,新版本的程序提供的同一个配置文件不会覆盖原有版本的配置文件,而是把新版本的配置文件重命名(FILENAME.rpmnew)后提供;

(3) Uninstall:

rpm {-e|--erase} [--allmatches] [--nodeps] [--noscripts] [--test] PACKAGE_NAME ...----allmatches:卸载所有匹配指定名称的程序包的各版本;
 --nodeps:忽略依赖关系
 --test:测试卸载,dry run模式

(4) query:

rpm {-q|--query} [select-options] [query-options]
[select-options]
PACKAGE_NAME:查询指定的程序包是否已经安装,及其版本;
 -a, --all:查询所有已经安装过的包;
 -f  FILE:查询指定的文件由哪个程序包安装生成;
 -p, --package PACKAGE_FILE:用于实现对未安装的程序包执行查询操作;
 --whatprovides CAPABILITY:查询指定的CAPABILITY由哪个程序包提供;
 --whatrequires CAPABILITY:查询指定的CAPABILITY被哪个包所依赖;
[query-options]
 --changelog:查询rpm包的changlog;
 -l, --list:程序安装生成的所有文件列表;
 -i, --info:程序包相关的信息,版本号、大小、所属的包组,等;
 -c, --configfiles:查询指定的程序包提供的配置文件;
 -d, --docfiles:查询指定的程序包提供的文档;
 --provides:列出指定的程序包提供的所有的CAPABILITY;
-R, --requires:查询指定的程序包的依赖关系;
--scripts:查看程序包自带的脚本片断;
用法:
-qi  PACKAGE, -qf FILE, -qc PACKAGE, -ql PACKAGE, -qd PACKAGE
-qpi  PACKAGE_FILE, -qpl PACKAGE_FILE, -qpc PACKAGE_FILE, ...

(5) Check:

rpm {-V|--verify} [select-options] [verify-options] 
S file Size differs
M Mode differs (includes permissions and file type)
5 digest (formerly MD5 sum) differs
D Device major/minor number mismatch
L readLink(2) path mismatch
U User ownership differs
G Group ownership differs
T mTime differs
P caPabilities differ
包来源合法性验正和完整性验正:
来源合法性验正:
完整性验正:
获取并导入信任的包制作者的密钥:
对于CentOS发行版来说:rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
验正:
(1) 安装此组织签名的程序时,会自动执行验正;
(2) 手动验正:rpm -K PACKAGE_FILE

(6) database Reconstruction:
RPM manager database path: / var / lib / rpm /
query: here performed by the database;

获取帮助:
CentOS 6:man rpm
CentOS 7:man rpmdb
rpm {--initdb|--rebuilddb} [--dbpath DIRECTORY] [--root DIRECTORY]
 --initdb:初始化数据库,当前无任何数据库可实始化创建一个新的;当前有时不执行任何操作;
 --rebuilddb:重新构建,通过读取当前系统上所有已经安装过的程序包进行重新创建;
  • yum configuration and usage summary and create yum private warehouse.
    (1) yum rpm is a front-end tool, you can rely on to solve the problem package. yum configuration file is divided into two parts: one is /etc/yum.conf profiles provide common configuration for all warehouse, a warehouse configuration file is /etc/yum.repos.d/*.repo. yum log file is saved in /etc/log/yum.log file.
    Use yum command:
    yum [Options] [the Command] [Package Penalty for ...]
    显示仓库列表:
    repolist [all|enabled|disabled]
    显示程序包:
    yum list [all | glob_exp1] [glob_exp2] [...]
    yum list {available|installed|updates} [glob_exp1] [...]
    安装程序包:
    install package1 [package2] [...]
    reinstall package1 [package2] [...]  (重新安装)
    升级程序包:
    update [package1] [package2] [...]
    downgrade package1 [package2] [...] (降级)
    检查可用升级:
    check-update
    卸载程序包:
    remove | erase package1 [package2] [...]
    查看程序包information:
    info [...]
    查看指定的特性(可以是某文件)是由哪个程序包所提供:
    provides | whatprovides feature1 [feature2] [...]
    清理本地缓存:
    clean [ packages | metadata | expire-cache | rpmdb | plugins | all ]
    构建缓存:
    makecache
    搜索:
    search string1 [string2] [...]
    以指定的关键字搜索程序包名及summary信息;
    查看指定包所依赖的capabilities:
    deplist package1 [package2] [...]
    查看yum事务历史:
    history [info|list|packages-list|packages-info|summary|addon-info|redo|undo|rollback|new|sync|stats]
    安装及升级本地程序包:
    localinstall rpmfile1 [rpmfile2] [...]
    (maintained for legacy reasons only - use install)
      localupdate rpmfile1 [rpmfile2] [...]
    (maintained for legacy reasons only - use update) 
    包组管理的相关命令:
    groupinstall group1 [group2] [...]
    groupupdate group1 [group2] [...]
    grouplist [hidden] [groupwildcard] [...]
    groupremove group1 [group2] [...]
    groupinfo group1 [...]

(2) create private yum repository

[root@localhost yum.repos.d]# vim epel.repo 
[epel]
 name=Fedora EPEL
 baseurl=https://mirrors.aliyun.com/epel/7/x86_64/
 gpgcheck=0
  • Write a script to achieve the user menu lists the following:
    (1) Disk: Disk info Show information
    (2) mem: show memory info information
    (3) cpu: show cpu info Information
    (*) quit
    /bin/bash
    cat <<EOF
    disk)show disks info
    mem)show mem info
    cpu)show cpu info
    *)QUIT
    EOF
    read -p "Your choice": option
    if [[ "${option}" == "disk" ]];then
    fdisk -l /dev/[sh]d[a-z]
    elif [[ "${option}" == "mem" ]];then
    free -m
    elif [[ "${option}" == "cpu" ]];then
    lscpu
    else
    echo "Unkown option"
    exit 3
    fi
  • sed usage summary with examples demonstrate
    1. Usage:
    sed [OPTION] ... 'Script' [the INPUT-File] ...
    Script:
    Address delimitation editing commands
    commonly used options:
    -n:不输出模式空间中的内容至屏幕;
    -e script, --expression=script:多点编辑;
    -f  /PATH/TO/SED_SCRIPT_FILE
    每行一个编辑命令;
    -r, --regexp-extended:支持使用扩展正则表达式;
    -i[SUFFIX], --in-place[=SUFFIX]:直接编辑原文件 ;
    地址定界:
    (1) 空地址:对全文进行处理;
    (2) 单地址:
    #:指定行;
    /pattern/:被此模式所匹配到的每一行;
    (3) 地址范围
    #,#:
    #,+#:
    #,/pat1/
    /pat1/,/pat2/
    $:最后一行;
    (4) 步进:~
    1~2:所有奇数行
    2~2:所有偶数行
    编辑命令:
    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:显示替换成功的行;
    高级编辑命令:
    h:把模式空间中的内容覆盖至保持空间中;
    H:把模式空间中的内容追加至保持空间中;
    g:把保持空间中的内容覆盖至模式空间中;
    G:把保持空间中的内容追加至模式空间中;
    x:把模式空间中的内容与保持空间中的内容互换;
    n:覆盖读取匹配到的行的下一行至模式空间中;
    N:追加读取匹配到的行的下一行至模式空间中;
    d:删除模式空间中的行;
    D:删除多行模式空间中的所有行;

    2 with examples demonstrate:
    (1) Print / etc / passwd file of the third row, all rows in the original document will be printed:
    sed ‘3p’ /etc/passwd
    (2) prints only the / etc / passwd file the third line
    sed -n ‘3p’ /etc/passwd
    (3) only print / etc / the first to fourth rows passwd file
    sed -n '1,4p' /etc/passwd
    (4) prints only the / etc all lines / passwd file package root character line
    sed -n '/root/p' /etc/passwd
    (5) a first line of print line number, the contents of the second row line printing cycle to print all match line
    sed -n -e '/root/p' -e '/root/=' /etc/passwd
    (6) is added in the next line matching superman line, and line to add over. Supports adding multiple lines.
    sed ‘/root/a\superman\nover’ /etc/passwd
    The line (7) in the matching line by adding superman, and add the new line over. Supports adding multiple lines.
    sed ‘/root/i\superman\nover’ /etc/passwd
    (8) for the new line superman over and replace the matching row line. Replace support multiple lines.
    sed ‘/root/c\superman\nover’ /etc/passwd
    (9) remove the empty file and prints lines
    sed ‘/^$/d’ file
    (10) to replace the root rootsuperman string is & Representative results of each match, such as a plurality of row select only the first matching string matching result
    sed -n 's/root/&superman/p' /etc/passwd
    ( 11) all whitespace characters are deleted / etc / fstab file all lines starting with # # # sign and back
    sed 's@^#[[:space:]]*@@' /etc/fstab

  • Statistics in bash achieve access log file status code greater than or equal to the number of IP 400 and ordered
    (1) awk '$9&gt;400{print $1}' /var/log/httpd/access_log | sort -n | uniq -c
    (2)sed -nr 's@^(.*)- -.*" ([0-9]{3}).*@\1,\2@p' /var/log/httpd/access_log | grep '.* ,[4|5].*' | sort -nr | uniq -c
  • Using homemade yum source mounting ftp, openssh, curl, wget, tcpdump and other packages
     [root@httpd-server yum.repos.d]# vim epel.repo 
      [epel]
      name=Fedora EPEL
      baseurl=https://mirrors.aliyun.com/epel/7/x86_64/
      gpgcheck=0

    yum install -y ftp openssh curl wget tcpdum &&gt; /dev/null

Guess you like

Origin blog.51cto.com/14418331/2436541