Skip to content

Latest commit

 

History

History
57 lines (47 loc) · 1023 Bytes

定时清理日志脚本.md

File metadata and controls

57 lines (47 loc) · 1023 Bytes

定时清理日志脚本

clean-log.sh 文件内容如下:

#!/bin/bash

LOG_DIR=/home/work/log
log_date=`date -d '-1 day' '+%Y%m%d'`

logs=(
  "avatar"
  "profile"
)

for log in ${logs[@]}
do
  file=$LOG_DIR/$log/*-$log_date.log
  bak=$LOG_DIR/$log/bak
  if [ ! -d $bak ]; then
    mkdir -p $bak
  fi
  mv $file $bak
done

使用:需要在定时任务配置该脚本,比如每天凌晨两点半备份昨天的日志:

30 2 * * * /bin/bash /home/work/script/clean-log.sh

效果如下:

# 当前目录
[work@bogon ~]$ tree log
log
├── avatar
│   ├── api-20210902.log
│   └── api-20210903.log
└── profile
    ├── api-20210901.log
    └── api-20210902.log

# 日志备份之后的目录
[work@bogon ~]$ tree log
log
├── avatar
│   ├── api-20210903.log
│   └── bak
│       └── api-20210902.log
└── profile
    ├── api-20210901.log
    └── bak
        └── api-20210902.log