From: Frank Brehm Date: Mon, 18 Mar 2019 16:51:26 +0000 (+0100) Subject: Adding cleanup-loghosts-hosts X-Git-Tag: 0.4.1~18 X-Git-Url: https://git.uhu-banane.net/?a=commitdiff_plain;h=8c74671680754646b8d462564330a71d92ba3d72;p=pixelpark%2Fpp-admin-tools.git Adding cleanup-loghosts-hosts --- diff --git a/sbin/cleanup-loghosts-hosts b/sbin/cleanup-loghosts-hosts new file mode 100755 index 0000000..1b33c9c --- /dev/null +++ b/sbin/cleanup-loghosts-hosts @@ -0,0 +1,207 @@ +#!/usr/bin/bash + +HOSTS_DIR=/opt/log/HOSTS + +if [[ ! -d "${HOSTS_DIR}" ]] ; then + echo "Logging directory for hosts '${HOSTS_DIR}' not found." >&2 + exit 5 +fi + +CUR_YEAR=$( date +'%Y' ) +CUR_MONTH=$( date +'%m' ) + +#------------------------------------------------------------------------------ +perform_day() { + + local ddir="$1" + local this_day=$( basename "${ddir}" ) + local cmd= + + if [[ "${this_day}" =~ ^[0-3][0-9]$ ]] ; then + : + else + return + fi + + echo " * day: ${this_day}" + + local empty="y" + local entry= + for entry in $( ls -1d "${ddir}"/* "${ddir}"/.[^.]* 2>/dev/null ) ; do + if [[ -e "${entry}" ]] ; then + empty="n" + echo " Found '${entry}'." + break + fi + done + + if [[ "${empty}" == "y" ]] ; then + echo " Removing ..." + cmd="rmdir \"${ddir}\"" + echo " ${cmd}" + eval ${cmd} + else + echo " Day not empty" + fi + +} + +#------------------------------------------------------------------------------ +perform_month() { + + local mdir="$1" + local this_month=$( basename "${mdir}" ) + local ddir= + local cmd= + + if [[ "${this_month}" =~ ^[0-1][0-9]$ ]] ; then + : + else + return + fi + + echo " * month: ${this_month}" + + for ddir in $( ls -1d "${mdir}"/* ) ; do + if [[ ! -d "${ddir}" ]] ; then + continue + fi + perform_day "${ddir}" + done + + local empty="y" + local entry= + for entry in $( ls -1d "${mdir}"/* "${mdir}"/.[^.]* 2>/dev/null ) ; do + if [[ -e "${entry}" ]] ; then + empty="n" + echo " Found '${entry}'." + break + fi + done + + if [[ "${empty}" == "y" ]] ; then + echo " Removing ..." + cmd="rmdir \"${mdir}\"" + echo " ${cmd}" + eval ${cmd} + else + echo " Month not empty" + fi + +} + +#------------------------------------------------------------------------------ +perform_year() { + + local ydir="$1" + local this_year=$( basename "${ydir}" ) + local mdir= + local cmd= + + if [[ "${this_year}" =~ ^[12][0-9][0-9][0-9]$ ]] ; then + : + else + return + fi + + echo " * year: ${this_year}" + + for mdir in $( ls -1d "${ydir}"/* ) ; do + if [[ ! -d "${mdir}" ]] ; then + continue + fi + perform_month "${mdir}" + done + + local empty="y" + local entry= + for entry in $( ls -1d "${ydir}"/* "${ydir}"/.[^.]* 2>/dev/null ) ; do + if [[ -e "${entry}" ]] ; then + empty="n" + echo " Found '${entry}'." + break + fi + done + + if [[ "${empty}" == "y" ]] ; then + echo " Removing ..." + cmd="rmdir \"${ydir}\"" + echo " ${cmd}" + eval ${cmd} + else + echo " Year not empty" + fi + +} + +#------------------------------------------------------------------------------ +perform_host() { + + local hdir="$1" + local this_host=$( basename "${hdir}" ) + local ydir= + local cmd= + + echo + echo "Performing host '${this_host}' ..." + + for ydir in $( ls -1d "${hdir}"/* ) ; do + if [[ ! -d "${ydir}" ]] ; then + continue + fi + perform_year "${ydir}" + done + + local empty="y" + local entry= + for entry in $( ls -1d "${hdir}"/* "${hdir}"/.[^.]* 2>/dev/null ) ; do + if [[ -e "${entry}" ]] ; then + empty="n" + echo " Found '${entry}'." + break + fi + done + + if [[ "${empty}" == "y" ]] ; then + echo " Removing ..." + cmd="rmdir \"${hdir}\"" + echo " ${cmd}" + eval ${cmd} + else + echo " Host not empty" + fi + +} + +#------------------------------------------------------------------------------ +do_cleanup() { + + local dir= + local oifs="${IFS}" + IFS=" +" + + for dir in $( ls -1d "${HOSTS_DIR}"/* ) ; do + if [[ ! -d "${dir}" ]] ; then + continue + fi + perform_host "${dir}" + done + +} + + +#------------------------------------------------------------------------------ +main() { + + do_cleanup +} + +main "$@" + + +exit 0 + +# vim: et ts=4 list + +