业务系统版本库或日志目录的自定义删除

 

案例一、 自定义删除版本库目录
线上业务系统版本在版本机上的存放目录为/opt/data/ONLINE,目录结构为: /opt/data/ONLINE/系统名称/应用名称/版本号,版本号是以日期(年月日)8位数字命令的。

查看业务系统
[root@localhost ONLINE]# ll /opt/data/ONLINE/
total 60
drwxr-xr-x 17 root root 4096 Dec 12 13:11 AFG
drwxr-xr-x 10 root root 4096 Dec 12 13:09 AFS
drwxr-xr-x 17 root root 4096 Dec 12 13:11 BAU
drwxr-xr-x 17 root root 4096 Dec 12 13:11 CCD
drwxr-xr-x 10 root root 4096 Dec 12 13:09 EFE
drwxr-xr-x 17 root root 4096 Dec 12 13:11 ERU
drwxr-xr-x 17 root root 4096 Dec 12 13:11 HUU
drwxr-xr-x 10 root root 4096 Dec 12 13:09 IFI
drwxr-xr-x 10 root root 4096 Dec 12 13:09 IOO
drwxr-xr-x 17 root root 4096 Dec 12 13:11 JKL
drwxr-xr-x 17 root root 4096 Dec 12 13:11 KPO
drwxr-xr-x 17 root root 4096 Dec 12 13:11 NFA
drwxr-xr-x 10 root root 4096 Dec 12 13:09 OOS
drwxr-xr-x 17 root root 4096 Dec 12 13:11 QJK
drwxr-xr-x 10 root root 4096 Dec 12 13:09 UPP

查看每个业务系统下的应用模块
[root@localhost ONLINE]# ll /opt/data/ONLINE/*/

比如查看AFG系统下的应用模块
[root@localhost ONLINE]# ll /opt/data/ONLINE/AFG
total 60
drwxr-xr-x 17 root root 4096 Dec 13 11:45 bacm
drwxr-xr-x 17 root root 4096 Dec 13 11:45 bay_uo
drwxr-xr-x 17 root root 4096 Dec 13 11:45 habnat
drwxr-xr-x 17 root root 4096 Dec 13 11:45 her
drwxr-xr-x 17 root root 4096 Dec 13 11:45 kio_pro
drwxr-xr-x 17 root root 4096 Dec 13 11:45 mod_bank
drwxr-xr-x 17 root root 4096 Dec 13 11:45 mpb_fos
drwxr-xr-x 17 root root 4096 Dec 13 11:45 namp_qs
drwxr-xr-x 17 root root 4096 Dec 13 11:45 pay_core
drwxr-xr-x 17 root root 4096 Dec 13 11:45 poo_kat
drwxr-xr-x 17 root root 4096 Dec 13 11:45 sand
drwxr-xr-x 17 root root 4096 Dec 13 11:45 sanf_pat
drwxr-xr-x 17 root root 4096 Dec 13 11:45 sans
drwxr-xr-x 17 root root 4096 Dec 13 11:45 werq
drwxr-xr-x 17 root root 4096 Dec 13 11:45 yar_kl

查看业务系统下应用模块的版本库
[root@localhost ONLINE]# ls /opt/data/ONLINE/*/*

比如查看AFG系统下的bacm应用的版本库
[root@localhost ONLINE]# ll /opt/data/ONLINE/AFG/bacm/
total 60
drwxr-xr-x 2 root root 4096 Dec 13 11:45 20190102
drwxr-xr-x 2 root root 4096 Dec 13 11:45 20190103
drwxr-xr-x 2 root root 4096 Dec 13 11:45 20190104
drwxr-xr-x 2 root root 4096 Dec 13 11:45 20190105
drwxr-xr-x 2 root root 4096 Dec 13 11:45 20190106
drwxr-xr-x 2 root root 4096 Dec 13 11:45 20190107
drwxr-xr-x 2 root root 4096 Dec 13 11:45 20190108
drwxr-xr-x 2 root root 4096 Dec 13 11:45 20190202
drwxr-xr-x 2 root root 4096 Dec 13 11:37 20190203
drwxr-xr-x 2 root root 4096 Dec 12 13:16 20190204
drwxr-xr-x 2 root root 4096 Dec 12 13:16 20190205
drwxr-xr-x 2 root root 4096 Dec 12 13:16 20190207
drwxr-xr-x 2 root root 4096 Dec 12 13:16 20190210
drwxr-xr-x 2 root root 4096 Dec 12 13:16 20190211
drwxr-xr-x 2 root root 4096 Dec 12 13:16 20190212

