bashscripts/utils/logger.sh

39 lines
1.1 KiB
Bash

#!/bin/bash
#Use ./logger.sh <logfile> <relativepath>
# uncomment when use script from cron
PATH=$PATH:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
# variables
REMOTE_PATH="$ES7_LOGS_PATH$2"
PUBLIC_LOG_URL="$ES7_LOGS_URL/$2"
# get file name in case of $1 is a path
file=$(basename -- "$1")
extension=${file##*.}
base=$(basename "${file%.*}")
# create remote folder to store logs if not exist
# \$ allow read remote variable value
ssh $ES7_LOGS_SERVER_SSH "sudo mkdir -p $REMOTE_PATH && sudo chown -R \$(whoami): $REMOTE_PATH"
# Copy local logfile to remote destination
scp $1 "$ES7_LOGS_SERVER_SSH:$REMOTE_PATH"
# set correct permissions
ssh $ES7_LOGS_SERVER_SSH "chmod 644 $REMOTE_PATH$file"
# Clear x days old remote logs
ssh $ES7_LOGS_SERVER_SSH 'bash -s' << EOF
find $REMOTE_PATH -maxdepth 1 -mtime +$E7S_LOGS_PRESERVE_DAYS -exec "rm" -R {} \;
EOF
# return final log url
# change to txt if log is another text format
if [ $extension != "txt" ]; then
ssh $ES7_LOGS_SERVER_SSH "mv $REMOTE_PATH$file $REMOTE_PATH$base.txt"
fi
echo "${PUBLIC_LOG_URL}${base}.txt"