#!/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 FILES=("clean.sh" "backup.sh" "docker-updater.sh") # Check connection with the server for all files echo -e "${GREY}[i] Checking connection with the server..." for FILE in "${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 scripts for FILE in "${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..." for FILE in "${FILES[@]}"; do chmod +x "./$FILE" done echo -e "${GREEN}[✓] Permissions set up.${NC}" # Setup crontab for files echo -e "${GREY}[i] Setting up crontab..." # Add crontabs for FILE in "${FILES[@]}"; do if crontab -l 2>/dev/null | grep -q $FILE; then echo -e "${LIGHT_BLUE}[i] [$FILE] Crontab already exists. Skipping.${NC}" else 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") | crontab - elif [ "$FILE" == "backup.sh" ]; then (crontab -l 2>/dev/null; echo "0 0 * * * ${CURRENT_WORKDIR}/$FILE") | crontab - elif [ "$FILE" == "docker-updater.sh" ]; then (crontab -l 2>/dev/null; echo "0 3 */4 * * ${CURRENT_WORKDIR}/$FILE") | 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") | 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}" fi done echo -e "${GREEN}[✓] Crontabs all set up.${NC}" echo -e "${GREEN}[✓] All done.${NC}"