scripts v2.0
All checks were successful
Check scripts syntax / check-scripts-syntax (push) Successful in 35s
All checks were successful
Check scripts syntax / check-scripts-syntax (push) Successful in 35s
This commit is contained in:
@@ -1,193 +1,292 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Usage:
|
||||
## curl -sSL https://git.ivanch.me/ivanch/server-scripts/raw/branch/main/scripts-download.sh | 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'
|
||||
set -euo pipefail
|
||||
|
||||
FILES_URL="https://git.ivanch.me/ivanch/server-scripts/raw/branch/main"
|
||||
#==============================================================================
|
||||
# CONFIGURATION
|
||||
#==============================================================================
|
||||
|
||||
echo -e "\r${LIGHT_BLUE}[i] Running scripts-download.sh"
|
||||
# Color definitions for output formatting
|
||||
readonly RED='\033[1;31m'
|
||||
readonly GREEN='\033[1;32m'
|
||||
readonly NC='\033[0m'
|
||||
readonly LIGHT_BLUE='\033[1;34m'
|
||||
readonly LIGHT_RED='\033[1;31m'
|
||||
readonly LIGHT_GREEN='\033[1;32m'
|
||||
readonly GREY='\033[1;30m'
|
||||
readonly YELLOW='\033[1;33m'
|
||||
|
||||
# Detect OS (Debian or Alpine)
|
||||
echo -e "${GREY}[i] Detecting OS..."
|
||||
# Configuration
|
||||
readonly FILES_URL="https://git.ivanch.me/ivanch/server-scripts/raw/branch/main"
|
||||
readonly REQUIRED_PACKAGES=("zip" "unzip" "sha256sum" "curl" "crontab")
|
||||
readonly AVAILABLE_SCRIPTS=("clean.sh" "backup.sh" "docker-updater.sh")
|
||||
|
||||
DETECTED=""
|
||||
# Format: [script_name]="cron_schedule"
|
||||
declare -A CRONTAB_SCHEDULES=(
|
||||
["clean.sh"]="0 23 * * *" # Daily at 11 PM
|
||||
["backup.sh"]="30 23 * * 1,5" # Monday and Friday at 11:30 PM
|
||||
["docker-updater.sh"]="0 3 */4 * *" # Every 4 days at 3 AM
|
||||
)
|
||||
|
||||
if [ -x "$(command -v apk)" ]; then
|
||||
DETECTED="Alpine"
|
||||
fi
|
||||
#==============================================================================
|
||||
# UTILITY FUNCTIONS
|
||||
#==============================================================================
|
||||
|
||||
if [ -x "$(command -v apt)" ]; then
|
||||
DETECTED="Debian"
|
||||
fi
|
||||
# Print formatted log messages
|
||||
log_info() { echo -e "${GREY}[i] $1${NC}"; }
|
||||
log_success() { echo -e "${GREEN}[✓] $1${NC}"; }
|
||||
log_warning() { echo -e "${YELLOW}[!] $1${NC}"; }
|
||||
log_error() { echo -e "${RED}[x] $1${NC}" >&2; }
|
||||
log_step() { echo -e "${LIGHT_BLUE}[i] $1${NC}"; }
|
||||
|
||||
if [ -z "$DETECTED" ]; then
|
||||
echo -e "${RED}[x] Error: OS not supported.${NC}" >&2
|
||||
# Exit with error message
|
||||
die() {
|
||||
log_error "$1"
|
||||
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
|
||||
# Check if a command exists
|
||||
command_exists() {
|
||||
command -v "$1" >/dev/null 2>&1
|
||||
}
|
||||
|
||||
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
|
||||
# Check if a process is running
|
||||
process_running() {
|
||||
pgrep "$1" >/dev/null 2>&1
|
||||
}
|
||||
|
||||
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}"
|
||||
#==============================================================================
|
||||
# MAIN FUNCTIONS
|
||||
#==============================================================================
|
||||
|
||||
# Detect the operating system
|
||||
detect_operating_system() {
|
||||
log_info "Detecting operating system..."
|
||||
|
||||
if command_exists apk; then
|
||||
echo "Alpine"
|
||||
elif command_exists apt; then
|
||||
echo "Debian"
|
||||
else
|
||||
die "Unsupported operating system. This script supports Alpine and Debian-based systems only."
|
||||
fi
|
||||
}
|
||||
|
||||
# Check for missing packages
|
||||
get_missing_packages() {
|
||||
local missing=()
|
||||
|
||||
for package in "${REQUIRED_PACKAGES[@]}"; do
|
||||
if ! command_exists "$package"; then
|
||||
missing+=("$package")
|
||||
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
|
||||
done
|
||||
|
||||
printf '%s\n' "${missing[@]}"
|
||||
}
|
||||
|
||||
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}"
|
||||
# Install packages based on the detected OS
|
||||
install_packages() {
|
||||
local os="$1"
|
||||
shift
|
||||
local packages=("$@")
|
||||
|
||||
if [[ ${#packages[@]} -eq 0 ]]; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
log_info "Installing required packages: ${packages[*]}"
|
||||
|
||||
case "$os" in
|
||||
"Alpine")
|
||||
log_info "Updating APK package index..."
|
||||
apk update >/dev/null || die "Failed to update APK package index"
|
||||
|
||||
log_info "Installing packages via APK..."
|
||||
apk add --no-cache "${packages[@]}" >/dev/null || die "Failed to install packages via APK"
|
||||
;;
|
||||
"Debian")
|
||||
log_info "Updating APT package index..."
|
||||
apt-get update -y >/dev/null || die "Failed to update APT package index"
|
||||
|
||||
log_info "Installing packages via APT..."
|
||||
apt-get install -y "${packages[@]}" >/dev/null || die "Failed to install packages via APT"
|
||||
;;
|
||||
*)
|
||||
die "Unknown operating system: $os"
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
# Verify all required packages are available
|
||||
verify_packages() {
|
||||
log_info "Verifying package installation..."
|
||||
|
||||
local missing_packages
|
||||
readarray -t missing_packages < <(get_missing_packages)
|
||||
|
||||
if [[ ${#missing_packages[@]} -gt 0 ]]; then
|
||||
die "Failed to install required packages: ${missing_packages[*]}"
|
||||
fi
|
||||
|
||||
log_success "All required packages are available"
|
||||
}
|
||||
|
||||
# Check if crontab service is running
|
||||
check_crontab_service() {
|
||||
log_info "Checking crontab service status..."
|
||||
|
||||
if ! process_running "cron"; then
|
||||
die "Crontab service is not running. Please start the cron service first."
|
||||
fi
|
||||
|
||||
log_success "Crontab service is running"
|
||||
}
|
||||
|
||||
# Prompt user to select scripts for installation
|
||||
select_scripts() {
|
||||
local selected=()
|
||||
|
||||
log_info "Available scripts for download and installation:"
|
||||
echo
|
||||
|
||||
for script in "${AVAILABLE_SCRIPTS[@]}"; do
|
||||
local schedule="${CRONTAB_SCHEDULES[$script]:-"0 0 * * *"}"
|
||||
echo -e " ${LIGHT_BLUE}$script${NC} - Schedule: ${GREY}$schedule${NC}"
|
||||
done
|
||||
|
||||
echo
|
||||
log_info "Select scripts to download and install:"
|
||||
|
||||
for script in "${AVAILABLE_SCRIPTS[@]}"; do
|
||||
read -p "Install $script? [Y/n]: " choice </dev/tty
|
||||
if [[ "$choice" =~ ^[Yy]?$ ]]; then
|
||||
selected+=("$script")
|
||||
fi
|
||||
done
|
||||
|
||||
if [[ ${#selected[@]} -eq 0 ]]; then
|
||||
die "No scripts selected. Exiting..."
|
||||
fi
|
||||
|
||||
printf '%s\n' "${selected[@]}"
|
||||
}
|
||||
|
||||
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
|
||||
FILES=("clean.sh" "backup.sh" "docker-updater.sh")
|
||||
|
||||
# Prompt user to select files to download
|
||||
selected_files=()
|
||||
echo -e "${GREY}[i] Select files to download and install on crontab:${NC} "
|
||||
for FILE in "${FILES[@]}"; do
|
||||
read -p "Do you want to download and install $FILE? [Y/n]: " choice </dev/tty
|
||||
if [[ "$choice" == "y" || "$choice" == "Y" || -z "$choice" ]]; then
|
||||
selected_files+=("$FILE")
|
||||
fi
|
||||
done
|
||||
|
||||
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..."
|
||||
# Verify server connectivity for selected scripts
|
||||
verify_server_connectivity() {
|
||||
local scripts=("$@")
|
||||
|
||||
log_info "Verifying server connectivity..."
|
||||
|
||||
for script in "${scripts[@]}"; do
|
||||
local url="$FILES_URL/$script"
|
||||
if ! curl -s --head "$url" | head -n 1 | grep -E "HTTP/[12] [23].." >/dev/null; then
|
||||
die "Script '$script' not found on server: $url"
|
||||
fi
|
||||
done
|
||||
|
||||
log_success "Server connectivity verified"
|
||||
}
|
||||
|
||||
# Download selected scripts
|
||||
for FILE in "${selected_files[@]}"; do
|
||||
curl -s -o "./$FILE" "$FILES_URL/$FILE"
|
||||
done
|
||||
download_scripts() {
|
||||
local scripts=("$@")
|
||||
|
||||
log_info "Downloading ${#scripts[@]} script(s)..."
|
||||
|
||||
for script in "${scripts[@]}"; do
|
||||
local url="$FILES_URL/$script"
|
||||
log_step "Downloading $script..."
|
||||
|
||||
if ! curl -s -o "./$script" "$url"; then
|
||||
die "Failed to download $script from $url"
|
||||
fi
|
||||
|
||||
# Set executable permissions
|
||||
chmod +x "./$script" || die "Failed to set executable permissions for $script"
|
||||
done
|
||||
|
||||
log_success "All scripts downloaded and configured"
|
||||
}
|
||||
|
||||
echo -e "${GREEN}[✓] Scripts downloaded.${NC}"
|
||||
# Setup crontab entries for selected scripts
|
||||
setup_crontab() {
|
||||
local scripts=("$@")
|
||||
local current_workdir
|
||||
current_workdir=$(pwd)
|
||||
|
||||
log_info "Setting up crontab entries..."
|
||||
|
||||
for script in "${scripts[@]}"; do
|
||||
local schedule="${CRONTAB_SCHEDULES[$script]:-"0 0 * * *"}"
|
||||
local log_file="/tmp/${script%.*}.log"
|
||||
local cron_entry="$schedule $current_workdir/$script > $log_file 2>&1"
|
||||
|
||||
log_step "Configuring crontab for $script (Schedule: $schedule)..."
|
||||
|
||||
# Remove existing crontab entry for this script
|
||||
if crontab -l 2>/dev/null | grep -q "$script"; then
|
||||
log_step "Removing existing crontab entry for $script..."
|
||||
crontab -l 2>/dev/null | grep -v "$script" | crontab - || die "Failed to remove existing crontab entry"
|
||||
fi
|
||||
|
||||
# Add new crontab entry
|
||||
(crontab -l 2>/dev/null; echo "$cron_entry") | crontab - || die "Failed to add crontab entry for $script"
|
||||
|
||||
# Verify the entry was added
|
||||
if ! crontab -l 2>/dev/null | grep -q "$script"; then
|
||||
die "Failed to verify crontab entry for $script"
|
||||
fi
|
||||
|
||||
log_success "Crontab configured for $script"
|
||||
done
|
||||
|
||||
log_success "All crontab entries configured successfully"
|
||||
}
|
||||
|
||||
CURRENT_WORKDIR=$(pwd)
|
||||
#==============================================================================
|
||||
# MAIN EXECUTION
|
||||
#==============================================================================
|
||||
|
||||
# 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 -
|
||||
main() {
|
||||
log_step "Starting Server Scripts Downloader"
|
||||
echo
|
||||
|
||||
# System detection and validation
|
||||
local detected_os
|
||||
detected_os=$(detect_operating_system)
|
||||
log_success "Detected $detected_os Linux"
|
||||
|
||||
# Package management
|
||||
local missing_packages
|
||||
readarray -t missing_packages < <(get_missing_packages)
|
||||
|
||||
if [[ ${#missing_packages[@]} -gt 0 ]]; then
|
||||
log_warning "Missing packages detected: ${missing_packages[*]}"
|
||||
install_packages "$detected_os" "${missing_packages[@]}"
|
||||
fi
|
||||
echo -e "${LIGHT_BLUE}[i] [$FILE] Adding crontab...${NC}"
|
||||
|
||||
verify_packages
|
||||
check_crontab_service
|
||||
|
||||
# Script selection and installation
|
||||
local selected_scripts
|
||||
readarray -t selected_scripts < <(select_scripts)
|
||||
|
||||
log_info "Selected scripts: ${selected_scripts[*]}"
|
||||
|
||||
verify_server_connectivity "${selected_scripts[@]}"
|
||||
download_scripts "${selected_scripts[@]}"
|
||||
setup_crontab "${selected_scripts[@]}"
|
||||
|
||||
echo
|
||||
log_success "Installation completed successfully!"
|
||||
log_info "Scripts have been downloaded to: $(pwd)"
|
||||
log_info "Crontab entries have been configured. Use 'crontab -l' to view them."
|
||||
log_info "Log files will be created in /tmp/ directory."
|
||||
}
|
||||
|
||||
if [ "$FILE" == "clean.sh" ]; then
|
||||
(crontab -l 2>/dev/null; echo "0 23 * * * ${CURRENT_WORKDIR}/$FILE > /tmp/clean.log") | crontab -
|
||||
elif [ "$FILE" == "backup.sh" ]; then
|
||||
(crontab -l 2>/dev/null; echo "30 23 * * 1,5 ${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}"
|
||||
# Execute main function
|
||||
main "$@"
|
Reference in New Issue
Block a user