=====================================================================================
现在需要清理版本库/opt/data/ONLINE的空间,即删除各业务系统下的版本号,只保留最近7天的
版本号数据。

脚本如下:
[root@localhost ~]# cat /opt/data/script/del_online_Sysname_version.sh 
#!/bin/sh
#scriptname:del_online_Sysname_version.sh
#author:kevin
#time:2018:12:01

#保留的版本号数量
# read -p "请输入版本号删除后保留的份数: " keepnum
keepnum=7

#记录各系统删除的版本号
LogPath=/opt/data/script/version_clear.log

Online_Version_Path=/opt/data/ONLINE
#注意这里的${Online_Version_Path}/*,后面用-maxdepth 0,表示搜索${Online_Version_Path}目录下的那一层目录
#如果是${Online_Version_Path}/,即后面不加*,则后面要用-maxdepth 1,表示搜索${Online_Version_Path}目录下的那一层目录
for Sysname in $(find ${Online_Version_Path}/* -maxdepth 0 -type d|awk -F"/" '{print $NF}')
do
  echo -e "`date +"%Y-%m-%d %H:%M:%S"`: ########### 开始清理${Sysname}版本号目录${Online_Version_Path}/${Sysname} ###########">> ${LogPath}
  for Appname in `find ${Online_Version_Path}/${Sysname}/* -maxdepth 0 -type d|awk -F"/" '{print $NF}'`
  do
    echo -e "`date +"%Y-%m-%d %H:%M:%S"`: 开始清理${Sysname}的应用${Appname}下的版本号" >> ${LogPath}
    #grep "^[0-9]\{8,8\}"表示以数字开头,连续出现8个数字。
    #ls -lrt ${Appname} 会以目录时间降序排列。即前面五个目录是最新的。默认最新的都放在后面。
    #这里只需要统计目录总数量,所以用ls -l即可
    Before_Del_version_Num=$(ls -l ${Online_Version_Path}/${Sysname}/${Appname} |grep ^d|awk '{ print $NF }'|grep "^[0-9]\{8,8\}"|wc -l)
    Del_version_Num=$(`which expr` ${Before_Del_version_Num} - ${keepnum})

    #开始清理版本
    for Del_version in $(ls -l ${Online_Version_Path}/${Sysname}/${Appname} |grep ^d|awk '{ print $NF }'|grep "^[0-9]\{8,8\}"|sed -n "1,${Del_version_Num}p")
    do
       cd ${Online_Version_Path}/${Sysname}/${Appname} && rm -rf ${Del_version}
       if [ $? -eq 0 ];then
          echo "`date +"%Y-%m-%d %H:%M:%S"`: 开始清理版本,${Del_version}已被成功删除!"|tee -a ${LogPath}
       fi
    done
  done
done


执行脚本:
[root@localhost ~]# chmod 755 /opt/data/script/del_online_Sysname_version.sh
[root@localhost ~]# sh /opt/data/script/del_online_Sysname_version.sh

查看执行日志:
[root@localhost ~]# cat /opt/data/script/version_clear.log 
2019-12-13 11:53:40: ########### 开始清理AFG版本号目录/opt/data/ONLINE/AFG ###########
2019-12-13 11:53:40: 开始清理AFG的应用bacm下的版本号
2019-12-13 11:53:40: 开始清理版本,20190102已被成功删除!
2019-12-13 11:53:40: 开始清理版本,20190103已被成功删除!
2019-12-13 11:53:40: 开始清理版本,20190104已被成功删除!
2019-12-13 11:53:40: 开始清理版本,20190105已被成功删除!
2019-12-13 11:53:40: 开始清理版本,20190106已被成功删除!
2019-12-13 11:53:40: 开始清理版本,20190107已被成功删除!
2019-12-13 11:53:40: 开始清理版本,20190108已被成功删除!
2019-12-13 11:53:40: 开始清理版本,20190202已被成功删除!
2019-12-13 11:53:40: 开始清理AFG的应用bay_uo下的版本号
2019-12-13 11:53:40: 开始清理版本,20190102已被成功删除!
2019-12-13 11:53:40: 开始清理版本,20190103已被成功删除!
2019-12-13 11:53:40: 开始清理版本,20190104已被成功删除!
2019-12-13 11:53:40: 开始清理版本,20190105已被成功删除!
2019-12-13 11:53:40: 开始清理版本,20190106已被成功删除!
2019-12-13 11:53:40: 开始清理版本,20190107已被成功删除!
2019-12-13 11:53:40: 开始清理版本,20190108已被成功删除!
2019-12-13 11:53:40: 开始清理版本,20190202已被成功删除!
2019-12-13 11:53:40: 开始清理AFG的应用habnat下的版本号
2019-12-13 11:53:40: 开始清理版本,20190102已被成功删除!
2019-12-13 11:53:40: 开始清理版本,20190103已被成功删除!
2019-12-13 11:53:40: 开始清理版本,20190104已被成功删除!
2019-12-13 11:53:40: 开始清理版本,20190105已被成功删除!
2019-12-13 11:53:40: 开始清理版本,20190106已被成功删除!
2019-12-13 11:53:40: 开始清理版本,20190107已被成功删除!
2019-12-13 11:53:40: 开始清理版本,20190108已被成功删除!
2019-12-13 11:53:40: 开始清理版本,20190202已被成功删除!
2019-12-13 11:53:41: 开始清理AFG的应用her下的版本号
2019-12-13 11:53:41: 开始清理版本,20190102已被成功删除!
.........
.........

验证脚本执行后,各系统的版本号是否保留了最近7天的数据
[root@localhost ~]# ll /opt/data/ONLINE/*/*|head -50
/opt/data/ONLINE/AFG/bacm:
total 28
drwxr-xr-x 2 root root 4096 Dec 13 11:37 20190203
drwxr-xr-x 2 root root 4096 Dec 12 13:16 20190204
drwxr-xr-x 2 root root 4096 Dec 12 13:16 20190205
drwxr-xr-x 2 root root 4096 Dec 12 13:16 20190207
drwxr-xr-x 2 root root 4096 Dec 12 13:16 20190210
drwxr-xr-x 2 root root 4096 Dec 12 13:16 20190211
drwxr-xr-x 2 root root 4096 Dec 12 13:16 20190212

/opt/data/ONLINE/AFG/bay_uo:
total 28
drwxr-xr-x 2 root root 4096 Dec 13 11:37 20190203
drwxr-xr-x 2 root root 4096 Dec 12 13:16 20190204
drwxr-xr-x 2 root root 4096 Dec 12 13:16 20190205
drwxr-xr-x 2 root root 4096 Dec 12 13:16 20190207
drwxr-xr-x 2 root root 4096 Dec 12 13:16 20190210
drwxr-xr-x 2 root root 4096 Dec 12 13:16 20190211
drwxr-xr-x 2 root root 4096 Dec 12 13:16 20190212

/opt/data/ONLINE/AFG/habnat:
total 28
drwxr-xr-x 2 root root 4096 Dec 13 11:37 20190203
drwxr-xr-x 2 root root 4096 Dec 12 13:16 20190204
drwxr-xr-x 2 root root 4096 Dec 12 13:16 20190205
drwxr-xr-x 2 root root 4096 Dec 12 13:16 20190207
drwxr-xr-x 2 root root 4096 Dec 12 13:16 20190210
drwxr-xr-x 2 root root 4096 Dec 12 13:16 20190211
drwxr-xr-x 2 root root 4096 Dec 12 13:16 20190212

.........
.........

发现通过上面脚本的执行,各业务系统的版本号成功被清理了,并正确保留了最近7天的数据!

案例二、自定义删除日志目录
线上某些业务系统的日志不定期产生, 有的每天产生, 有的好几天才产生, 因为系统只有在用的时候才产生日志,日志文件均存放在以当天日期命名的目录下. 当日志目录越来越多时就需要处理, 由此开发同事提出来一个需求, 需要一个自定义删除或保留这些日志目录的脚本。

[root@localhost pay-sign-oper]# ls
2018-09-13  2018-09-20  2018-10-11  2018-10-28  2018-11-02  2018-11-14  2018-12-04  2018-12-09          
2018-09-14  2018-09-27  2018-10-24  2018-10-29  2018-11-06  2018-11-15  2018-12-05  2018-12-10
2018-09-15  2018-09-29  2018-10-25  2018-10-30  2018-11-07  2018-11-30  2018-12-06  2018-12-11
2018-09-17  2018-09-30  2018-10-26  2018-10-31  2018-11-08  2018-12-02  2018-12-07  2018-12-12
2018-09-19  2018-10-08  2018-10-27  2018-11-01  2018-11-09  2018-12-03  2018-12-08  pay-sign-oper.log

[root@localhost pay-sign-oper]# ls 2018-09-13/
pay-sign-oper.2018-09-13.log

[root@localhost pay-sign-oper]# ls 2018-12-12/
pay-sign-oper.2018-12-12.log

比如现在开发同事想要删除2018年11月08号之前的日志, 现在就需要一个智能脚本, 用来一键实现这个需求.

脚本内容如下:

[root@localhost pay-sign-oper]# cat date_log_delete.sh 
#!/bin/bash

echo -n "请输入日期, 截止到该日期之前的日志目录接下来将要被删除:"  
read  date1

date2=$(echo ${date1}|awk -F"-" '{print $1$2$3}')

cd `pwd`
for date3 in $(ls -l|grep "drwxr"|awk '{print $9}'|awk -F"-" '{print $1$2$3}')
do
   a=$(echo ${date3}|cut -c 1-4)
   b=$(echo ${date3}|cut -c 5-6)
   c=$(echo ${date3}|cut -c 7-8)
   date4=$(echo ${a}-${b}-${c})

     if [ ${date3} -lt ${date2} ];then
        rm -rf ${date4} && echo "deleted ${date4}"
     else
        echo "${date4} do not need to delete"
     fi
done

授予脚本执行权限
[root@localhost pay-sign-oper]# chmod 755 date_log_delete.sh
[root@localhost pay-sign-oper]# ll date_log_delete.sh
-rwxr-xr-x 1 root root 557 Dec 13 14:50 date_log_delete.sh

比如现在想要删除2018年9月27号之前的日志, 则脚本执行后, 按照提示输入:2018-09-27 即可实现:

[root@localhost pay-sign-oper]# sh date_log_delete.sh 
请输入日期, 截止到该日期之前的日志目录接下来将要被删除:2018-09-27
deleted 2018-09-13
deleted 2018-09-14
deleted 2018-09-15
deleted 2018-09-17
deleted 2018-09-19
deleted 2018-09-20
2018-09-27 do not need to delete
2018-09-29 do not need to delete
2018-09-30 do not need to delete
2018-10-08 do not need to delete
2018-10-11 do not need to delete
2018-10-24 do not need to delete
2018-10-25 do not need to delete
2018-10-26 do not need to delete
2018-10-27 do not need to delete
2018-10-28 do not need to delete
2018-10-29 do not need to delete
2018-10-30 do not need to delete
2018-10-31 do not need to delete
2018-11-01 do not need to delete
2018-11-02 do not need to delete
2018-11-06 do not need to delete
2018-11-07 do not need to delete
2018-11-08 do not need to delete
2018-11-09 do not need to delete
2018-11-14 do not need to delete
2018-11-15 do not need to delete
2018-11-30 do not need to delete
2018-12-02 do not need to delete
2018-12-03 do not need to delete
2018-12-04 do not need to delete
2018-12-05 do not need to delete
2018-12-06 do not need to delete
2018-12-07 do not need to delete
2018-12-08 do not need to delete
2018-12-09 do not need to delete
2018-12-10 do not need to delete
2018-12-11 do not need to delete
2018-12-12 do not need to delete

脚本执行看, 查看, 发现2018年9月27号之前的日志和日志目录都被删除了.
[root@localhost pay-sign-oper]# ls
2018-09-27  2018-10-11  2018-10-27  2018-10-31  2018-11-07  2018-11-15  2018-12-04  2018-12-08  2018-12-12
2018-09-29  2018-10-24  2018-10-28  2018-11-01  2018-11-08  2018-11-30  2018-12-05  2018-12-09  date_log_delete.sh
2018-09-30  2018-10-25  2018-10-29  2018-11-02  2018-11-09  2018-12-02  2018-12-06  2018-12-10  pay-sign-oper.log
2018-10-08  2018-10-26  2018-10-30  2018-11-06  2018-11-14  2018-12-03  2018-12-07  2018-12-11

以上脚本看起来非常好用, 非常智能! 此脚本可以在任何这样的日志场景下使用. 下面针对脚本中涉及到的一些shell小脚本进行拆分说明:

1)
echo -n "请输入日期, 截止到该日期之前的日志目录接下来将要被删除:" 
read  date1
 
