Update installer.sh

This commit is contained in:
SubleXBle
2025-07-28 08:00:12 +02:00
committed by GitHub
parent 56a967aa9a
commit 9677d540f3

View File

@@ -7,105 +7,115 @@ RED='\033[1;31m'
YELLOW='\033[33m'
BLUE='\033[34m'
# Repo info
REPO_URL="https://github.com/SubleXBle/Fail2Ban-Report.git"
BRANCH_NAME="latest"
# Default install path
TARGET_DIR="/var/www/html/Fail2Ban-Report"
DEFAULT_WEBROOT="/var/www/html"
DEFAULT_SH_PATH="/opt/Fail2Ban-Report"
echo -e "${BLUE}--- Fail2Ban-Report Installer ---${NORMAL}"
# Ask for custom install path
read -rp "Do you want to use a custom installation path? (Y/N): " USE_CUSTOM_PATH
USE_CUSTOM_PATH=${USE_CUSTOM_PATH,,} # to lowercase
# Ask for Webroot path
read -rp "Enter your webroot path where the tool should be installed (default: $DEFAULT_WEBROOT): " WEBROOT
WEBROOT=${WEBROOT:-$DEFAULT_WEBROOT}
if [[ "$USE_CUSTOM_PATH" == "y" ]]; then
read -rp "Enter the full desired installation path (or leave empty for default: $TARGET_DIR): " USER_PATH
if [[ -z "$USER_PATH" ]]; then
echo "No path entered. Using default: $TARGET_DIR"
else
if mkdir -p "$USER_PATH" 2>/dev/null; then
TARGET_DIR="$USER_PATH"
echo "Using custom path: $TARGET_DIR"
else
echo -e "${YELLOW}Invalid path or no permission to create directory. Using default path: $TARGET_DIR${NORMAL}"
fi
fi
else
echo "Using default installation path: $TARGET_DIR"
fi
# Full target directory where repo will be cloned (webroot + Fail2Ban-Report)
TARGET_DIR="${WEBROOT%/}/Fail2Ban-Report"
echo -e "${BLUE}Checking if Git is installed...${NORMAL}"
echo -e "Using webroot installation path: $TARGET_DIR"
# Ask for .sh script storage path
read -rp "Enter path where fail2ban_log2json.sh should be stored (default: $DEFAULT_SH_PATH): " SH_PATH
SH_PATH=${SH_PATH:-$DEFAULT_SH_PATH}
echo -e "Using shell script path: $SH_PATH"
# Check for git
echo -e "${BLUE}Checking if git is installed...${NORMAL}"
if ! command -v git &> /dev/null; then
echo -e "${RED}Error: Git is not installed. Please install Git and rerun this script.${NORMAL}"
exit 1
echo -e "${RED}Git not found. Please install git and rerun the installer.${NORMAL}"
exit 1
fi
# Clone or update repo
if [ ! -d "$TARGET_DIR" ]; then
echo -e "${YELLOW}Target directory does not exist. Cloning repository...${NORMAL}"
git clone -b "$BRANCH_NAME" "$REPO_URL" "$TARGET_DIR"
if [ $? -ne 0 ]; then
echo -e "${RED}Failed to clone repository.${NORMAL}"
exit 1
fi
echo -e "${YELLOW}Cloning Fail2Ban-Report repository into $TARGET_DIR...${NORMAL}"
git clone -b "$BRANCH_NAME" "$REPO_URL" "$TARGET_DIR"
if [ $? -ne 0 ]; then
echo -e "${RED}Failed to clone repository.${NORMAL}"
exit 1
fi
else
echo -e "${BLUE}Repository already exists. Pulling latest changes...${NORMAL}"
cd "$TARGET_DIR" || { echo -e "${RED}Failed to cd into $TARGET_DIR${NORMAL}"; exit 1; }
git pull origin "$BRANCH_NAME"
if [ $? -ne 0 ]; then
echo -e "${RED}Failed to pull latest changes.${NORMAL}"
exit 1
fi
echo -e "${BLUE}Repository already exists. Pulling latest changes...${NORMAL}"
cd "$TARGET_DIR" || { echo -e "${RED}Cannot cd to $TARGET_DIR${NORMAL}"; exit 1; }
git pull origin "$BRANCH_NAME"
if [ $? -ne 0 ]; then
echo -e "${RED}Failed to pull repository updates.${NORMAL}"
exit 1
fi
fi
# Set permissions for writable folders (archive & includes)
echo -e "${BLUE}Setting permissions for archive and includes folders...${NORMAL}"
mkdir -p "$TARGET_DIR/archive"
chmod 755 "$TARGET_DIR/archive"
chmod 755 "$TARGET_DIR/includes"
# Create shell script target dir if needed
mkdir -p "$SH_PATH"
# Prompt for archive folder path in .sh script
FAIL2BAN_SH="$TARGET_DIR/fail2ban_log2json.sh"
echo -e "${BLUE}Configuring path to archive folder in fail2ban_log2json.sh...${NORMAL}"
# Escape slashes for sed usage
ESCAPED_ARCHIVE_PATH=$(echo "$TARGET_DIR/archive" | sed 's_/_\\/_g')
# Replace the archive path line (assumes a specific placeholder or variable line)
# Let's assume the line starts with ARCHIVE_PATH= or similar in the .sh
if grep -q "^ARCHIVE_PATH=" "$FAIL2BAN_SH"; then
sed -i "s/^ARCHIVE_PATH=.*/ARCHIVE_PATH=\"$ESCAPED_ARCHIVE_PATH\"/" "$FAIL2BAN_SH"
# Copy fail2ban_log2json.sh to SH_PATH and make executable
if [ -f "$TARGET_DIR/fail2ban_log2json.sh" ]; then
echo -e "${BLUE}Copying fail2ban_log2json.sh to $SH_PATH...${NORMAL}"
cp "$TARGET_DIR/fail2ban_log2json.sh" "$SH_PATH/"
chmod +x "$SH_PATH/fail2ban_log2json.sh"
else
# fallback: append at start if not found
sed -i "1iARCHIVE_PATH=\"$ESCAPED_ARCHIVE_PATH\"" "$FAIL2BAN_SH"
echo -e "${RED}fail2ban_log2json.sh not found in repo, please check.${NORMAL}"
exit 1
fi
echo -e "${GREEN}Archive path set to: $TARGET_DIR/archive${NORMAL}"
# Set archive path inside the .sh script to point to the archive folder inside the webroot repo
ARCHIVE_PATH="${TARGET_DIR}/archive"
echo -e "${BLUE}Setting archive path in fail2ban_log2json.sh to $ARCHIVE_PATH ...${NORMAL}"
# Escape slashes for sed
ESCAPED_ARCHIVE_PATH=$(echo "$ARCHIVE_PATH" | sed 's_/_\\/_g')
if grep -q "^ARCHIVE_PATH=" "$SH_PATH/fail2ban_log2json.sh"; then
sed -i "s/^ARCHIVE_PATH=.*/ARCHIVE_PATH=\"$ESCAPED_ARCHIVE_PATH\"/" "$SH_PATH/fail2ban_log2json.sh"
else
sed -i "1iARCHIVE_PATH=\"$ESCAPED_ARCHIVE_PATH\"" "$SH_PATH/fail2ban_log2json.sh"
fi
# Create archive folder if missing
mkdir -p "$ARCHIVE_PATH"
chmod 755 "$ARCHIVE_PATH"
# Set permissions and ownership to www-data:www-data on the entire tool directory
echo -e "${BLUE}Setting ownership of $TARGET_DIR to www-data:www-data ...${NORMAL}"
chown -R www-data:www-data "$TARGET_DIR"
# Remove fail2ban_log2json.sh from repo folder to avoid duplicates
if [ -f "$TARGET_DIR/fail2ban_log2json.sh" ]; then
echo -e "${BLUE}Removing fail2ban_log2json.sh from repo directory to avoid duplicates...${NORMAL}"
rm "$TARGET_DIR/fail2ban_log2json.sh"
fi
# Inform about .htaccess setup
echo -e "${BLUE}\nIMPORTANT:${NORMAL} Please configure your .htaccess file in the webroot to secure your app as described in the README."
echo "You may want to activate protections by uncommenting the relevant lines."
echo "Example .htaccess is included."
echo -e "${BLUE}\nIMPORTANT: Configure your .htaccess in the webroot to secure the application.${NORMAL}"
echo "Example .htaccess is included in the repo."
# Ask about setting up a daily cronjob for the JSON update script
read -rp "Do you want to install a daily cronjob to run fail2ban_log2json.sh at 3 AM? (Y/N): " INSTALL_CRON
# Ask about cronjob setup
read -rp "Do you want to install a daily cronjob for fail2ban_log2json.sh at 3 AM? (Y/N): " INSTALL_CRON
INSTALL_CRON=${INSTALL_CRON,,}
CRON_CMD="0 3 * * * $SH_PATH/fail2ban_log2json.sh > /dev/null 2>&1"
if [[ "$INSTALL_CRON" == "y" ]]; then
CRON_CMD="0 3 * * * $FAIL2BAN_SH > /dev/null 2>&1"
(crontab -l 2>/dev/null | grep -v -F "$FAIL2BAN_SH"; echo "$CRON_CMD") | crontab -
echo -e "${GREEN}Cronjob installed:${NORMAL} $CRON_CMD"
(crontab -l 2>/dev/null | grep -v -F "$SH_PATH/fail2ban_log2json.sh"; echo "$CRON_CMD") | crontab -
echo -e "${GREEN}Cronjob installed:${NORMAL} $CRON_CMD"
else
echo -e "${YELLOW}Skipping cronjob setup. Remember to setup cron manually to generate JSON files daily.${NORMAL}"
echo -e "${YELLOW}Skipping cronjob setup.${NORMAL}"
echo "To set it up manually, add this line to your crontab:"
echo "$CRON_CMD"
fi
echo -e "${GREEN}\nInstallation complete!${NORMAL}"
echo "You can now configure your webserver to serve files from:"
echo " $TARGET_DIR"
echo
echo "Remember to secure your web app with proper .htaccess or other access control."
echo "Also, check and customize fail2ban_log2json.sh if needed."
echo -e "${GREEN}\nInstallation completed successfully!${NORMAL}"
echo "Webroot path: $TARGET_DIR"
echo "Shell script path: $SH_PATH/fail2ban_log2json.sh"
echo
echo "Remember to adjust your webserver config and secure the web directory properly."
echo "Happy selfhosting! 🚀"