Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ba553036fc | |||
| 1151b16e52 | |||
|
|
9abf312833 | ||
|
|
8be56308ba | ||
|
|
35727a02b2 | ||
|
|
9fd8824ddd | ||
| a87f743c9f | |||
| c0995d9c4c | |||
| e5aa060d94 | |||
| fd22bbe72b | |||
|
|
c74434e950 | ||
|
|
7d991b0341 |
@@ -1,3 +0,0 @@
|
||||
# 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.
|
||||
89
docker-cleanup.sh
Normal file
89
docker-cleanup.sh
Normal file
@@ -0,0 +1,89 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Script: docker-cleanup.sh
|
||||
# Description: Cleans up unused Docker images, volumes, networks, and builder cache.
|
||||
# Preserves stopped containers.
|
||||
# Calculates reclaimed space and logs it to syslog.
|
||||
# Dependencies: docker, bc, grep (with PCRE support)
|
||||
|
||||
USAGE_TAG="docker-cleanup"
|
||||
|
||||
# Logging functions
|
||||
log_info() {
|
||||
logger -t "$USAGE_TAG" -p user.info "$1"
|
||||
}
|
||||
|
||||
log_error() {
|
||||
logger -t "$USAGE_TAG" -p user.error "$1"
|
||||
}
|
||||
|
||||
# Check for 'bc' dependency
|
||||
if ! command -v bc &> /dev/null; then
|
||||
log_error "Error: 'bc' is not installed. Please install it (apt install bc) to calculate reclaimed space."
|
||||
# Fail safe: don't run blindly if we can't calculate/log properly, or just run without logging space?
|
||||
# User requested logging to syslog, assuming they want the space report.
|
||||
exit 1
|
||||
fi
|
||||
|
||||
log_info "Starting Docker cleanup (Images, Volumes, Networks, Builder)..."
|
||||
|
||||
# Initialize output variable
|
||||
OUTPUT=""
|
||||
|
||||
# 1. Prune Images (all unused images, not just dangling) with input 'y' just in case, though -f usually covers it
|
||||
# Note: 'image prune -a' removes images not used by ANY container (including stopped ones).
|
||||
CMD_OUTPUT=$(docker image prune -a -f 2>&1)
|
||||
OUTPUT="${OUTPUT}${CMD_OUTPUT}"$'\n'
|
||||
|
||||
# 2. Prune Volumes
|
||||
CMD_OUTPUT=$(docker volume prune -f 2>&1)
|
||||
OUTPUT="${OUTPUT}${CMD_OUTPUT}"$'\n'
|
||||
|
||||
# 3. Prune Networks
|
||||
CMD_OUTPUT=$(docker network prune -f 2>&1)
|
||||
OUTPUT="${OUTPUT}${CMD_OUTPUT}"$'\n'
|
||||
|
||||
# 4. Prune Builder Cache
|
||||
CMD_OUTPUT=$(docker builder prune -f 2>&1)
|
||||
OUTPUT="${OUTPUT}${CMD_OUTPUT}"$'\n'
|
||||
|
||||
# Check for critical failures (though docker commands might succeed partially)
|
||||
# We proceed to calculate space regardless.
|
||||
|
||||
# Extract reclaimed space amounts using grep with Perl-compatible regex.
|
||||
RECLAIMED_SPACES=$(echo "$OUTPUT" | grep -Po '(?<=Total reclaimed space: )[0-9.]+[A-Z]+' | grep -v '^0B$')
|
||||
|
||||
# Initialize total reclaimed space counter
|
||||
TOTAL_RECLAIMED=0
|
||||
|
||||
# Loop through each reclaimed space value
|
||||
for SPACE in $RECLAIMED_SPACES; do
|
||||
# Extract the unit (KB, MB, GB)
|
||||
UNIT=${SPACE: -2}
|
||||
# Extract the numeric value
|
||||
VALUE=${SPACE%${UNIT}}
|
||||
|
||||
# Normalize to GB
|
||||
case $UNIT in
|
||||
kB|KB) VAL_IN_GB=$(echo "$VALUE / 1024 / 1024" | bc -l) ;;
|
||||
mB|MB) VAL_IN_GB=$(echo "$VALUE / 1024" | bc -l) ;;
|
||||
gB|GB) VAL_IN_GB=$VALUE ;;
|
||||
*) VAL_IN_GB=0 ;;
|
||||
esac
|
||||
|
||||
# Add to total
|
||||
TOTAL_RECLAIMED=$(echo "$TOTAL_RECLAIMED + $VAL_IN_GB" | bc -l)
|
||||
done
|
||||
|
||||
# Check if we reclaimed anything significant (greater than 0)
|
||||
IS_POSITIVE=$(echo "$TOTAL_RECLAIMED > 0" | bc -l)
|
||||
|
||||
if [ "$IS_POSITIVE" -eq 1 ]; then
|
||||
# Format to 3 decimal places
|
||||
FORMATTED_TOTAL=$(printf "%.3f" "$TOTAL_RECLAIMED")
|
||||
log_info "Cleanup finished. Total reclaimed space: ${FORMATTED_TOTAL} GB"
|
||||
else
|
||||
log_info "Cleanup finished. No significant space was reclaimed."
|
||||
fi
|
||||
|
||||
exit 0
|
||||
@@ -1,45 +0,0 @@
|
||||
#!/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
|
||||
71
vaultwarden-backup.sh
Normal file
71
vaultwarden-backup.sh
Normal file
@@ -0,0 +1,71 @@
|
||||
#!/bin/bash
|
||||
set -euo pipefail
|
||||
|
||||
# --- Settings ---
|
||||
# Container Name
|
||||
CONTAINER_NAME="vaultwarden"
|
||||
|
||||
# Host path mapped to /data inside the container
|
||||
BACKUP_SOURCE_DIR="/home/mbuz/docker/vaultwarden"
|
||||
|
||||
# Destination for backups
|
||||
BACKUP_DEST_DIR="/mnt/media/vault_backups"
|
||||
|
||||
# Retention Policy: Delete backups older than N days
|
||||
RETENTION_DAYS=30
|
||||
|
||||
# Syslog Tag
|
||||
LOG_TAG="vaultwarden-backup"
|
||||
# --- End of Settings ---
|
||||
|
||||
# Logging helpers
|
||||
log_info() { logger -t "$LOG_TAG" -p user.info "$1"; }
|
||||
log_error() { logger -t "$LOG_TAG" -p user.error "$1"; }
|
||||
|
||||
log_info "Starting Vaultwarden backup..."
|
||||
|
||||
# 1. Mount Check
|
||||
if [ ! -d "$BACKUP_DEST_DIR" ]; then
|
||||
log_info "Destination directory not found. Attempting to mount..."
|
||||
sudo mount -a || true
|
||||
sleep 2
|
||||
if [ ! -d "$BACKUP_DEST_DIR" ]; then
|
||||
log_error "ERROR: Destination '$BACKUP_DEST_DIR' unavailable after mount attempt."
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# 2. Backup Execution
|
||||
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
|
||||
FILENAME="db_backup_${TIMESTAMP}.sqlite3"
|
||||
|
||||
log_info "Creating backup: $FILENAME"
|
||||
|
||||
# FALLBACK METHOD: Since 'sqlite3' is missing in the container, we simply copy the DB file.
|
||||
# Note: This is *technically* not transaction-safe if writes happen exactly during copy,
|
||||
# but for a personal vault, it's usually acceptable (WAL mode helps).
|
||||
# Ideally, install sqlite3 in container or use vaultwarden/server:alpine which might have it.
|
||||
|
||||
if [ -f "$BACKUP_SOURCE_DIR/db.sqlite3" ]; then
|
||||
cp "$BACKUP_SOURCE_DIR/db.sqlite3" "$BACKUP_DEST_DIR/$FILENAME"
|
||||
|
||||
# Also copy the WAL and SHM files if they exist (crucial for WAL mode integrity)
|
||||
if [ -f "$BACKUP_SOURCE_DIR/db.sqlite3-wal" ]; then
|
||||
cp "$BACKUP_SOURCE_DIR/db.sqlite3-wal" "$BACKUP_DEST_DIR/db_backup_${TIMESTAMP}.sqlite3-wal"
|
||||
fi
|
||||
if [ -f "$BACKUP_SOURCE_DIR/db.sqlite3-shm" ]; then
|
||||
cp "$BACKUP_SOURCE_DIR/db.sqlite3-shm" "$BACKUP_DEST_DIR/db_backup_${TIMESTAMP}.sqlite3-shm"
|
||||
fi
|
||||
|
||||
log_info "SUCCESS: Backup created at $BACKUP_DEST_DIR/$FILENAME"
|
||||
else
|
||||
log_error "ERROR: Source DB file '$BACKUP_SOURCE_DIR/db.sqlite3' not found."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 3. Cleanup / Rotation
|
||||
log_info "Cleaning up backups older than $RETENTION_DAYS days..."
|
||||
find "$BACKUP_DEST_DIR" -name "db_backup_*.sqlite3*" -type f -mtime +$RETENTION_DAYS -delete
|
||||
|
||||
log_info "Backup process completed."
|
||||
exit 0
|
||||
Reference in New Issue
Block a user