feat: update print function

This commit is contained in:
2025-07-30 10:32:30 +02:00
parent ef4b24a200
commit a9fa8e1e4f
3 changed files with 44 additions and 52 deletions

View File

@@ -10,10 +10,10 @@ create_env_variable() {
if [[ -z "$value" ]]; then if [[ -z "$value" ]]; then
if grep -q "^$key=*" "$ENV_FILE" 2>/dev/null; then if grep -q "^$key=*" "$ENV_FILE" 2>/dev/null; then
datetime_print "$key not updated." info_print "$key not updated."
return return
else else
datetime_print "$key not set (empty input)." info_print "$key not set (empty input)." 1
return return
fi fi
fi fi
@@ -27,15 +27,15 @@ create_env_variable() {
case "$yn" in case "$yn" in
[Yy]*) [Yy]*)
sed -i "s/^$key=.*/$key=$value/" "$ENV_FILE" 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 esac
else else
echo "$key=$value" >> "$ENV_FILE" echo "$key=$value" >> "$ENV_FILE"
datetime_print "$key \e[32mset\e[0m." info_print "$key \e[32mset\e[0m."
fi fi
} }
@@ -44,10 +44,10 @@ install_file() {
local pathname="$2" local pathname="$2"
local argument="${3:--Dm755}" local argument="${3:--Dm755}"
datetime_print "Installing $filename to $pathname..." info_print "Installing $filename to $pathname..."
if [[ ! -f "scripts/$filename" ]]; then 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 exit 1
fi fi
@@ -61,21 +61,21 @@ install_file() {
[Yy]*) [Yy]*)
;; ;;
*) *)
datetime_print "$pathname/$filename not changed." info_print "$pathname/$filename not changed."
return return
;; ;;
esac esac
fi fi
install $argument "scripts/$filename" "$pathname/$filename" && \ install $argument "scripts/$filename" "$pathname/$filename" && \
datetime_print "$filename \e[32minstalled.\e[0m" || \ info_print "$filename \e[32minstalled.\e[0m" || \
{ datetime_print "Error while installing $filename" "\e[31mERROR\e[0m"; exit 1; } { info_print "Error while installing $filename" "\e[31mERROR\e[0m"; exit 1; }
} }
AUTO_CONFIRM=false AUTO_CONFIRM=false
[[ "$1" == "--true" ]] && AUTO_CONFIRM=true [[ "$1" == "--true" ]] && AUTO_CONFIRM=true
INSTALLED_PROG=("curl" "aws") INSTALLED_PROG=("curl")
for prog in ${INSTALLED_PROG[@]}; do for prog in ${INSTALLED_PROG[@]}; do
echo -n "Verifing $prog... " echo -n "Verifing $prog... "
if ! command -v $prog &> "/dev/null"; then 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; } touch "$LOG" || { echo "\e[31mCannot create log file $LOG\e[0m"; exit 1; }
chmod 644 "$LOG" chmod 644 "$LOG"
datetime_print "Starting ServerConfig Installation v1.0.0" info_print "Starting ServerConfig Installation v1.0.0"
mkdir -p "$ETC_DIR" && \ 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 echo
datetime_print "------------- Install Files -------------" info_print "------------- Install Files -------------"
for element in scripts/*.sh; do install_file "$(basename $element)" "$SCRIPT_FILE" -Dm755; done for element in scripts/*.sh; do install_file "$(basename $element)" "$SCRIPT_FILE" -Dm755; done
install_file "docker-compose.yml" "$ETC_DIR" -Dm644 install_file "docker-compose.yml" "$ETC_DIR" -Dm644
echo echo
datetime_print "--------- Define .env variables ---------" info_print "--------- Define .env variables ---------"
ENV_LIST=("AWS" "TELEGRAM_CHAT_ID" "TELEGRAM_TOKEN" "EMAIL" "WG_HOSTNAME_VPN") ENV_LIST=("AWS" "TELEGRAM_CHAT_ID" "TELEGRAM_TOKEN" "EMAIL" "WG_HOSTNAME_VPN")
@@ -125,14 +125,14 @@ while true; do
done done
echo echo
datetime_print "---------- Script Installation ----------" info_print "---------- Script Installation ----------"
for element in /usr/local/bin/*.sh; do for element in /usr/local/bin/*.sh; do
bash "$element" --install bash "$element" --install
done done
echo echo
datetime_print "--------- Installation Complete ---------" info_print "--------- Installation Complete ---------"
datetime_print "All config files are in $ETC_DIR" info_print "All config files are in $ETC_DIR"
datetime_print "All scripts are in $SCRIPT_FILE" info_print "All scripts are in $SCRIPT_FILE"
echo "Log file written at: $LOG" echo "Log file written at: $LOG"

View File

@@ -1,40 +1,27 @@
#!/bin/bash #!/bin/bash
source /etc/serverconfig/.env source /usr/local/bin/notifications.sh
source /usr/local/bin/common.sh
DIR="$(cd "$(dirname "$0")" && pwd)" DIR="$(cd "$(dirname "$0")" && pwd)"
BACKUP="$DIR/aws-bakup.bak" BACKUP="$DIR/aws-bakup.bak"
LOG="/var/log/aws-bak.log"
SYNCED_FILES="" SYNCED_FILES=""
date_print() {
echo -n "$(date +"%d-%m-%Y %H:%M:%S") - " | tee -a $LOG
}
error_print() { if [[ "$1" == "clean" ]] then
date_print info_print "Cleaning the S3 bucket: s3://$AWS" | tee -a "$LOG"
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"
aws s3 rm "s3://$AWS/" --recursive | tee -a "$LOG" aws s3 rm "s3://$AWS/" --recursive | tee -a "$LOG"
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
error_print true "Failed to clean the S3 bucket" info_print "Failed to clean the S3 bucket" 3
exit 1 exit 1
fi fi
echo "Bucket cleaned successfully." | tee -a "$LOG" info_print "Bucket cleaned successfully."
exit 0 exit 0
else else
echo "Purge aws-bak files." info_print "Purge aws-bak files."
rm -f $BACKUP $LOG $DIR/aws-bak.sh rm -f $BACKUP $LOG $DIR/aws-bak.sh
exit 0 exit 0
fi fi
@@ -46,8 +33,7 @@ fi
if [ ! -e "$BACKUP" ]; then if [ ! -e "$BACKUP" ]; then
touch "$BACKUP" touch "$BACKUP"
error_print true "$BACKUP" info_print "$BACKUP created. Please only include dirnames." 5
echo "$BACKUP created. Please include only the dirname in this file." | tee -a "$LOG"
exit 1 exit 1
fi 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 aws s3 sync "$SOURCE_PATH" "s3://$AWS/$(basename "$SOURCE_PATH")" --delete > /dev/null 2>&1
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
error_print true "$SOURCE_PATH" info_print "Error while syncing $SOURCE_PATH to the AWS server." 3
echo "Error while syncing $SOURCE_PATH to the AWS server." | tee -a "$LOG"
exit 1 exit 1
fi fi
SYNCED_FILES="$SYNCED_FILES\n$SOURCE_PATH" SYNCED_FILES="$SYNCED_FILES\n$SOURCE_PATH"
else else
error_print true "$SOURCE_PATH" info_print "$SOURCE_PATH not found or inaccessible." 3
echo "$SOURCE_PATH not found or inaccessible." | tee -a "$LOG"
fi fi
done < "$BACKUP" done < "$BACKUP"
if [ -n "$SYNCED_FILES" ]; then if [ -n "$SYNCED_FILES" ]; then
error_print false "" info_print "Files synced:$SYNCED_FILES"
echo -e "Files synced:$SYNCED_FILES" | tee -a "$LOG"
else else
echo "No files synced." | tee -a "$LOG" info_print "No files synced." 3
exit 0; exit 0;
fi fi
echo "All files synced to AWS." | tee -a "$LOG" info_print "All files synced to AWS."

View File

@@ -8,10 +8,19 @@ LOG="/var/log/$FILENAME.log"
DATETIME_FORMAT="%d-%m-%Y %H:%M:%S" DATETIME_FORMAT="%d-%m-%Y %H:%M:%S"
datetime_print() { info_print() {
local message="$1" local message="$1"
local level="${2:-INFO}" local level="${2:-1}"
local timestamp="[$(date +"$DATETIME_FORMAT")]" 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" echo -e "$timestamp - $level: $message" | tee -a "$LOG"
} }