From a9fa8e1e4f395743c78003a78c280559abb4a024 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20GUEZO?= Date: Wed, 30 Jul 2025 10:32:30 +0200 Subject: [PATCH] feat: update print function --- install.sh | 40 ++++++++++++++++++++-------------------- scripts/aws-backup.sh | 43 +++++++++++++------------------------------ scripts/common.sh | 13 +++++++++++-- 3 files changed, 44 insertions(+), 52 deletions(-) diff --git a/install.sh b/install.sh index 2be39e6..c108e30 100644 --- a/install.sh +++ b/install.sh @@ -10,10 +10,10 @@ create_env_variable() { if [[ -z "$value" ]]; then if grep -q "^$key=*" "$ENV_FILE" 2>/dev/null; then - datetime_print "$key not updated." + info_print "$key not updated." return else - datetime_print "$key not set (empty input)." + info_print "$key not set (empty input)." 1 return fi fi @@ -27,15 +27,15 @@ create_env_variable() { case "$yn" in [Yy]*) sed -i "s/^$key=.*/$key=$value/" "$ENV_FILE" - datetime_print "$key updated." + info_print "$key updated." ;; *) - datetime_print "$key not changed." + info_print "$key not changed." ;; esac else echo "$key=$value" >> "$ENV_FILE" - datetime_print "$key \e[32mset\e[0m." + info_print "$key \e[32mset\e[0m." fi } @@ -44,10 +44,10 @@ install_file() { local pathname="$2" local argument="${3:--Dm755}" - datetime_print "Installing $filename to $pathname..." + info_print "Installing $filename to $pathname..." if [[ ! -f "scripts/$filename" ]]; then - datetime_print "Source file scripts/$filename not found." "\e[31mERROR\e[0m" + info_print "Source file scripts/$filename not found." "\e[31mERROR\e[0m" exit 1 fi @@ -61,21 +61,21 @@ install_file() { [Yy]*) ;; *) - datetime_print "$pathname/$filename not changed." + info_print "$pathname/$filename not changed." return ;; esac fi install $argument "scripts/$filename" "$pathname/$filename" && \ - datetime_print "$filename \e[32minstalled.\e[0m" || \ - { datetime_print "Error while installing $filename" "\e[31mERROR\e[0m"; exit 1; } + info_print "$filename \e[32minstalled.\e[0m" || \ + { info_print "Error while installing $filename" "\e[31mERROR\e[0m"; exit 1; } } AUTO_CONFIRM=false [[ "$1" == "--true" ]] && AUTO_CONFIRM=true -INSTALLED_PROG=("curl" "aws") +INSTALLED_PROG=("curl") for prog in ${INSTALLED_PROG[@]}; do echo -n "Verifing $prog... " if ! command -v $prog &> "/dev/null"; then @@ -93,21 +93,21 @@ fi touch "$LOG" || { echo "\e[31mCannot create log file $LOG\e[0m"; exit 1; } chmod 644 "$LOG" -datetime_print "Starting ServerConfig Installation v1.0.0" +info_print "Starting ServerConfig Installation v1.0.0" mkdir -p "$ETC_DIR" && \ -datetime_print "$ETC_DIR ensured." +info_print "$ETC_DIR ensured." -datetime_print "Installing scripts to $SCRIPT_FILE..." +info_print "Installing scripts to $SCRIPT_FILE..." echo -datetime_print "------------- Install Files -------------" +info_print "------------- Install Files -------------" for element in scripts/*.sh; do install_file "$(basename $element)" "$SCRIPT_FILE" -Dm755; done install_file "docker-compose.yml" "$ETC_DIR" -Dm644 echo -datetime_print "--------- Define .env variables ---------" +info_print "--------- Define .env variables ---------" ENV_LIST=("AWS" "TELEGRAM_CHAT_ID" "TELEGRAM_TOKEN" "EMAIL" "WG_HOSTNAME_VPN") @@ -125,14 +125,14 @@ while true; do done echo -datetime_print "---------- Script Installation ----------" +info_print "---------- Script Installation ----------" for element in /usr/local/bin/*.sh; do bash "$element" --install done echo -datetime_print "--------- Installation Complete ---------" -datetime_print "All config files are in $ETC_DIR" -datetime_print "All scripts are in $SCRIPT_FILE" +info_print "--------- Installation Complete ---------" +info_print "All config files are in $ETC_DIR" +info_print "All scripts are in $SCRIPT_FILE" echo "Log file written at: $LOG" \ No newline at end of file diff --git a/scripts/aws-backup.sh b/scripts/aws-backup.sh index ad6adb2..d4fe8b2 100644 --- a/scripts/aws-backup.sh +++ b/scripts/aws-backup.sh @@ -1,40 +1,27 @@ #!/bin/bash -source /etc/serverconfig/.env +source /usr/local/bin/notifications.sh +source /usr/local/bin/common.sh DIR="$(cd "$(dirname "$0")" && pwd)" BACKUP="$DIR/aws-bakup.bak" -LOG="/var/log/aws-bak.log" SYNCED_FILES="" -date_print() { - echo -n "$(date +"%d-%m-%Y %H:%M:%S") - " | tee -a $LOG -} -error_print() { - date_print - if [ "$1" == "true" ]; then - echo "Backup error $2 -/> s3://$AWS : " | tee -a "$LOG" - else - echo "Backup succeeded --> s3://$AWS : " | tee -a "$LOG" - fi -} - -if [ "$1" == "clean" ]; then - if [ "$2" == "aws" ]; then - echo "Cleaning the S3 bucket: s3://$AWS" | tee -a "$LOG" +if [[ "$1" == "clean" ]] then + info_print "Cleaning the S3 bucket: s3://$AWS" | tee -a "$LOG" aws s3 rm "s3://$AWS/" --recursive | tee -a "$LOG" if [ $? -ne 0 ]; then - error_print true "Failed to clean the S3 bucket" + info_print "Failed to clean the S3 bucket" 3 exit 1 fi - echo "Bucket cleaned successfully." | tee -a "$LOG" + info_print "Bucket cleaned successfully." exit 0 else - echo "Purge aws-bak files." + info_print "Purge aws-bak files." rm -f $BACKUP $LOG $DIR/aws-bak.sh exit 0 fi @@ -46,8 +33,7 @@ fi if [ ! -e "$BACKUP" ]; then touch "$BACKUP" - error_print true "$BACKUP" - echo "$BACKUP created. Please include only the dirname in this file." | tee -a "$LOG" + info_print "$BACKUP created. Please only include dirnames." 5 exit 1 fi @@ -60,24 +46,21 @@ while IFS= read -r SOURCE_PATH || [ -n "$SOURCE_PATH" ]; do aws s3 sync "$SOURCE_PATH" "s3://$AWS/$(basename "$SOURCE_PATH")" --delete > /dev/null 2>&1 if [ $? -ne 0 ]; then - error_print true "$SOURCE_PATH" - echo "Error while syncing $SOURCE_PATH to the AWS server." | tee -a "$LOG" + info_print "Error while syncing $SOURCE_PATH to the AWS server." 3 exit 1 fi SYNCED_FILES="$SYNCED_FILES\n$SOURCE_PATH" else - error_print true "$SOURCE_PATH" - echo "$SOURCE_PATH not found or inaccessible." | tee -a "$LOG" + info_print "$SOURCE_PATH not found or inaccessible." 3 fi done < "$BACKUP" if [ -n "$SYNCED_FILES" ]; then - error_print false "" - echo -e "Files synced:$SYNCED_FILES" | tee -a "$LOG" + info_print "Files synced:$SYNCED_FILES" else - echo "No files synced." | tee -a "$LOG" + info_print "No files synced." 3 exit 0; fi -echo "All files synced to AWS." | tee -a "$LOG" \ No newline at end of file +info_print "All files synced to AWS." \ No newline at end of file diff --git a/scripts/common.sh b/scripts/common.sh index def8590..898ac00 100644 --- a/scripts/common.sh +++ b/scripts/common.sh @@ -8,10 +8,19 @@ LOG="/var/log/$FILENAME.log" DATETIME_FORMAT="%d-%m-%Y %H:%M:%S" -datetime_print() { +info_print() { local message="$1" - local level="${2:-INFO}" + local level="${2:-1}" local timestamp="[$(date +"$DATETIME_FORMAT")]" + case $level in + 1) local level="\e[34mINFO\e[0m";; + 2) local level="\e[33mWARN\e[0m";; + 3) local level="\e[31mERROR\e[0m";; + 4) local level="\e[35mDEBUG\e[0m";; + 5) local level="\e[36mACTION\e[0m";; + *);; + esac + echo -e "$timestamp - $level: $message" | tee -a "$LOG" } \ No newline at end of file