This commit is contained in:
Oleg Sheynin 2023-04-23 13:58:31 -04:00
parent 593066e773
commit 2718b23474
2 changed files with 52 additions and 1 deletions

View File

@ -1 +1 @@
0.0.1
0.0.2

51
utils/archive_logs.sh Executable file
View File

@ -0,0 +1,51 @@
#!/bin/bash
function usage {
echo "Usage: ${0} <log directory>"
exit 1
}
LogDir=${1}
if [ "${LogDir}" == "" ]
then
usage
fi
echo Starting $0 $*
LogArchiveDir=/home/cvtt/prod/archive/logs
# DateCriteria="week ago"
DateCriteria="2 days ago"
Oldest=$(date -d "${DateCriteria}" '+%Y-%m-%d')
Now=$(date '+%Y%m%d_%H%M%S')
echo "Looking for log files older than ${DateCriteria} in ${LogDir}"
mkdir -p ${LogArchiveDir}
echo "find ${LogDir}/ '(' -name '*.log' -o -name '*.log.*' ')' -type f -not -newermt ${Oldest})"
files=$(find ${LogDir}/ '(' -name '*.log' -o -name '*.log.*' ')' -type f -not -newermt ${Oldest})
if [ "$files" == "" ]
then
echo "No files found older than ${Oldest} in ${LogDir}"
else
echo Archiving files:
echo -----------------
echo ${files}
oldest_file=$(ls -t ${files} | tail -1)
youngest_file=$(ls -tr ${files} | tail -1)
tstmp1=$(stat --printf '%y' ${oldest_file})
tstmp2=$(stat --printf '%y' ${youngest_file})
tmsig="$(date -d "${tstmp1}" +"%Y%m%d_%H%M%S")-$(date -d "${tstmp2}" +"%Y%m%d_%H%M%S")"
echo $tmsig
Cmd="tar zcvf ${LogArchiveDir}/${tmsig}.logs.tgz ${files}"
echo $Cmd
eval $Cmd
rm $files
fi
echo Done ${0} ${*}