1
+ #! /bin/bash
2
+
3
+ # Script to clean up old timestamped builds from Cloudflare R2
4
+ # This will remove old .txz files with the pattern dynamix.unraid.net-YYYY.MM.DD.HHMM.txz
5
+
6
+ set -e
7
+
8
+ # Colors for output
9
+ RED=' \033[0;31m'
10
+ GREEN=' \033[0;32m'
11
+ YELLOW=' \033[1;33m'
12
+ BLUE=' \033[0;34m'
13
+ NC=' \033[0m' # No Color
14
+
15
+ echo -e " ${YELLOW} 🧹 Cloudflare Old Build Cleanup Script${NC} "
16
+ echo " This will delete old timestamped .txz builds from the preview bucket"
17
+ echo " "
18
+
19
+ # Check for required environment variables
20
+ if [ -z " $CF_ACCESS_KEY_ID " ] || [ -z " $CF_SECRET_ACCESS_KEY " ] || [ -z " $CF_ENDPOINT " ] || [ -z " $CF_BUCKET_PREVIEW " ]; then
21
+ echo -e " ${RED} ❌ Error: Missing required environment variables${NC} "
22
+ echo " Please set the following environment variables:"
23
+ echo " - CF_ACCESS_KEY_ID"
24
+ echo " - CF_SECRET_ACCESS_KEY"
25
+ echo " - CF_ENDPOINT"
26
+ echo " - CF_BUCKET_PREVIEW"
27
+ exit 1
28
+ fi
29
+
30
+ # Configure AWS CLI for Cloudflare R2
31
+ export AWS_ACCESS_KEY_ID=" $CF_ACCESS_KEY_ID "
32
+ export AWS_SECRET_ACCESS_KEY=" $CF_SECRET_ACCESS_KEY "
33
+ export AWS_DEFAULT_REGION=" auto"
34
+
35
+ echo " Endpoint: $CF_ENDPOINT "
36
+ echo " Bucket: $CF_BUCKET_PREVIEW "
37
+ echo " "
38
+
39
+ # Optional: specify number of days to keep (default: 7)
40
+ KEEP_DAYS=${1:- 7}
41
+ echo -e " ${BLUE} Keeping builds from the last ${KEEP_DAYS} days${NC} "
42
+ echo " "
43
+
44
+ # Calculate cutoff date
45
+ if [[ " $OSTYPE " == " darwin" * ]]; then
46
+ # macOS
47
+ CUTOFF_DATE=$( date -v -${KEEP_DAYS} d +" %Y.%m.%d" )
48
+ else
49
+ # Linux
50
+ CUTOFF_DATE=$( date -d " ${KEEP_DAYS} days ago" +" %Y.%m.%d" )
51
+ fi
52
+
53
+ echo " Cutoff date: ${CUTOFF_DATE} (will delete builds older than this)"
54
+ echo " "
55
+
56
+ # List all timestamped TXZ files in the unraid-api directory
57
+ echo -e " ${YELLOW} 📋 Scanning for old builds...${NC} "
58
+
59
+ # Get all .txz files matching the pattern
60
+ ALL_FILES=$( aws s3 ls " s3://${CF_BUCKET_PREVIEW} /unraid-api/" --endpoint-url " $CF_ENDPOINT " --recursive | \
61
+ grep -E " dynamix\.unraid\.net-[0-9]{4}\.[0-9]{2}\.[0-9]{2}\.[0-9]{4}\.txz" | \
62
+ awk ' {print $4}' || true)
63
+
64
+ if [ -z " $ALL_FILES " ]; then
65
+ echo -e " ${GREEN} ✅ No timestamped builds found${NC} "
66
+ exit 0
67
+ fi
68
+
69
+ # Filter files older than cutoff
70
+ OLD_FILES=" "
71
+ KEEP_FILES=" "
72
+ TOTAL_COUNT=0
73
+ OLD_COUNT=0
74
+
75
+ while IFS= read -r file; do
76
+ (( TOTAL_COUNT++ ))
77
+ # Extract date from filename (format: YYYY.MM.DD.HHMM)
78
+ if [[ $file =~ ([0-9]{4}\. [0-9]{2}\. [0-9]{2})\. [0-9]{4}\. txz ]]; then
79
+ FILE_DATE=" ${BASH_REMATCH[1]} "
80
+
81
+ # Compare dates (string comparison works for YYYY.MM.DD format)
82
+ if [[ " $FILE_DATE " < " $CUTOFF_DATE " ]]; then
83
+ OLD_FILES=" ${OLD_FILES}${file} \n"
84
+ (( OLD_COUNT++ ))
85
+ else
86
+ KEEP_FILES=" ${KEEP_FILES}${file} \n"
87
+ fi
88
+ fi
89
+ done <<< " $ALL_FILES"
90
+
91
+ echo " Found ${TOTAL_COUNT} total timestamped builds"
92
+ echo " Will delete ${OLD_COUNT} old builds"
93
+ echo " Will keep $(( TOTAL_COUNT - OLD_COUNT)) recent builds"
94
+ echo " "
95
+
96
+ if [ " $OLD_COUNT " -eq 0 ]; then
97
+ echo -e " ${GREEN} ✅ No old builds to delete${NC} "
98
+ exit 0
99
+ fi
100
+
101
+ # Show sample of files to be deleted
102
+ echo -e " ${YELLOW} Sample of files to be deleted:${NC} "
103
+ echo -e " $OLD_FILES " | head -5
104
+ if [ " $OLD_COUNT " -gt 5 ]; then
105
+ echo " ... and $(( OLD_COUNT - 5 )) more"
106
+ fi
107
+ echo " "
108
+
109
+ # Confirmation prompt
110
+ read -p " Are you sure you want to delete these ${OLD_COUNT} old builds? (yes/no): " -r
111
+ echo " "
112
+
113
+ if [[ ! $REPLY =~ ^[Yy]es$ ]]; then
114
+ echo -e " ${YELLOW} ⚠️ Cleanup cancelled${NC} "
115
+ exit 0
116
+ fi
117
+
118
+ # Delete old files
119
+ DELETED=0
120
+ FAILED=0
121
+
122
+ echo -e " ${YELLOW} 🗑️ Deleting old builds...${NC} "
123
+ while IFS= read -r file; do
124
+ if [ -n " $file " ]; then
125
+ echo -n " Deleting $( basename " $file " ) ... "
126
+
127
+ if aws s3 rm " s3://${CF_BUCKET_PREVIEW} /${file} " \
128
+ --endpoint-url " $CF_ENDPOINT " \
129
+ > /dev/null 2>&1 ; then
130
+ echo -e " ${GREEN} ✓${NC} "
131
+ (( DELETED++ ))
132
+ else
133
+ echo -e " ${RED} ✗${NC} "
134
+ (( FAILED++ ))
135
+ fi
136
+ fi
137
+ done <<< " $(echo -e " $OLD_FILES " )"
138
+
139
+ echo " "
140
+ echo -e " ${GREEN} 🎉 Cleanup complete!${NC} "
141
+ echo " - Deleted: $DELETED old build(s)"
142
+ if [ $FAILED -gt 0 ]; then
143
+ echo -e " - Failed: ${RED} $FAILED ${NC} build(s)"
144
+ fi
145
+
146
+ # Show remaining recent builds
147
+ echo " "
148
+ echo -e " ${BLUE} 📦 Recent builds kept:${NC} "
149
+ echo -e " $KEEP_FILES " | head -5
150
+ KEEP_COUNT=$( echo -e " $KEEP_FILES " | grep -c . || echo 0)
151
+ if [ " $KEEP_COUNT " -gt 5 ]; then
152
+ echo " ... and $(( KEEP_COUNT - 5 )) more"
153
+ fi
0 commit comments