Files
server-scripts/scripts-download.sh
Jose Henrique 819816572e
All checks were successful
Check scripts syntax / check-scripts-syntax (push) Successful in 22s
menu for file selection
2025-07-26 14:04:03 -03:00

288 lines
9.7 KiB
Bash

#!/bin/bash
# Usage:
## curl -sSL https://git.ivanch.me/ivanch/server-scripts/raw/branch/main/scripts-download.sh | bash
# colors
RED='\033[1;31m'
GREEN='\033[1;32m'
NC='\033[0m'
LIGHT_BLUE='\033[1;34m'
LIGHT_RED='\033[1;31m'
LIGHT_GREEN='\033[1;32m'
GREY='\033[1;30m'
YELLOW='\033[1;33m'
FILES_URL="https://git.ivanch.me/ivanch/server-scripts/raw/branch/main"
echo -e "\r${LIGHT_BLUE}[i] Running scripts-download.sh"
# Detect OS (Debian or Alpine)
echo -e "${GREY}[i] Detecting OS..."
DETECTED=""
if [ -x "$(command -v apk)" ]; then
DETECTED="Alpine"
fi
if [ -x "$(command -v apt)" ]; then
DETECTED="Debian"
fi
if [ -z "$DETECTED" ]; then
echo -e "${RED}[x] Error: OS not supported.${NC}" >&2
exit 1
fi
echo -e "${GREEN}[✓] Detected '$DETECTED' Linux.${NC}"
echo -e "${GREY}[i] Checking if required packages are installed..."
PACKAGES=("zip" "unzip" "sha256sum" "curl" "crontab")
NOT_INSLALLED=()
detect_packages() {
for PACKAGE in "${PACKAGES[@]}"; do
if ! [ -x "$(command -v $PACKAGE)" ]; then
echo -e "${YELLOW}[!] Error: $PACKAGE is not installed, will attempt to install later.${NC}" >&2
NOT_INSLALLED+=($PACKAGE)
fi
done
}
detect_packages
if [ ${#NOT_INSLALLED[@]} -ne 0 ]; then
if [ "$DETECTED" == "Alpine" ]; then
echo -e "${GREY}[i] Installing required packages using APK...${NC}"
echo -e "${GREY}[i] Updating APK...${NC}"
apk update >/dev/null
echo -e "${GREY}[i] Installing packages...${NC}"
apk add --no-cache ${NOT_INSLALLED[@]} >/dev/null
if [ $? -ne 0 ]; then
echo -e "${RED}[x] Error: Failed to install required packages.${NC}" >&2
exit 1
else
echo -e "${GREEN}[✓] All required packages should be installed.${NC}"
fi
elif [ "$DETECTED" == "Debian" ]; then
echo -e "${GREY}[i] Installing required packages using APT...${NC}"
echo -e "${GREY}[i] Updating APT...${NC}"
apt-get update -y >/dev/null
echo -e "${GREY}[i] Installing packages...${NC}"
apt-get install -y ${NOT_INSLALLED[@]} >/dev/null
if [ $? -ne 0 ]; then
echo -e "${RED}[x] Error: Failed to install required packages.${NC}" >&2
exit 1
else
echo -e "${GREEN}[✓] All required packages should be installed.${NC}"
fi
fi
NOT_INSLALLED=()
detect_packages
if [ ${#NOT_INSLALLED[@]} -ne 0 ]; then
echo -e "${RED}[x] Error: Failed to run some of the required packages.${NC}" >&2
echo -e "${RED}[x] [${NOT_INSLALLED[@]}] are not installed.${NC}" >&2
exit 1
fi
fi
echo -e "${GREEN}[✓] All required packages are installed.${NC}"
echo -e "${GREY}[i] Checking if crontab is running..."
# Check if crontab is running on the system using pgrep (crond or cron)
if ! pgrep "cron" > /dev/null; then
echo -e "${RED}[x] Error: Crontab is not running.${NC}" >&2
exit 1
fi
echo -e "${GREEN}[✓] Crontab is running.${NC}"
# Variables
declare -A FILES=(
["clean.sh"]="System cleanup script - Removes temporary files and logs"
["backup.sh"]="Backup script - Creates automated backups of important data"
["docker-updater.sh"]="Docker updater - Automatically updates Docker containers"
)
# Function to display available scripts
display_script_menu() {
echo ""
echo -e "${LIGHT_BLUE}┌─────────────────────────────────────────────────────────────┐${NC}"
echo -e "${LIGHT_BLUE}│ Available Scripts for Installation │${NC}"
echo -e "${LIGHT_BLUE}├─────────────────────────────────────────────────────────────┤${NC}"
local index=1
for script in "${!FILES[@]}"; do
printf "${LIGHT_BLUE}${NC} ${YELLOW}%d.${NC} %-15s ${GREY}%-35s${NC} ${LIGHT_BLUE}${NC}\n" \
"$index" "$script" "${FILES[$script]}"
((index++))
done
echo -e "${LIGHT_BLUE}${NC} ${YELLOW}A.${NC} All scripts ${GREY}Install all available scripts${NC} ${LIGHT_BLUE}${NC}"
echo -e "${LIGHT_BLUE}${NC} ${YELLOW}Q.${NC} Quit ${GREY}Exit without installing anything${NC} ${LIGHT_BLUE}${NC}"
echo -e "${LIGHT_BLUE}└─────────────────────────────────────────────────────────────┘${NC}"
echo ""
}
# Function to get user selection
get_user_selection() {
local scripts_array=($(printf '%s\n' "${!FILES[@]}" | sort))
local selected_files=()
while true; do
display_script_menu
echo -e "${GREY}[i] Enter your choice(s) separated by spaces (e.g., '1 3' or 'A' for all):${NC}"
read -p "Selection: " -r user_input
# Convert to uppercase for easier processing
user_input=$(echo "$user_input" | tr '[:lower:]' '[:upper:]')
# Handle quit option
if [[ "$user_input" == "Q" ]]; then
echo -e "${YELLOW}[!] Installation cancelled by user.${NC}"
exit 0
fi
# Handle select all option
if [[ "$user_input" == "A" ]]; then
selected_files=("${scripts_array[@]}")
break
fi
# Process individual selections
selected_files=()
local valid_selection=true
for choice in $user_input; do
if [[ "$choice" =~ ^[0-9]+$ ]] && [ "$choice" -ge 1 ] && [ "$choice" -le "${#scripts_array[@]}" ]; then
local script_index=$((choice - 1))
local script_name="${scripts_array[$script_index]}"
if [[ ! " ${selected_files[@]} " =~ " ${script_name} " ]]; then
selected_files+=("$script_name")
fi
else
echo -e "${RED}[x] Invalid selection: '$choice'. Please try again.${NC}"
valid_selection=false
break
fi
done
if $valid_selection && [ ${#selected_files[@]} -gt 0 ]; then
break
elif $valid_selection; then
echo -e "${RED}[x] No valid scripts selected. Please try again.${NC}"
fi
echo ""
read -p "Press Enter to continue..." -r
echo ""
done
# Display selected scripts for confirmation
echo ""
echo -e "${GREEN}[✓] Selected scripts for installation:${NC}"
for script in "${selected_files[@]}"; do
echo -e " ${YELLOW}${NC} $script - ${GREY}${FILES[$script]}${NC}"
done
echo ""
# Confirmation prompt
while true; do
read -p "Continue with installation? [Y/n]: " -r confirm
case $confirm in
[Yy]*|"") break ;;
[Nn]*)
echo -e "${YELLOW}[!] Installation cancelled by user.${NC}"
exit 0
;;
*) echo -e "${RED}[x] Please answer Y or n.${NC}" ;;
esac
done
echo "${selected_files[@]}"
}
# Get user selection
echo -e "${GREY}[i] Script selection and installation setup...${NC}"
selected_files_result=$(get_user_selection)
read -a selected_files <<< "$selected_files_result"
if [ ${#selected_files[@]} -eq 0 ]; then
echo -e "${RED}[x] No files selected. Exiting...${NC}"
exit 1
fi
# Check connection with the server for selected files
echo -e "${GREY}[i] Checking connection with the server..."
for FILE in "${selected_files[@]}"; do
curl -s --head "$FILES_URL/$FILE" | head -n 1 | grep -E "HTTP/[12] [23].." > /dev/null
if [ $? -ne 0 ]; then
echo -e "${RED}[x] Error: $FILE not found on the server.${NC}" >&2
exit 1
fi
done
echo -e "${GREEN}[✓] Connection with the server established.${NC}"
echo -e "${GREY}[i] Downloading scripts..."
# Download selected scripts
for FILE in "${selected_files[@]}"; do
curl -s -o "./$FILE" "$FILES_URL/$FILE"
done
echo -e "${GREEN}[✓] Scripts downloaded.${NC}"
CURRENT_WORKDIR=$(pwd)
# Setup permissions
echo -e "${GREY}[i] Setting up permissions..."
# Setup permissions for selected files
for FILE in "${selected_files[@]}"; do
chmod +x "./$FILE"
done
echo -e "${GREEN}[✓] Permissions set up.${NC}"
# Setup crontab for selected files
echo -e "${GREY}[i] Setting up crontab..."
# Add crontabs
for FILE in "${selected_files[@]}"; do
if crontab -l 2>/dev/null | grep -q $FILE; then
echo -e "${LIGHT_BLUE}[i] [$FILE] Crontab already exists. Removing...${NC}"
crontab -l | grep -v $FILE | crontab -
fi
echo -e "${LIGHT_BLUE}[i] [$FILE] Adding crontab...${NC}"
if [ "$FILE" == "clean.sh" ]; then
(crontab -l 2>/dev/null; echo "0 0 * * * ${CURRENT_WORKDIR}/$FILE > /tmp/clean.log") | crontab -
elif [ "$FILE" == "backup.sh" ]; then
(crontab -l 2>/dev/null; echo "0 1 * * * ${CURRENT_WORKDIR}/$FILE > /tmp/backup.log") | crontab -
elif [ "$FILE" == "docker-updater.sh" ]; then
(crontab -l 2>/dev/null; echo "0 3 */4 * * ${CURRENT_WORKDIR}/$FILE > /tmp/docker-updater.log") | crontab -
else
echo -e "${YELLOW}[w] [$FILE] Warning: Crontab specific schedule not setup.${NC}" >&2
(crontab -l 2>/dev/null; echo "0 0 * * * ${CURRENT_WORKDIR}/$FILE" > /tmp/$FILE.log) | crontab -
fi
echo -e "${GREEN}[✓] [$FILE] Crontab added, double-checking set up...${NC}"
if ! crontab -l | grep -q $FILE; then
echo -e "${RED}[x] [$FILE] Error: Crontab was not set up.${NC}" >&2
exit 1
fi
echo -e "${GREEN}[✓] [$FILE] Crontab confirmed.${NC}"
done
echo -e "${GREEN}[✓] Crontabs all set up.${NC}"
echo -e "${GREEN}[✓] All done.${NC}"