]> Frank Brehm's Git Trees - pixelpark/pp-admin-tools.git/commitdiff
Adding compressing old files, changing output
authorFrank Brehm <frank.brehm@pixelpark.com>
Wed, 20 Mar 2019 09:28:12 +0000 (10:28 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Wed, 20 Mar 2019 09:28:12 +0000 (10:28 +0100)
sbin/cleanup-loghosts-hosts

index 1b33c9c4aa3d653239aa1f3d37c7de4b20d28dfd..de09e85948691f4f0c567570b480f826378a0ba8 100755 (executable)
@@ -10,6 +10,21 @@ fi
 CUR_YEAR=$( date +'%Y' )
 CUR_MONTH=$( date +'%m' )
 
+REMOVED_FILES=0
+REMOVED_DIRS=0
+TOTAL_DIRS=0
+COMPRESSED_FILES=0
+
+MAX_AGE=210
+MIN_AGE_COMPRESS=3
+
+#------------------------------------------------------------------------------
+my_date() {
+
+    date +'%Y-%m-%d %H:%M:%S %Z'
+
+}
+
 #------------------------------------------------------------------------------
 perform_day() {
 
@@ -17,31 +32,21 @@ perform_day() {
     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
 
+    TOTAL_DIRS=$(( $TOTAL_DIRS + 1 ))
     if [[ "${empty}" == "y" ]] ; then
-        echo "               Removing ..."
         cmd="rmdir \"${ddir}\""
-        echo "               ${cmd}"
+        echo "           ${cmd}"
         eval ${cmd}
-    else
-        echo "               Day not empty"
+        REMOVED_DIRS=$(( $REMOVED_DIRS + 1 ))
     fi
 
 }
@@ -54,12 +59,6 @@ perform_month() {
     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
@@ -74,18 +73,16 @@ perform_month() {
     for entry in $( ls -1d "${mdir}"/* "${mdir}"/.[^.]* 2>/dev/null ) ; do
         if [[ -e "${entry}" ]] ; then
             empty="n"
-            echo "           Found '${entry}'."
             break
         fi
     done
 
+    TOTAL_DIRS=$(( $TOTAL_DIRS + 1 ))
     if [[ "${empty}" == "y" ]] ; then
-        echo "           Removing ..."
         cmd="rmdir \"${mdir}\""
         echo "           ${cmd}"
         eval ${cmd}
-    else
-        echo "           Month not empty"
+        REMOVED_DIRS=$(( $REMOVED_DIRS + 1 ))
     fi
 
 }
@@ -98,12 +95,6 @@ perform_year() {
     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
@@ -118,18 +109,16 @@ perform_year() {
     for entry in $( ls -1d "${ydir}"/* "${ydir}"/.[^.]* 2>/dev/null ) ; do
         if [[ -e "${entry}" ]] ; then
             empty="n"
-            echo "        Found '${entry}'."
             break
         fi
     done
 
+    TOTAL_DIRS=$(( $TOTAL_DIRS + 1 ))
     if [[ "${empty}" == "y" ]] ; then
-        echo "        Removing ..."
         cmd="rmdir \"${ydir}\""
         echo "        ${cmd}"
         eval ${cmd}
-    else
-        echo "        Year not empty"
+        REMOVED_DIRS=$(( $REMOVED_DIRS + 1 ))
     fi
 
 }
@@ -157,51 +146,104 @@ perform_host() {
     for entry in $( ls -1d "${hdir}"/* "${hdir}"/.[^.]* 2>/dev/null ) ; do
         if [[ -e "${entry}" ]] ; then
             empty="n"
-            echo "  Found '${entry}'."
             break
         fi
     done
 
+    TOTAL_DIRS=$(( $TOTAL_DIRS + 1 ))
     if [[ "${empty}" == "y" ]] ; then
-        echo "  Removing ..."
         cmd="rmdir \"${hdir}\""
         echo "  ${cmd}"
         eval ${cmd}
-    else
-        echo "  Host not empty"
+        REMOVED_DIRS=$(( $REMOVED_DIRS + 1 ))
     fi
 
+    sleep 1
+
 }
 
 #------------------------------------------------------------------------------
-do_cleanup() {
+remove_old_files() {
 
-    local dir=
-    local oifs="${IFS}"
-    IFS="
-"
+    echo
+    echo "[$( my_date )]: Removing files older than ${MAX_AGE} days:"
+    for fname in $( find "${HOSTS_DIR}" -type f -mtime +${MAX_AGE} ) ; do
+        if [[ -f "${fname}" ]] ; then
+            echo "  * '${fname}'"
+            rm "${fname}"
+            REMOVED_FILES=$(( $REMOVED_FILES + 1 ))
+        fi
+    done
+    echo
+    echo "-----------------------------------------------------------------"
+    echo "[$( my_date )]: Finished removing old files."
+    echo "             Removed $REMOVED_FILES files."
+    echo "-----------------------------------------------------------------"
+    sleep 2
+
+}
+
+#------------------------------------------------------------------------------
+do_cleanup_dirs() {
 
+    echo
+    echo "[$( my_date )]: Removing empty directories:"
     for dir in $( ls -1d "${HOSTS_DIR}"/* ) ; do
         if [[ ! -d "${dir}" ]] ; then
             continue
         fi
         perform_host "${dir}"
     done
+    echo
+    echo "-----------------------------------------------------------------"
+    echo "[$( my_date )]: Finished removing empty directories."
+    echo "             Removed $REMOVED_DIRS of $TOTAL_DIRS directories."
+    echo "-----------------------------------------------------------------"
+    sleep 2
 
 }
 
+#------------------------------------------------------------------------------
+compress_old_files() {
+
+    echo
+    echo "[$( my_date )]: Compressing files older than ${MIN_AGE_COMPRESS} days:"
+    for fname in $( find "${HOSTS_DIR}" -type f -mtime +${MIN_AGE_COMPRESS} | \
+                        grep -vi '\.gz$' | head -n 1000 ) ; do
+        if [[ -f "${fname}" ]] ; then
+            echo "  * '${fname}'"
+            ls -l "${fname}"
+            gzip -v9 "${fname}"
+            ls -l "${fname}"*
+            COMPRESSED_FILES=$(( $COMPRESSED_FILES + 1 ))
+        fi
+    done
+    echo
+    echo "-----------------------------------------------------------------"
+    echo "[$( my_date )]: Finished compressing old files."
+    echo "             Compressed $COMPRESSED_FILES files."
+    echo "-----------------------------------------------------------------"
+
+}
 
 #------------------------------------------------------------------------------
 main() {
 
-    do_cleanup
-}
+    local dir=
+    local fname=
+    local oifs="${IFS}"
+    IFS="
+"
 
-main "$@"
+    remove_old_files
+    do_cleanup_dirs
+    compress_old_files
 
+    echo
+
+}
 
+main "$@"
 exit 0
 
 # vim: et ts=4 list
-
-