ppeeou / tip

1 stars 1 forks source link

[Bash] Date #3

Open ppeeou opened 5 years ago

ppeeou commented 5 years ago
TIMESTAMP=`TZ="Asia/Seoul" date +"%Y-%m-%d-%H_%M"`
$TIMESTAMP 
2019-05-08-17_11
ppeeou commented 5 years ago

cron

#!/bin/bash
### centos 7
# yum -y install cronie                    ## 다운로드 
# systemctl start crond                    ## 크론탭 시작

# /sbin/chkconfig --levels 235 crond on  ## 서버 재시작 할 때 
## log format YYYY-MM-DD.log remove

# INPUT
INPUT_DATE=`TZ="Asia/Seoul" date +"%Y-%m-%d"`
SUBTRACT_DAY=5
LOG_PATH=/app

# COMPARE DATE
COMPARE_DATE=$(date --date="${INPUT_DATE} -${SUBTRACT_DAY}day" +%Y-%m-%d)
DATAS=$(find $LOG_PATH/*.log )

# COMPARE
for DATA in ${DATAS[@]}
do 
  echo "$DATA $COMPARE_DATE $INPUT_DATE"
  if [ "$(echo "$DATA" | tr -d $LOG_PATH.log )" \< $COMPARE_DATE ];
  then
    rm -rf $DATA
  fi
done
ppeeou commented 5 years ago

ex)

nginx custom logrotate

#!/usr/bin/bash
INPUT_DATE=`TZ="Asia/Seoul" date +"%Y-%m-%d"`
### backup
mv access.log access."$INPUT_DATE".log
mv error.log  error."$INPUT_DATE".log

kill -USR1 `cat nginx.pid`
sleep 1

gzip access."$INPUT_DATE".log
gzip error."$INPUT_DATE".log

#### cron remove logdata
SUBTRACT_DAY=3
LOG_PATH=$PWD

# COMPARE DATE
COMPARE_DATE=$(date --date "${INPUT_DATE} -${SUBTRACT_DAY}day" +%Y-%m-%d)
DATAS=$(find $LOG_PATH/*.gz )

echo "----------------------------------"
echo "COMPARE DATE"
echo $COMPARE_DATE
echo "----------------------------------"

# echo $DATAS
for DATA in ${DATAS[@]}
do
  echo "$DATA $COMPARE_DATE $INPUT_DATE" 
  if [ "$(echo "$DATA" | tr -d $LOG_PATH.log )" \< $COMPARE_DATE ];
  then
    echo "------------------------------------------"
    echo "access"
    rm -rf $DATA
    echo "------------------------------------------"
  fi

  if [ "$(echo "$DATA" | tr -d "$LOG_PATH"error.gz )" \< $COMPARE_DATE ];
  then
    echo "------------------------------------------"
    echo "error"
    rm -rf $DATA
    echo "------------------------------------------"
  fi
done