server-scripts/scripts-download.sh

177 lines
5.2 KiB
Bash
Raw Normal View History

2024-12-27 23:11:17 -03:00
#!/bin/bash
# Usage:
2024-12-27 23:32:53 -03:00
## curl -sSL https://git.ivanch.me/ivanch/server-scripts/raw/branch/main/scripts-download.sh | bash
2024-12-27 23:11:17 -03:00
# 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'
2024-12-27 23:32:53 -03:00
FILES_URL="https://git.ivanch.me/ivanch/server-scripts/raw/branch/main"
2024-12-27 23:44:46 -03:00
echo -e "\r${LIGHT_BLUE}[i] Running scripts-download.sh"
2024-12-27 23:11:17 -03:00
# 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")
2024-12-27 23:11:17 -03:00
# Check connection with the server for all files
echo -e "${GREY}[i] Checking connection with the server..."
for FILE in "${FILES[@]}"; do
2024-12-27 23:44:46 -03:00
curl -s --head "$FILES_URL/$FILE" | head -n 1 | grep -E "HTTP/[12] [23].." > /dev/null
2024-12-27 23:11:17 -03:00
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
2024-12-27 23:32:53 -03:00
curl -s -o "./$FILE" "$FILES_URL/$FILE"
2024-12-27 23:11:17 -03:00
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}"
2024-12-28 21:13:05 -03:00
if [ "$FILE" == "clean.sh" ]; then
2024-12-29 15:38:04 -03:00
(crontab -l 2>/dev/null; echo "0 0 * * * ${CURRENT_WORKDIR}/$FILE > /dev/null") | crontab -
2024-12-28 21:13:05 -03:00
elif [ "$FILE" == "backup.sh" ]; then
2024-12-29 15:38:04 -03:00
(crontab -l 2>/dev/null; echo "0 0 * * * ${CURRENT_WORKDIR}/$FILE > /dev/null") | crontab -
2024-12-28 21:13:05 -03:00
elif [ "$FILE" == "docker-updater.sh" ]; then
2024-12-29 15:38:04 -03:00
(crontab -l 2>/dev/null; echo "0 3 */4 * * ${CURRENT_WORKDIR}/$FILE > /dev/null") | crontab -
2024-12-28 21:13:05 -03:00
else
echo -e "${YELLOW}[w] [$FILE] Warning: Crontab specific schedule not setup.${NC}" >&2
2024-12-29 15:38:04 -03:00
(crontab -l 2>/dev/null; echo "0 0 * * * ${CURRENT_WORKDIR}/$FILE" > /dev/null) | crontab -
2024-12-28 21:13:05 -03:00
fi
2024-12-27 23:11:17 -03:00
echo -e "${GREEN}[✓] [$FILE] Crontab added, double-checking set up...${NC}"
2024-12-27 23:11:17 -03:00
if ! crontab -l | grep -q $FILE; then
echo -e "${RED}[x] [$FILE] Error: Crontab was not set up.${NC}" >&2
exit 1
fi
2024-12-27 23:11:17 -03:00
echo -e "${GREEN}[✓] [$FILE] Crontab confirmed.${NC}"
fi
done
2024-12-27 23:11:17 -03:00
echo -e "${GREEN}[✓] Crontabs all set up.${NC}"
2024-12-27 23:11:17 -03:00
echo -e "${GREEN}[✓] All done.${NC}"