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() {
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
}
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
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
}
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
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
}
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
-
-