bashscripts/utils/logger.sh

39 lines
1.1 KiB
Bash
Raw Normal View History

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