system-configs/opt/sbin/cronjob-rotate-logs-symlinks

45 lines
1.2 KiB
Bash
Executable file

#!/bin/bash
# Default configuration.
LOGS_DIR="/var/log"
DIR_MODE="0750"
UMASK="027"
# Allow /etc/default/rotate-logs-symlinks to override default configuration.
[[ -e /etc/default/rotate-logs-symlinks ]] && {
# shellcheck disable=SC1091
source /etc/default/rotate-logs-symlinks || {
printf "%s: %s\\n" "${0##*/}" "failed reading /etc/default/rotate-logs-symlinks" >&2
exit 1
}
}
# Process the directories in the logs directory.
[[ -d "$LOGS_DIR" ]] && {
TODAY="$(printf "%(%Y/%m/%d)T")"
umask "$UMASK"
# Process all the directories in the logs directory.
for DIR in "$LOGS_DIR"/*; do
cd "$DIR" 2>/dev/null || {
printf "%s: %s\\n" "${0##*/}" "failed to change directory to '$DIR'" >&2
continue
}
# Create a new logs directory for today.
# shellcheck disable=SC2174
mkdir -p -m "$DIR_MODE" "$TODAY" 2>/dev/null || {
printf "%s: %s\\n" "${0##*/}" "failed to create directory '$DIR/$TODAY'" >&2
continue
}
# Create a 'today' symlink to the new days' directory.
( cd "$DIR" 2>/dev/null && ln -sf "$TODAY" "today" 2>/dev/null ) || {
printf "%s: %s\\n" "${0##*/}" "creating 'today' symlink failed" >&2
continue
}
done
}
exit 0