Select Git revision
testfile.sh
move_old_logs.sh 3.00 KiB
#!/usr/bin/env bash
# This script is intended to be run regularly and move logfiles from previous
# years to an archive directory. Logfiles from the current year are NOT moved,
# so they can be easily found.
START_YEAR="2015"
CURRENT_YEAR="$( date +%Y )"
PREVIOUS_YEAR="$(( CURRENT_YEAR - 1 ))"
[[ -n ${1} ]] && APP="${1}"
# shellcheck disable=SC2016
# ...because we don't want the expansion to happen in the log message
[[ (! ${APP} =~ "disapp") && (! ${APP} =~ "subapp") && (! ${APP} =~ "subapp_webservice") ]] && \
echo 'ERROR: $1 needs to be one of "disapp", "subapp" or "subapp_webservice"'
cd "/var/log/${APP}/" || exit 1
# Initially, this script will ALWAYS be called by root. This is because we have
# to switch users depending on the workflow that we're working with. Also, we
# HAVE to switch to a non-root user, because the NFS-share prohibits write
# operations from the root user.
# To solve this, we check if the script is run by root, and if it is, we run
# the script again with the correct user by calling `exec`.
# Once `exec` is called, the script terminates and is called again with the
# new user, which makes the UID check skip and executes the move operations
# below.
if [ $UID -eq 0 ]; then
if [[ "${APP}" == "disapp" ]]; then
exec su "access" "$0" "$@"
# nothing will be executed beyond that line,
# because exec replaces running process with the new one
fi
if [[ "${APP}" == "subapp" ]]; then
exec su "processing" "$0" "$@"
# nothing will be executed beyond that line,
# because exec replaces running process with the new one
fi
if [[ "${APP}" == "subapp_webservice" ]]; then
exec su "processing" "$0" "$@"
# nothing will be executed beyond that line,
# because exec replaces running process with the new one
fi
fi
# Execution resumes here, if we're a non-root user.
cd "/var/log/${APP}/" || exit 1
for YEAR in $( seq ${START_YEAR} ${PREVIOUS_YEAR} ); do
mkdir -p "old/${YEAR}";
# DISapp
if [[ -n $( find ./ -maxdepth 1 -name "disapp.log.${YEAR}-*.lz" ) ]]; then mv disapp.log.${YEAR}-*.lz "old/${YEAR}/"; fi
# SUBapp
if [[ -n $( find ./ -maxdepth 1 -name "Protokoll_SLUBArchiv_Erfolgreich-${YEAR}*.log" ) ]]; then mv Protokoll_SLUBArchiv_Erfolgreich-${YEAR}*.log "old/${YEAR}/"; fi
if [[ -n $( find ./ -maxdepth 1 -name "Protokoll_SLUBArchiv_FEHLER-${YEAR}*.log" ) ]]; then mv Protokoll_SLUBArchiv_FEHLER-${YEAR}*.log "old/${YEAR}/"; fi
if [[ -n $( find ./ -maxdepth 1 -name "sips.log.${YEAR}-*.lz" ) ]]; then mv sips.log.${YEAR}-*.lz "old/${YEAR}/"; fi
if [[ -n $( find ./ -maxdepth 1 -name "subapp.log.${YEAR}-*.lz" ) ]]; then mv subapp.log.${YEAR}-*.lz "old/${YEAR}/"; fi
if [[ -n $( find ./ -maxdepth 1 -name "producer_mails.log.${YEAR}-*.lz" ) ]]; then mv producer_mails.log.${YEAR}-*.lz "old/${YEAR}/"; fi
if [[ -n $( find ./ -maxdepth 1 -name "staff_mails.log.${YEAR}-*.lz" ) ]]; then mv staff_mails.log.${YEAR}-*.lz "old/${YEAR}/"; fi
# SUBapp Webservice
if [[ -n $( find ./ -maxdepth 1 -name "webservice.log.${YEAR}-*.lz" ) ]]; then mv webservice.log.${YEAR}-*.lz "old/${YEAR}/"; fi
done