上面两行脚本内容用到了shell脚本中的read参数.
read参数表示接收标准输入(键盘)的输入,或其他文件描述符的输入。
得到输入内容后,read参数将该内容传递到一个标准变量中。
也就是脚本执行后提示输入的日期, 并将该日期传给${date1}变量
 
2)
date2=$(echo ${date1}|awk -F"-" '{print $1$2$3}')
 
这一行脚本内容是将输入的日期的格式转变成数字格式, 并传给变量${date2}, 是为了和后面的变量${date3}做if语句中的大小判断之用.
比如: 脚本执行后提示输入的内容是2018-09-27, 这个也就是变量${date1}的数值. 则变量${date}的值就是20180927.
[root@localhost pay-sign-oper]# echo 2018-09-27|awk -F"-" '{print $1$2$3}'
20180927
 
3)
cd `pwd`
 
这一行脚本内容表示切换到当前目录路径下 (其实可以不写这一行, 因为脚本文件就在当前目录下)
 
4)
for date3 in $(ls -l|grep "drwxr"|awk '{print $9}'|awk -F"-" '{print $1$2$3}')
 
这一行脚本内容表示在当前目录下取日期目录, 并将该日期目录的格式转变成数字形式, 并传给变量${date3}, 是为了和${date2}做if语句中的大小判断之用. 如下:
[root@localhost pay-sign-oper]# ls -l
total 136
drwxr-xr-x 2 root root 4096 Dec 13 14:56 2018-09-27
drwxr-xr-x 2 root root 4096 Dec 13 14:56 2018-09-29
drwxr-xr-x 2 root root 4096 Dec 13 14:55 2018-09-30
drwxr-xr-x 2 root root 4096 Dec 13 14:55 2018-10-08
...........
...........
drwxr-xr-x 2 root root 4096 Dec 13 12:40 2018-12-10
drwxr-xr-x 2 root root 4096 Dec 13 12:40 2018-12-11
drwxr-xr-x 2 root root 4096 Dec 13 12:40 2018-12-12
-rwxr-xr-x 1 root root  557 Dec 13 14:50 date_log_delete.sh
-rw-r--r-- 1 root root    0 Dec 13 12:40 pay-sign-oper.log


