From 42782597cd47fe71f9ac0bab4d6bc0c99c949596 Mon Sep 17 00:00:00 2001 From: Matan Horovitz Date: Sun, 16 Oct 2022 22:11:19 +0300 Subject: [PATCH] Logging format updates - hurray! --- HatarashiHako | 30 +++++++++++++-------------- Kumonoboru | 46 +++++++++++++++++++++++++----------------- Okiru | 56 +++++++++++++++++++++++++++++++++++++-------------- 3 files changed, 84 insertions(+), 48 deletions(-) diff --git a/HatarashiHako b/HatarashiHako index cca39e5..568363c 100755 --- a/HatarashiHako +++ b/HatarashiHako @@ -25,7 +25,7 @@ CONTAINER_PATHS=$(find $CONTAINER_DIR -maxdepth 2 -type f -name docker-compose.y #Find containers in ^ base dir ^ in base container path ^ by finding compose files ^ (not here) ^ and getting their directory name. for container_path in ${CONTAINER_PATHS[@]}; do cd $container_path - debug "Working on container directory $container_path" + debug "Working on container directory" "$container_path" container_stack=$(basename $container_path) info "Working on stack" "$container_stack" container_images="$(cat $container_path/docker-compose.yml | grep -E "image: ([a-z]+)/([a-z]+)(:[a-z0-9].*$)?" | grep -Ev 'postgresql|mariadb' | awk '{print $2}')" @@ -33,16 +33,16 @@ for container_path in ${CONTAINER_PATHS[@]}; do for container_image in $container_images; do container_name="$(echo $container_image | awk -F/ '{print $2}' | sed "s/\:.*//")" # remove everything after the : ^ - debug "$container_stack has image $container_image" + debug "$container_stack has image" "$container_image" if [[ -n $(echo $container_image | grep -E "(.*:[a-z0-9].*$)") ]]; then # check if there is a :tag present ^ image_tag=":$(echo $container_image | awk -F: '{print $NF}')" # !! Add : ^ before image !! so it is only added to later commands if there is an image at all - debug "$container_image has tag $image_tag" + debug "$container_image has tag" "$image_tag" export container_image=$(echo $container_image | awk -F: '{print $1}') # If the container does have a tag, keep the base name ^ without it (before the :) fi - debug "Fetching local image checksum with: docker inspect \"$container_image$image_tag\" | grep -Eo \"$container_image@sha256:([0-9a-zA-Z].*)(\")\" | sed -e 's/\"//g' | awk -F@ '{print \$2}" + debug "Fetching local image checksum with:" "docker inspect \"$container_image$image_tag\" | grep -Eo \"$container_image@sha256:([0-9a-zA-Z].*)(\")\" | sed -e 's/\"//g' | awk -F@ '{print \$2}" local_image=$(docker inspect "$container_image$image_tag" | grep -Eo "$container_image@sha256:([0-9a-zA-Z].*)(\")" | sed -e 's/"//g' -e 's/\s+//g' | awk -F@ '{print $2}') # remember, this bit ^ is empty without an image ^ this is the main image checksum remove ^ " and whitespace and^ get the checksum after the @ if [[ -z $local_image ]]; then @@ -51,9 +51,9 @@ for container_path in ${CONTAINER_PATHS[@]}; do FAILED_CONTAINERS+=("(local) $container_name") continue 2 else - debug "Local SHA256 for $container_image is $local_image" + debug "Local SHA256 for $container_image is" "$local_image" fi - debug "Fetching remote image with: skopeo inspect --creds \"dkd6:Vulthuryol569\" docker://docker.io/$container_image$image_tag | grep Digest | head -1 | grep -Eo 'sha256:([0-9a-zA-Z].*)(\")' | sed -e 's/\"//g'" + debug "Fetching remote image with:" "skopeo inspect --creds \"dkd6:Vulthuryol569\" docker://docker.io/$container_image$image_tag | grep Digest | head -1 | grep -Eo 'sha256:([0-9a-zA-Z].*)(\")' | sed -e 's/\"//g'" #Use Skopeo, a Red Hat tool, with my Docker Hub account to register the remote image checksum remote_image=$(skopeo inspect --creds "dkd6:Vulthuryol569" docker://docker.io/$container_image$image_tag | grep Digest | head -1 | grep -Eo 'sha256:([0-9a-zA-Z].*)(")' | sed -e 's/"//g' -e 's/\s+//g' ) #Sometimes; Docker hub hangs up; try again if you failed @@ -62,32 +62,32 @@ for container_path in ${CONTAINER_PATHS[@]}; do fi #Now, if you still don't have an image after the second try, something's fuckey. if [[ -z $remote_image ]]; then - error "Error fetching remote image checksum for container $container_name!" + error "Error fetching remote image checksum for container" "$container_name!" FAILED_CONTAINERS+=("(remote) $container_name") continue 2 else - debug "Remote SHA256 for $container_image is $remote_image" + debug "Remote SHA256 for $container_image is" "$remote_image" fi #If we have both checksums, compare them; they should be identical, or the container is outdated. if [[ -n $local_image ]] && [[ -n $remote_image ]] && [[ "$local_image" =~ "$remote_image" ]]; then - ok "$container_name is up to date!" + ok "$container_name" "is up to date!" else - warn "$container_name is out of date!" - info "Attempting to update $container_name" + warn "$container_name" "is out of date!" + info "Attempting to update" "$container_name" if docker-compose pull $container_name; then - info "Pulled latest image for $container_name succesfully. Restarting..." + info "Pulled latest image for" "$container_name" if docker-compose up -d --remove-orphans; then - ok "$container_stack has been updated sucessfully!" + ok "$container_stack" "has been updated sucessfully!" UPDATED_CONTAINERS+=("($container_stack) $container_name") else - error "Failed to update $container_name!" + error "Failed to update" "$container_name!" FAILED_CONTAINERS+=("($container_stack|update) $container_name") #Add to array for mail report OUTDATED_CONTAINERS+=("($container_stack) $container_name") export OUTDATED_COUNT=$(($OUTDATED_COUNT+1)) fi else - error "Failed to pull image for $container_name!" + error "Failed to pull image for" "$container_name!" FAILED_CONTAINERS+=("($container_stack|pull) $container_name") #Add to array for mail report OUTDATED_CONTAINERS+=("($container_stack) $container_name") diff --git a/Kumonoboru b/Kumonoboru index 5425542..0a4d529 100755 --- a/Kumonoboru +++ b/Kumonoboru @@ -1,7 +1,6 @@ #!/bin/bash ##Simple script to run Restic backups source /root/.bash_profile -source /home/shmick/Scripts/Okiru source /etc/environment export HOME=/root/ arg0=$(basename "$0") @@ -26,6 +25,7 @@ help() echo " {-c|--clean} -- Force prune of the remote repositories" echo " {-r|--repository} repository -- Only backup the specified repository." echo " {-l|--limit} #[Kbps] -- Limit upload & download speed" + echo " {-v|--verbose} -- Print debug messages" echo " {-h|--help} -- Print this help message and exit" echo "Available repositories:" echo "Gerbil-TK Photos (path: /var/Red-Vol/Media/Pictures)" @@ -44,18 +44,19 @@ flags() do case "$1" in (-c|--clean) - debug "Cleaning will take place per request." export CLEAN="1" shift;; (-r|--repository) shift export REPOSITORY="$1" - debug "Only repository $1 will be processed per request." shift;; (-l|--limit) shift export BWLIMIT="$1" - debug "Bandwidth will be limited to $BWLIMIT Kbps per request." + shift;; + (-v|--verbose) + shift + export VERBOSE="$1" shift;; (-h|--help) help;; @@ -64,23 +65,32 @@ flags() done } flags "$@" +source /home/shmick/Scripts/Okiru "$VERBOSE" logging Kumonoboru #Defaults if [[ -z $BWLIMIT ]]; then export BWLIMIT="0" +else + debug "Bandwidth will be limited to" "$BWLIMIT Kbps" fi +if [[ -n $CLEAN ]]; then + debug "Cleaning will take place per request." +fi +if [[ -n $REPOSITORY ]]; then + debug "Will only process repository" "$1" +fi #Safety function; accepts repository to check safety(){ REPOSITORY="$1" - info "Checking if repository $REPOSITORY is in use..." + info "Checking if repository is in use - " "$REPOSITORY" #Check no other Restic process is using this repository; Free unnecessary locks, if present if [[ -n $(ps aux | grep restic | grep "$REPOSITORY") ]]; then - warn "Repository $REPOSITORY is in use - ignoring" + warn "Repository is in use - ignoring" return 1 # ^ If there's a restic process holding the repository, leave it alone. else - info "Repository $REPOSITORY is not in use - unlocking" - restic -r b2:$REPOSITORY unlock + info "Repository is not in use - unlocking" + restic -q -r b2:$REPOSITORY unlock # ^ If a lock exists but no process, the repository is safe and should be unlocked. fi } @@ -90,46 +100,46 @@ backup(){ REPOSITORY_PATH="$2" if safety "$REPOSITORY"; then #Run the backup - info "Backing up repository $REPOSITORY" + info "Backing up repository" "$REPOSITORY" if restic --cache-dir="$RESTIC_CACHE_DIR" -r b2:"$REPOSITORY" backup "$REPOSITORY_PATH" --limit-upload="$BWLIMIT" --limit-download="$BWLIMIT" | tee -a $LOG; then - ok "Path $REPOSITORY_PATH completed upload to $REPOSITORY." + ok "$REPOSITORY_PATH" "completed upload to $REPOSITORY." check "$REPOSITORY" else - error "Repository $REPOSTIORY failed to upload path $REPOSITORY_PATH!" + error "$REPOSITORY failed to upload path" "$REPOSITORY_PATH" fi fi } check(){ REPOSITORY="$1" PRUNE="$2" - debug "Working on Repostory $1 with prune option $2" + debug "Checking integrity (prune: $PRUNE) of repository" "$REPOSITORY" ## ^ This variable will have value if repo is already clean, indicating #+ This is a post backup check. if [[ -n $PRUNE ]]; then warn "This repository has been cleaned already; will not clean again." fi if safety "$REPOSITORY"; then - info "Checking repository $REPOSITORY" + info "Checking repository health - " "$REPOSITORY" if restic -r b2:"$REPOSITORY" check --limit-upload="$BWLIMIT" --limit-download="$BWLIMIT" | tee -a $LOG; then - ok "Repository $REPOSITORY passed integrity check!" + ok "Repository passed integrity check - " "$REPOSITORY" info "Current snapshots:" restic -r b2:"$REPOSITORY" snapshots | tee -a $LOG else - error "Repository $REPOSITORY failed integrity check!" + error "Repository failed integrity check - " "$REPOSITORY" fi fi } clean(){ REPOSITORY="$1" if safety "$REPOSITORY"; then - info "Cleaning repository $REPOSITORY" + info "Cleaning repository" "$REPOSITORY" if restic -r b2:$REPOSITORY forget --keep-daily 7 --keep-weekly 4 --keep-monthly 12 --prune --limit-upload="$BWLIMIT" --limit-download="$BWLIMIT" | tee -a $LOG; then - ok "Repository $REPOSITORY is trim!" + ok "Repository is trim - " "$REPOSITORY" debug "Running post clean check..." check "$REPOSITORY" "1" # Marks repository as cleaned already ^ so it won't passed to this function again. else - error "Repository $REPOSITORY failed to prune!" + error "Failed to prune repository" "$REPOSITORY" fi fi } diff --git a/Okiru b/Okiru index f1ddc40..05e48e7 100755 --- a/Okiru +++ b/Okiru @@ -83,9 +83,17 @@ logging () { if [[ -n $VERBOSE ]]; then #Debugging level logging; can be toggled via a switch debug () { - printf "${BLUE}[DEBUG]: $1${STOP}\n" - if [[ -f $LOG ]]; then - echo -e "$(date +"%T:%N") [DEBUG]: $1" >> $LOG + if [[ -z $2 ]]; then + printf "${PURPLE}$(date +"%T:%N")${STOP} ${BLUE}[DEBUG]: %s${STOP}\n" "$1" + if [[ -f $LOG ]]; then + echo -e "$(date +"%T:%N") [DEBUG]: $1" >> $LOG + fi + elif [[ -n $2 ]]; then + printf "${PURPLE}$(date +"%T:%N")${STOP} ${BLUE}[DEBUG]: %s${STOP} ${LIGHT_BLUE}%s${STOP}\n" "$1" "$2" + if [[ -f $LOG ]]; then + echo -e "$(date +"%T:%N") [DEBUG]: $1" >> $LOG + fi + fi } #Otherwise, ignore debug calls; @@ -97,32 +105,43 @@ fi #Information level logging; info () { if [[ -z $2 ]]; then - printf "${CYAN}[INFO]:${STOP} %s\n" "$1" + printf "${PURPLE}$(date +"%T:%N")${STOP} ${CYAN}[INFO]:${STOP} %s\n" "$1" if [[ -f $LOG ]]; then echo -e "$(date +"%T:%N") [INFO]: $1" >> $LOG fi elif [[ -n $2 ]]; then - printf "${CYAN}[INFO]:${STOP} %s ${PURPLE}%s${STOP}\n" "$1" "$2" + printf "${PURPLE}$(date +"%T:%N")${STOP} ${CYAN}[INFO]:${STOP} %s ${LIGHT_CYAN}%s${STOP}\n" "$1" "$2" if [[ -f $LOG ]]; then - printf "$(date +"%T:%N")${CYAN}[INFO]:${STOP} %s ${PURPLE}%s${STOP}\n" "$1" "$2" >> $LOG + printf "${PURPLE}$(date +"%T:%N")${CYAN}[INFO]:${STOP} %s ${LIGHT_CYAN}%s${STOP}\n" "$1" "$2" >> $LOG fi fi } #Warning level logging; warn () { - printf "${YELLOW}[WARNING]:${STOP} $1\n" - if [[ -f $LOG ]]; then - echo -e "$(date +"%T:%N") [WARN]: $1" >> $LOG + if [[ -z $2 ]]; then + printf "${PURPLE}$(date +"%T:%N")${STOP} ${ORANGE}[WARNING]:${STOP} %s\n" "$1" + if [[ -f $LOG ]]; then + echo -e "$(date +"%T:%N") [WARN]: $1" >> $LOG + fi + elif [[ -n $2 ]]; then + printf "${PURPLE}$(date +"%T:%N")${STOP} ${ORANGE}[WARNING]:${STOP} %s ${YELLOW}%s${STOP}\n" "$1" "$2" + if [[ -f $LOG ]]; then + echo -e "$(date +"%T:%N") [WARN]: $1" >> $LOG + fi fi } #Error logging function; Errors are added to an array and a report file. error () { - printf "${RED}[ERROR]: $1${STOP}\n" -# ^ Print it to the screen + if [[ -z $2 ]]; then + printf "${PURPLE}$(date +"%T:%N")${STOP} ${RED}[ERROR]: %s${STOP}\n" "$1" +# ^ Print it to the screen + elif [[ -n $2 ]]; then + printf "${PURPLE}$(date +"%T:%N")${STOP} ${RED}[ERROR]: %s${STOP}${LIGHT_RED} %s${STOP}\n" "$1" + fi errors+=("$1") # ^ And add to errors array if [[ -f $LOG ]]; then - echo -e "$(date +"%T:%N") [ERROR]: $1" >> $LOG + echo -e "$(date +"%T:%N") [ERROR]: $1" >> $LOG # ^ Log it with its' time and date fi if [[ -f $REPORT ]]; then @@ -132,9 +151,16 @@ error () { return 1 } ok () { - printf "${GREEN}[SUCCESS]: $1${STOP}\n" - if [[ -f $LOG ]]; then - echo -e "$(date +"%T:%N") [SUCCESS]: $1" >> $LOG + if [[ -z $2 ]]; then + printf "${PURPLE}$(date +"%T:%N")${STOP} ${GREEN}[SUCCESS]: %s${STOP}\n" "$1" + if [[ -f $LOG ]]; then + echo -e "$(date +"%T:%N") [SUCCESS]: $1" >> $LOG + fi + elif [[ -n $2 ]]; then + printf "${PURPLE}$(date +"%T:%N")${STOP} ${GREEN}[SUCCESS]:${LIGHT_GREEN} %s${STOP}${GREEN} %s${STOP}\n" "$1" "$2" + if [[ -f $LOG ]]; then + echo -e "$(date +"%T:%N") [SUCCESS]: $1" >> $LOG + fi fi }