ops/utils/move_archives.sh
2024-01-13 15:10:38 -05:00

73 lines
1.3 KiB
Bash
Executable File

#!/bin/bash
# FOR cloud hosts with limited disk space - move to storage server
function usage {
echo "Usage: ${0} <host> <from_dir> <days>"
exit 1
}
echo Starting $0 $*
FromHost=$(hostname -s)
if [ "${FromHost}" == "" ]
then
usage
fi
ArchiveDir=${2}
if [ "${ArchiveDir}" == "" ]
then
usage
fi
Days=${3}
if [ "${Days}" == "" ]
then
Days=2
fi
DateCriteria="${Days} days ago"
TargeHost=cloudstore.cvtt.vpn
Oldest=$(date -d "${DateCriteria}" '+%Y-%m-%d')
Now=$(date '+%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 ${LogDir}"
else
Target="${TargetHost}:/home/cvtt/Archive/${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} ${*}