commit 209839be44dc501cbe16440208a11439ef64e438 Author: Jose Ivanchechen Date: Fri Dec 27 23:11:17 2024 -0300 initial diff --git a/.gitea/workflows/main.yaml b/.gitea/workflows/main.yaml new file mode 100644 index 0000000..ad2a7aa --- /dev/null +++ b/.gitea/workflows/main.yaml @@ -0,0 +1,15 @@ +name: Scripts Testing +on: [push] + +jobs: + scripts-download-test: + runs-on: ubuntu-latest + steps: + - name: Check out repository code + uses: actions/checkout@v4 + - name: List files in the repository + run: | + ls ${{ gitea.workspace }} + - name: Execute download script bash + run: | + bash ${{ gitea.workspace }}/scripts-download.sh diff --git a/backup.sh b/backup.sh new file mode 100644 index 0000000..b307a57 --- /dev/null +++ b/backup.sh @@ -0,0 +1,66 @@ +#!/bin/bash + +### AUTO-UPDATER ### +# Variables +SERVER_FILE="http://h.local/backup.sh" +SERVER_OK=1 + +# Check if the server file exists +curl -s --head $SERVER_FILE | head -n 1 | grep "HTTP/1.[01] [23].." > /dev/null + +if [ $? -ne 0 ]; then + echo "Error: $SERVER_FILE not found." >&2 + SERVER_OK=0 +fi + +if [ $SERVER_OK -eq 1 ]; then + echo "Running auto-update..." + + # Compare the local and server files sha256sum to check if an update is needed + LOCAL_SHA256=$(sha256sum backup.sh | awk '{print $1}') + SERVER_SHA256=$(curl -s $SERVER_FILE | sha256sum | awk '{print $1}') + + if [ "$LOCAL_SHA256" != "$SERVER_SHA256" ]; then + echo "Updating backup.sh..." + curl -s -o backup.sh $SERVER_FILE + echo "backup.sh updated." + + chmod +x backup.sh + echo "Permissions set up." + + echo "Running updated backup.sh..." + ./backup.sh + exit 0 + else + echo "backup.sh is up to date.." + fi +fi + +#################### + +# Variables +SOURCE_DIR="/root/docker" +BACKUP_FILE="/tmp/docker_backup_$(date +%Y%m%d%H%M%S).tar.gz" +REMOTE_USER="ivanch" +REMOTE_HOST="nas.local" +REMOTE_DIR="/mnt/Storage/Backups/Docker/$(cat /etc/hostname)" + +# Create a compressed backup file +zip -r $BACKUP_FILE $SOURCE_DIR + +# Check if remote path exists +ssh $REMOTE_USER@$REMOTE_HOST "mkdir -p $REMOTE_DIR" + +# Transfer the backup file to the remote server +scp $BACKUP_FILE $REMOTE_USER@$REMOTE_HOST:$REMOTE_DIR + +# Remove the backup file +rm $BACKUP_FILE + +# Erase last 7 days backups from remote server +ssh $REMOTE_USER@$REMOTE_HOST "find $REMOTE_DIR -type f -name 'docker_backup_*' -mtime +7 -exec rm {} \;" + +# Exit +echo "Backup completed successfully" +exit 0 + diff --git a/clean.sh b/clean.sh new file mode 100644 index 0000000..15a3815 --- /dev/null +++ b/clean.sh @@ -0,0 +1,74 @@ +#!/bin/bash + +### AUTO-UPDATER ### +# Variables +SERVER_FILE="http://h.local/clean.sh" +SERVER_OK=1 + +# Check if the server file exists +curl -s --head $SERVER_FILE | head -n 1 | grep "HTTP/1.[01] [23].." > /dev/null + +if [ $? -ne 0 ]; then + echo "Error: $SERVER_FILE not found." >&2 + SERVER_OK=0 +fi + +if [ $SERVER_OK -eq 1 ]; then + echo "Running auto-update..." + + # Compare the local and server files sha256sum to check if an update is needed + LOCAL_SHA256=$(sha256sum clean.sh | awk '{print $1}') + SERVER_SHA256=$(curl -s $SERVER_FILE | sha256sum | awk '{print $1}') + + if [ "$LOCAL_SHA256" != "$SERVER_SHA256" ]; then + echo "Updating clean.sh..." + curl -s -o clean.sh $SERVER_FILE + echo "clean.sh updated." + + chmod +x clean.sh + echo "Permissions set up." + + echo "Running updated clean.sh..." + ./clean.sh + exit 0 + else + echo "clean.sh is up to date.." + fi +fi + +#################### + +# Run Docker system prune +echo "Running Docker system prune..." +docker image prune -af +docker system prune -af + +# Clean APK cache from Alpine or apt for Debian +if [ -x "$(command -v apk)" ]; then + echo "Cleaning APK cache..." + rm -rf /var/cache/apk/* + apk cache clean + apk update +fi + +if [ -x "$(command -v apt)" ]; then + echo "Cleaning apt cache..." + apt-get clean + apt-get autoclean + apt-get update +fi + +# Clean system caches +echo "Cleaning system caches..." +rm -rf /var/cache/* +rm -rf /tmp/* + +# General system maintenance +echo "Performing general system maintenance..." +sync; echo 3 > /proc/sys/vm/drop_caches + +# Remove old logs +echo "Removing old logs..." +find /var/log -type f -name "*.log" -mtime +30 -delete + +echo "Maintenance completed." \ No newline at end of file diff --git a/scripts-download.sh b/scripts-download.sh new file mode 100644 index 0000000..039b36c --- /dev/null +++ b/scripts-download.sh @@ -0,0 +1,182 @@ +#!/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}" \ No newline at end of file