From bb806c2b6c821641aaea179b2ecaf089bf6d6f68 Mon Sep 17 00:00:00 2001 From: Max Buz <79866323+xopek-by@users.noreply.github.com> Date: Sun, 2 Feb 2025 23:09:51 +0100 Subject: [PATCH] Scripts section added. Readme revorked. --- README.md | 10 ++++++++- Scripts/README.md | 3 +++ Scripts/docker_cleanup.sh | 45 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 Scripts/README.md create mode 100644 Scripts/docker_cleanup.sh diff --git a/README.md b/README.md index c340713..0d746cb 100644 --- a/README.md +++ b/README.md @@ -1 +1,9 @@ -# homelab +# Max's Homelab + +Welcome to my personal Homelab repository! This repository contains various projects that I use, have used, or plan to use in my Homelab. +At the moment I have a Lenovo P520 running as a server and I am experimenting a lot. Use with caution, I am not responsible for your data. But feel free to explore and use the resources provided to enhance your home lab setup. + +- **Docker Projects**: Pre-configured Docker containers for different applications and services. Sample configuration files for this services. +- **Scripting & Automation**: Useful scripts for automation and management tasks, Ansible playbooks. + +Contributions and suggestions are always welcome! diff --git a/Scripts/README.md b/Scripts/README.md new file mode 100644 index 0000000..c92a36a --- /dev/null +++ b/Scripts/README.md @@ -0,0 +1,3 @@ +# Important Notice + +When downloading and running scripts from the internet, it is crucial to understand what the script does. Running unknown scripts can pose security risks, such as exposing your system to malware or other malicious activities. Always review the code and ensure it comes from a trusted source before executing it. \ No newline at end of file diff --git a/Scripts/docker_cleanup.sh b/Scripts/docker_cleanup.sh new file mode 100644 index 0000000..f42f792 --- /dev/null +++ b/Scripts/docker_cleanup.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +# This script cleans up Docker resources that are not being used. Including images, containers, volumes. + +### !!! WARNING !!! ### +# This command is destructive and should be used with caution, as it permanently deletes all unused containers, images, networks, and volumes without recovery options. +# This will also delete persistent data volumes! +# Always make sure, that you have a backup of your data before running this script. + +# Script: cleanup_docker.sh +# Log file location +LOG_FILE="$(dirname "$0")/docker_cleanup.log" + +# Run cleanup command and capture output +OUTPUT=$(docker system prune -af --volumes 2>&1) + +# Extract reclaimed space amounts (excluding "Total reclaimed space: 0B" lines) +RECLAIMED_SPACES=$(echo "$OUTPUT" | grep -Po '(?<=Total reclaimed space: )[0-9.]+[A-Z]+' | grep -v '^0B$') + +# Calculate total reclaimed space +TOTAL_RECLAIMED=0 +for SPACE in $RECLAIMED_SPACES; do + UNIT=${SPACE: -2} # Extract the unit (e.g., MB, GB) + VALUE=${SPACE%${UNIT}} # Extract the numeric value + VALUE=${VALUE//,/} # Remove any commas if present + + case $UNIT in + KB) VALUE=$(echo "$VALUE / 1024 / 1024" | bc -l);; # Convert KB to GB + MB) VALUE=$(echo "$VALUE / 1024" | bc -l);; # Convert MB to GB + GB) VALUE=$VALUE;; # Already in GB + *) VALUE=0;; # Unknown unit + esac + + TOTAL_RECLAIMED=$(echo "$TOTAL_RECLAIMED + $VALUE" | bc -l) +done + +# Get the current timestamp +TIMESTAMP=$(date '+%d.%m.%Y %H:%M:%S') + +# Write the log entry only if there's something reclaimed +if (( $(echo "$TOTAL_RECLAIMED > 0" | bc -l) )); then + printf "%s - Cleanup finished. Total reclaimed space: %.3f GB\n" "$TIMESTAMP" "$TOTAL_RECLAIMED" >> "$LOG_FILE" +else + printf "%s - Cleanup finished. No space was reclaimed.\n" "$TIMESTAMP" >> "$LOG_FILE" +fi \ No newline at end of file