server-scripts/scripts-download.sh

182 lines
5.1 KiB
Bash
Raw Normal View History

2024-12-27 23:11:17 -03:00
#!/bin/bash
# Usage:
## curl -sSL https://git.ivanch.me/ivanch/server-scripts/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'
echo -ne "\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")
SERVER_URL="http://h.local/"
# 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 "$SERVER_URL/$FILE" | head -n 1 | grep "HTTP/1.[01] [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" "$SERVER_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 crontab for backup.sh
# Check if crontab for backup.sh already exists
if crontab -l 2>/dev/null | grep -q "backup.sh"; then
echo -e "${LIGHT_BLUE}[i] Crontab for backup.sh already exists. Skipping.${NC}"
else
echo -e "${LIGHT_BLUE}[i] Adding crontab for backup.sh...${NC}"
(crontab -l 2>/dev/null; echo "0 0 * * * ${CURRENT_WORKDIR}/backup.sh") | crontab -
fi
# Add crontab for clean.sh
# Check if crontab for clean.sh already exists
if crontab -l | grep -q "clean.sh"; then
echo -e "${LIGHT_BLUE}[i] Crontab for clean.sh already exists. Skipping.${NC}"
else
echo -e "${LIGHT_BLUE}[i] Adding crontab for clean.sh...${NC}"
(crontab -l 2>/dev/null; echo "0 0 * * * ${CURRENT_WORKDIR}/clean.sh") | crontab -
fi
echo -e "${GREEN}[✓] Crontab added, checking if was correctly set up...${NC}"
CRONTABS_OK=1
if ! crontab -l | grep -q "backup.sh"; then
echo -e "${RED}[x] Error: Crontab for backup.sh was not set up.${NC}" >&2
CRONTABS_OK=0
fi
if ! crontab -l | grep -q "clean.sh"; then
echo -e "${RED}[x] Error: Crontab for clean.sh was not set up.${NC}" >&2
CRONTABS_OK=0
fi
if [ $CRONTABS_OK -eq 0 ]; then
echo -e "${RED}[x] Error: Crontab was not set up correctly.${NC}" >&2
exit 1
fi
echo -e "${GREEN}[✓] Crontab set up.${NC}"
echo -e "${GREEN}[✓] All done.${NC}"