From bea5ae340a43517efc3d1676efd4e260d8cee3ab Mon Sep 17 00:00:00 2001 From: Cryptoval Trading Technologies Date: Sat, 17 May 2025 02:19:12 +0000 Subject: [PATCH] inital --- archive_logs.sh | 83 +++++++++++++++++++++++++++++++++++++++++++ move_archives.sh | 91 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 174 insertions(+) create mode 100755 archive_logs.sh create mode 100755 move_archives.sh diff --git a/archive_logs.sh b/archive_logs.sh new file mode 100755 index 0000000..a9ce3b3 --- /dev/null +++ b/archive_logs.sh @@ -0,0 +1,83 @@ +#!/bin/bash + +function usage { + echo -n "Usage: ${0}" + echo -n " -L " + echo -n " [ -A (default /works/archive/logs)]" + echo -n " [-D (default: '2 days ago')]" + echo + exit 1 +} + +echo Starting $0 $* + +LogDir=${1} +LogArchiveDir=/works/archive/logs +DateCriteria="2 days ago" + + +# ---------------- cmdline +while getopts "A:L:D:" opt; do + case ${opt} in + A ) + LogArchiveDir=$OPTARG + ;; + L ) + LogDir=$OPTARG + ;; + D ) + DateCriteria=$OPTARG + ;; + \? ) + echo "Invalid option: -$OPTARG" >&2 + usage + ;; + : ) + echo "Option -$OPTARG requires an argument." >&2 + usage + ;; + esac +done +# ---------------- cmdline + + +if [ "${LogDir}" == "" ] +then + usage +fi + +Cmd="mkdir -p ${LogArchiveDir}" +echo ${Cmd} && eval ${Cmd} + +echo "Looking for log files older than '${DateCriteria}' in ${LogDir}" + +Oldest=$(date -d "${DateCriteria}" '+%Y-%m-%d %H:%M:%S') + +Cmd="find ${LogDir}/ '(' -name '*.log' -o -name '*.log.*' ')' -type f -not -newermt \"${Oldest}\"" +echo $Cmd + +files=$(eval ${Cmd}) +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} ${*} + diff --git a/move_archives.sh b/move_archives.sh new file mode 100755 index 0000000..8081984 --- /dev/null +++ b/move_archives.sh @@ -0,0 +1,91 @@ +#!/bin/bash +# FOR hosts with limited disk space - move to storage server + +function usage { + echo -n "Usage: ${0}" + echo -n " -H " + echo -n " [ -A (default /works/archive)]" + echo -n " [-D (default: '2 days ago')]" + echo + exit 1 +} + +echo Starting $0 $* + +# ---- D e f a u l t s +ArchiveDir=/works/archive +DateCriteria="2 days ago" +FromHost=$(hostname -s) +# ---- D e f a u l t s + +# ---------------- cmdline +while getopts "A:H:D:" opt; do + case ${opt} in + A ) + ArchiveDir=$OPTARG + ;; + H ) + FromHost=$OPTARG + ;; + D ) + DateCriteria=$OPTARG + ;; + \? ) + echo "Invalid option: -$OPTARG" >&2 + usage + ;; + : ) + echo "Option -$OPTARG requires an argument." >&2 + usage + ;; + esac +done +# ---------------- cmdline + + +if [ "${FromHost}" == "" ] +then + usage +fi + +TargetHost=cloud21.cvtt.vpn +TargetRootDir=/opt/store/cvtt/archive + +Oldest=$(date -d "${DateCriteria}" '+%Y-%m-%d %H:%M:%S') + +echo "Looking for log files older than ${DateCriteria} in ${ArchiveDir}" +Cmd="find ${ArchiveDir}/" +Cmd="${Cmd} '('" +Cmd="${Cmd} -name '*.log'" +Cmd="${Cmd} -o -name '*.log.*'" +Cmd="${Cmd} -o -name '*.logs.*'" +Cmd="${Cmd} -o -name '*.tgz'" +Cmd="${Cmd} ')'" +Cmd="${Cmd} -type f" +Cmd="${Cmd} -not -newermt \"${Oldest}\"" + +echo ${Cmd} +files=$(eval ${Cmd}) + +if [ "$files" == "" ] +then + echo "No files found older than ${Oldest} in ${ArchiveDir}" +else + Target="${TargetHost}:${TargetRootDir}/${FromHost}/" + echo "Moving files to ${Target}:" + echo ----------------- + for f in ${files} + do + echo ${f} + done + + Cmd="rsync -ahvv" + Cmd="${Cmd} --remove-source-files" + Cmd="${Cmd} $files" + Cmd="${Cmd} ${Target}" + echo ${Cmd} +# exit + eval ${Cmd} +fi +echo Done ${0} ${*} +