From da2de96c9c1332f8ec97ba4103a5fb3660673473 Mon Sep 17 00:00:00 2001 From: Peter Ajamian Date: Sat, 18 Dec 2021 17:12:11 +1300 Subject: [PATCH] Add log rotation. --- migrate2rocky/migrate2rocky.sh | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/migrate2rocky/migrate2rocky.sh b/migrate2rocky/migrate2rocky.sh index 6e01108..3b139ab 100644 --- a/migrate2rocky/migrate2rocky.sh +++ b/migrate2rocky/migrate2rocky.sh @@ -55,6 +55,26 @@ fi # Path to logfile logfile=/var/log/migrate2rocky.log +# Rotate old logs +numlogs=5 +if [[ -e $logfile ]]; then + # Here we use mv before bin_check, so simply check the exit status to see if + # it worked. + if ! mv -f "$logfile" "$logfile.0"; then + printf '%s\n' "Unable to rotate logfiles, continuing without rotation." >&2 + else + for ((i=numlogs;i>0;i--)); do + if [[ -e "$logfile.$((i-1))" ]]; then + if ! mv -f "$logfile.$((i-1))" "$logfile.$i"; then + printf '%s\n' \ +"Unable to rotate logfiles, continuing without rotation." + break + fi + fi + done + fi +fi + # Send all output to the logfile as well as stdout. # After the following we get: # Output to 1 goes to stdout and the logfile. @@ -62,7 +82,7 @@ logfile=/var/log/migrate2rocky.log # Output to 3 just goes to stdout. # Output to 4 just goes to stderr. # Output to 5 just goes to the logfile. -truncate -s0 "$logfile" + # shellcheck disable=SC2094 exec \ 3>&1 \ @@ -106,6 +126,8 @@ errmsg () { printf '%s%s%s' "$errcolor" "$msg" "$nocolor" >&4 } +infomsg 'migrate2rocky - Begin logging at %(%c)T.\n\n' -1 + export LC_ALL=C.UTF-8 unset LANGUAGE shopt -s nullglob