[root@localhost pay-sign-oper]# ls -l|grep "drwxr"|awk '{print $9}'
2018-09-27
2018-09-29
2018-09-30
2018-10-08
...........
...........
2018-12-10
2018-12-11
2018-12-12

[root@localhost pay-sign-oper]# ls -l|grep "drwxr"|awk '{print $9}'|awk -F"-" '{print $1$2$3}'
20180927
20180929
20180930
20181008
...........
...........
20181210
20181211
20181212
 
5)
a=$(echo ${date3}|cut -c 1-4)
b=$(echo ${date3}|cut -c 5-6)
c=$(echo ${date3}|cut -c 7-8)
 
这三行脚本内容表示提取一个数字字符串中的某几位. a , b, c 三个变量分别表示取变量${date3}数字中的第1-4位, 第5-6位, 第7-8位.
比如变量${date3}的数值是20180927, 则:
[root@localhost pay-sign-oper]# echo 20180927|cut -c 1-4
2018
[root@localhost pay-sign-oper]# echo 20180927|cut -c 5-6
09
[root@localhost pay-sign-oper]# echo 20180927|cut -c 7-8
27
 
6) date4=$(echo ${a}-${b}-${c})
 
这一行脚本内容表示将变量${date3}的数值拆分后再组成日期格式. 为了后面做if语句中的判断结果后的日期目录删除操作.
 
7)
if [ ${date3} -lt ${date2} ];then
        rm -rf ${date4} && echo "deleted ${date4}"
     else
        echo "${date4} do not need to delete"
     fi
 
上面的脚本内容表示if语句中判断,:
当变量${date3}数值小于变量${date2}的数值时, 就删除变量${date4}的日期目录,并打印删除信息.
当变量${date3}数值大于等于变量${date2}的数值时, 就打印不要删除的信息.
posted @ 2018-12-13 15:35  散尽浮华  阅读(1118)  评论(0编辑  收藏  举报