-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleanup-old-backups.sh
More file actions
106 lines (87 loc) · 2.73 KB
/
cleanup-old-backups.sh
File metadata and controls
106 lines (87 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/bin/bash
#######################################
# Docker Backup Cleanup Script
# Manages backup retention and removes old backups
#######################################
set -euo pipefail
#######################################
# OS Detection
#######################################
detect_os() {
if [[ -f /etc/os-release ]]; then
. /etc/os-release
OS_NAME="$NAME"
OS_ID="$ID"
OS_VERSION="$VERSION_ID"
case "$OS_ID" in
debian) OS_TYPE="debian" ;;
ubuntu) OS_TYPE="ubuntu" ;;
scale|truenas) OS_TYPE="truenas" ;;
proxmox) OS_TYPE="proxmox" ;;
*)
if [[ -f /etc/version ]] && grep -q "TrueNAS" /etc/version 2>/dev/null; then
OS_TYPE="truenas"
elif [[ -f /etc/debian_version ]]; then
OS_TYPE="debian"
else
OS_TYPE="unknown"
fi
;;
esac
else
OS_TYPE="unknown"
OS_NAME="Unknown"
fi
export OS_TYPE OS_NAME
}
detect_os
# Configuration
BACKUP_BASE="/mnt/backup/docker-backups"
RETENTION_DAYS=30 # Keep backups for this many days
LOG_FILE="/var/log/docker-backup-cleanup.log"
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
log() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $*" | tee -a "$LOG_FILE"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $*" | tee -a "$LOG_FILE"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $*" | tee -a "$LOG_FILE"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $*" | tee -a "$LOG_FILE"
}
main() {
log "========================================="
log "Starting backup cleanup"
log "Retention: $RETENTION_DAYS days"
log "========================================="
if [[ ! -d "$BACKUP_BASE" ]]; then
log_error "Backup directory not found: $BACKUP_BASE"
exit 1
fi
local total_removed=0
local total_size=0
# Find and remove old backup directories
while IFS= read -r backup_dir; do
local size=$(du -sb "$backup_dir" | cut -f1)
log "Removing old backup: $backup_dir ($(numfmt --to=iec-i --suffix=B $size))"
if rm -rf "$backup_dir"; then
((total_removed++))
((total_size+=size))
else
log_error "Failed to remove: $backup_dir"
fi
done < <(find "$BACKUP_BASE" -mindepth 2 -maxdepth 2 -type d -mtime +$RETENTION_DAYS)
log "========================================="
log "Cleanup Summary:"
log "Directories removed: $total_removed"
log "Space freed: $(numfmt --to=iec-i --suffix=B $total_size)"
log "========================================="
}
main "$@"