Last active
January 16, 2022 08:46
-
-
Save rojenzaman/71257efd6021a5722c0e50be601f27d7 to your computer and use it in GitHub Desktop.
Bash script to merge NGINX logrotate files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
print_cron_rules() { echo "ClNFVCBDUk9OVEFCOgoKQGhvdXJseSAvdXNyL2Jpbi9tZXJnZV9uZ2lueF9sb2dyb3RhdGVkLnNoIC92YXIvbG9nL25naW54IGhvdXIKQGRhaWx5IC91c3IvYmluL21lcmdlX25naW54X2xvZ3JvdGF0ZWQuc2ggL3Zhci9sb2cvbmdpbnggZGF5CkB3ZWVrbHkgL3Vzci9iaW4vbWVyZ2VfbmdpbnhfbG9ncm90YXRlZC5zaCAvdmFyL2xvZy9uZ2lueCB3ZWVrCkBtb250aGx5IC91c3IvYmluL21lcmdlX25naW54X2xvZ3JvdGF0ZWQuc2ggL3Zhci9sb2cvbmdpbnggbW9udGgKCg==" | base64 -d; } | |
usage() { echo "Usage: ${BASH_SOURCE[0]} [DIR] [ hour | day | week | month ] <error>"; print_cron_rules; exit 1; } ; if [[ "$#" -lt 1 ]]; then usage; fi | |
DIR="$(realpath "${1}")" | |
TIME="${2:-hour}" | |
ERROR_LOG="${3:-false}" | |
ZSTD_LEVEL="19" | |
MERGE_ROTATED_LOCKFILE="/tmp/MERGE_ROTATED_IS_RUNNING.${TIME}.lock" | |
hour() { date +hour.%d-%m-%Y_%Hh%Mm%Ss.%A ; } | |
day() { date +day.%d-%m-%Y_%Hh%Mm.%A ; } | |
week() { date +week.%U.%d-%m-%Y_%Hh%Mm ; } | |
month() { date +month.%d-%m-%Y_%Hh%Mm.%B ; } | |
TIMESTAMP="$(${TIME})"; DATE_TIME="$(date -u "+%Y-%m-%d %H:%M:%S %Z")"; ME="$(basename "$0")" | |
# exit_with_failure() outputs a message before exiting the script. | |
exit_with_failure() { echo; echo "FAILURE: $1"; echo; del_lock; exit 1; } | |
# command_exists() tells if a given command exists. | check_command() check if command exists and exit if not exists | |
command_exists() { command -v "$1" >/dev/null 2>&1; }; check_command() { if ! command_exists "$1"; then exit_with_failure "Command '$1' not found"; fi; } | |
# set_lock() sets lock file | |
set_lock() { if ! echo "$DATE_TIME" > "$MERGE_ROTATED_LOCKFILE"; then exit_with_failure "Can not create lock file '$MERGE_ROTATED_LOCKFILE'"; fi; } | |
# del_lock() delets lock file | |
del_lock() { rm -f "$MERGE_ROTATED_LOCKFILE"; } | |
# check_lock() checks lock file and exit if the file exists | |
check_lock() { if [[ -f "$MERGE_ROTATED_LOCKFILE" ]]; then exit_with_failure "$ME is already running. Please wait... In case of problems simply delete the file: '$MERGE_ROTATED_LOCKFILE'"; fi; } | |
# MAIN | |
check_lock | |
set_lock | |
check_command zstd; check_command zcat | |
if [[ ! -d "${DIR}/merged" ]]; then | |
exit_with_failure "'${DIR}/merged' directory not found, please create it" | |
fi | |
if [[ "${TIME}" == "hour" ]]; then | |
mkdir -p "${DIR}/merged/hour" | |
zcat $(ls -1rt ${DIR}/access.log.*.gz) | zstd -f -${ZSTD_LEVEL} -o "${DIR}/merged/hour/${TIMESTAMP}.zst" | |
rm $(ls -1rt ${DIR}/access.log.*.gz) | |
if [[ "${ERROR_LOG}" == "error" ]]; then | |
zcat $(ls -1rt ${DIR}/error.log.*.gz) | zstd -f -${ZSTD_LEVEL} -o "${DIR}/merged/hour/error.${TIMESTAMP}.zst" | |
rm $(ls -1rt ${DIR}/error.log.*.gz) | |
fi | |
fi | |
if [[ "${TIME}" == "day" ]]; then | |
mkdir -p "${DIR}/merged/day" | |
zstdcat $(ls -1rt ${DIR}/merged/hour/hour.*.zst) | zstd -f -${ZSTD_LEVEL} -o "${DIR}/merged/day/${TIMESTAMP}.zst" | |
rm $(ls -1rt ${DIR}/merged/hour/hour.*.zst) | |
if [[ "${ERROR_LOG}" == "error" ]]; then | |
zstdcat $(ls -1rt ${DIR}/merged/hour/error.hour.*.zst) | zstd -f -${ZSTD_LEVEL} -o "${DIR}/merged/day/error.${TIMESTAMP}.zst" | |
rm $(ls -1rt ${DIR}/merged/hour/error.hour.*.zst) | |
fi | |
fi | |
if [[ "${TIME}" == "week" ]]; then | |
mkdir -p "${DIR}/merged/week" | |
zstdcat $(ls -1rt ${DIR}/merged/day/day.*.zst) | zstd -f -${ZSTD_LEVEL} -o "${DIR}/merged/week/${TIMESTAMP}.zst" | |
rm $(ls -1rt ${DIR}/merged/day/day.*.zst) | |
if [[ "${ERROR_LOG}" == "error" ]]; then | |
zstdcat $(ls -1rt ${DIR}/merged/day/error.day.*.zst) | zstd -f -${ZSTD_LEVEL} -o "${DIR}/merged/week/error.${TIMESTAMP}.zst" | |
rm $(ls -1rt ${DIR}/merged/day/error.day.*.zst) | |
fi | |
fi | |
if [[ "${TIME}" == "month" ]]; then | |
mkdir -p "${DIR}/merged/month" | |
zstdcat $(ls -1rt ${DIR}/merged/week/week.*.zst) | zstd -f -${ZSTD_LEVEL} -o "${DIR}/merged/month/${TIMESTAMP}.zst" | |
rm $(ls -1rt ${DIR}/merged/week/week.*.zst) | |
if [[ "${ERROR_LOG}" == "error" ]]; then | |
zstdcat $(ls -1rt ${DIR}/merged/week/error.week.*.zst) | zstd -f -${ZSTD_LEVEL} -o "${DIR}/merged/month/error.${TIMESTAMP}.zst" | |
rm $(ls -1rt ${DIR}/merged/week/error.week.*.zst) | |
fi | |
fi | |
del_lock |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment