workflow/backup-management/round.sh
2025-07-19 17:15:10 +02:00

137 lines
3.9 KiB
Bash
Executable file

#!/bin/bash
# Script to check if mounted disks have up-to-date borg2 backups
# Compares the last modification date of borg_archives/borg2 on each disk
# with /home/poule/borg_archives/borg2
# Set colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color
echo "Checking mounted disks for borg2 backup status..."
# Reference directory
REFERENCE_DIR="/home/poule/borg_archives/borg2"
# Check if reference directory exists
if [ ! -d "$REFERENCE_DIR" ]; then
echo -e "${RED}Error: Reference directory $REFERENCE_DIR does not exist.${NC}"
exit 1
fi
# Get current time
CURRENT_TIME=$(date +%s)
# Calculate time 24 hours ago
TIME_24H_AGO=$((CURRENT_TIME - 86400))
# Get only mount points in /media/$USER/
MOUNT_POINTS=$(df -h | grep "/media/$USER/" | awk '{print $6}')
# Initialize arrays for results
OUTDATED_DISKS=()
OUTDATED_TIMES=()
UP_TO_DATE_DISKS=()
NO_BORG_DISKS=()
LOW_SPACE_DISKS=()
LOW_SPACE_VALUES=()
# Function to calculate time difference in years, months, days
calculate_time_diff() {
local timestamp=$1
local now=$(date +%s)
local diff=$((now - timestamp))
# Calculate years, months, days
local days=$((diff / 86400))
local years=$((days / 365))
local remaining_days=$((days % 365))
local months=$((remaining_days / 30))
remaining_days=$((remaining_days % 30))
echo "${years}y ${months}m ${remaining_days}d"
}
# Check each mount point
for MOUNT in $MOUNT_POINTS; do
BORG_DIR="$MOUNT/borg_archives/borg2"
# Skip the reference directory itself
if [ "$MOUNT" == "/home" ] || [ "$MOUNT" == "/" ]; then
continue
fi
echo -e "${YELLOW}Checking $MOUNT...${NC}"
# Check available disk space
AVAILABLE_SPACE=$(df -BG "$MOUNT" | awk 'NR==2 {print $4}' | sed 's/G//')
if [ "$AVAILABLE_SPACE" -lt 10 ]; then
echo -e " ${RED}! Low disk space: ${AVAILABLE_SPACE}GB available${NC}"
LOW_SPACE_DISKS+=("$MOUNT")
LOW_SPACE_VALUES+=("$AVAILABLE_SPACE")
fi
# Check if borg_archives/borg2 exists on this mount
if [ -d "$BORG_DIR" ]; then
# Get last modification time of the borg directory
BORG_TIME=$(stat -c %Y "$BORG_DIR")
# Compare times - consider up-to-date if modified within the last 24 hours
if [ "$BORG_TIME" -ge "$TIME_24H_AGO" ]; then
echo -e " ${GREEN}✓ Backup is up to date${NC}"
UP_TO_DATE_DISKS+=("$MOUNT")
else
echo -e " ${RED}✗ Backup is outdated${NC}"
OUTDATED_DISKS+=("$MOUNT")
OUTDATED_TIMES+=("$BORG_TIME")
fi
else
echo -e " ${YELLOW}! No borg2 backup directory found${NC}"
NO_BORG_DISKS+=("$MOUNT")
fi
done
# Print summary
echo -e "\n${YELLOW}=== Backup Status Summary ===${NC}"
if [ ${#UP_TO_DATE_DISKS[@]} -gt 0 ]; then
echo -e "\n${GREEN}Up-to-date disks:${NC}"
for DISK in "${UP_TO_DATE_DISKS[@]}"; do
echo " - $DISK"
done
fi
if [ ${#OUTDATED_DISKS[@]} -gt 0 ]; then
echo -e "\n${RED}Outdated disks (need backup):${NC}"
for i in "${!OUTDATED_DISKS[@]}"; do
DISK="${OUTDATED_DISKS[$i]}"
BORG_TIME="${OUTDATED_TIMES[$i]}"
FORMATTED_DATE=$(date -d "@$BORG_TIME" "+%Y-%m-%d %H:%M:%S")
TIME_DIFF=$(calculate_time_diff "$BORG_TIME")
echo " - $DISK (Last modified: $FORMATTED_DATE, Age: $TIME_DIFF)"
done
fi
if [ ${#NO_BORG_DISKS[@]} -gt 0 ]; then
echo -e "\n${YELLOW}Disks without borg2 backup directory:${NC}"
for DISK in "${NO_BORG_DISKS[@]}"; do
echo " - $DISK"
done
fi
if [ ${#LOW_SPACE_DISKS[@]} -gt 0 ]; then
echo -e "\n${RED}Disks with low space (less than 10GB free):${NC}"
for i in "${!LOW_SPACE_DISKS[@]}"; do
DISK="${LOW_SPACE_DISKS[$i]}"
SPACE="${LOW_SPACE_VALUES[$i]}"
echo " - $DISK (${SPACE}GB available)"
done
fi
# Exit with error code if there are outdated disks
if [ ${#OUTDATED_DISKS[@]} -gt 0 ]; then
exit 1
else
exit 0
fi