This commit is contained in:
Para Dox
2025-04-27 16:49:09 +07:00
parent b6f5c17a9f
commit 339af89892

View File

@@ -117,70 +117,50 @@ services:
# Install necessary packages # Install necessary packages
RUN apk add --no-cache bash coreutils sed findutils RUN apk add --no-cache bash coreutils sed findutils
# Create the prune script inline # Create the prune script inline
RUN echo '#!/bin/bash\n\ RUN <<EOF > /app/prune.sh
set -euo pipefail\n\ #!/bin/bash
\n\ set -euo pipefail
# Settings\n\ # Settings
source_dir="/data" # Data directory (mount)\n\ source_dir="/data" # Data directory (mount)
backup_dir="/backup" # Backup directory (mount)\n\ backup_dir="/backup" # Backup directory (mount)
target_free_percent=10 # Percentage of space to free\n\ # Make sure backup dir exists
\n\ mkdir -p "$backup_dir"
# Make sure backup dir exists\n\ # Step 1: List base filenames (without .conf/.off) and sort by starting block number
mkdir -p "$$backup_dir"\n\ base_files=$(find "$source_dir" -maxdepth 1 -type f \
\n\ | sed -E "s/\.(conf|off)$//" \
# Step 1: List base filenames (without .conf/.off) and sort by starting block number\n\ | sort -u \
base_files=$$(find "$$source_dir" -maxdepth 1 -type f \\\n\ | awk -F_ '{print $NF-0, $0}' | sort -n | cut -d" " -f2-)
| sed -E "s/\\.(conf|off)\$$//" \\\n\ # Convert base_files to an array for easier manipulation
| sort -u \\\n\ base_files_array=($base_files)
| awk -F_ '\''{print $$NF-0, $$0}'\'' | sort -n | cut -d" " -f2-)\n\ # Step 3: Group files by prefix and block range, keeping only the last two block ranges
\n\ declare -A file_groups
# Step 2: Calculate total size\n\ # Group files by prefix
total_size_bytes=$$(du -cb "$$source_dir"/* | tail -1 | awk '\''{print $$1}'\'')\n\ for base in "${base_files_array[@]}"; do
target_free_bytes=$$(( total_size_bytes * target_free_percent / 100 ))\n\ prefix=$(echo "$base" | sed -E "s/_([0-9]+)$//") # Get everything before the block range
\n\ block_range=$(echo "$base" | sed -E "s/.*_([0-9]+)$//") # Get the block range
echo "Total size: $$total_size_bytes bytes"\n\ file_groups["$prefix"]+="$block_range:$base "
echo "Target to free: $$target_free_bytes bytes"\n\ done
\n\ # Step 4: Process each group
# Convert base_files to an array for easier manipulation\n\ for prefix in "${!file_groups[@]}"; do
base_files_array=($$base_files)\n\ block_ranges=(${file_groups[$prefix]})
\n\ num_files=${#block_ranges[@]}
# Step 3: Group files by prefix and block range, keeping only the last two block ranges\n\ # Keep the last 2 block ranges
declare -A file_groups\n\ files_to_move=("${block_ranges[@]:0:num_files-2}")
\n\ # Move files for the current group
# Group files by prefix\n\ for file_range in "${files_to_move[@]}"; do
for base in "$${base_files_array[@]}"; do\n\ base="${file_range#*:}" # Remove block range part, keeping the full filename
prefix=$$(echo "$$base" | sed -E "s/_([0-9]+)\$$//") # Get everything before the block range\n\ for ext in "" ".conf" ".off"; do
block_range=$$(echo "$$base" | sed -E "s/.*_([0-9]+)\$$//") # Get the block range\n\ file="${base}${ext}"
file_groups["$$prefix"]+="$$block_range:$$base "\n\ if [[ -f "$file" ]]; then
done\n\ size=$(stat --printf="%s" "$file")
\n\ mv "$file" "$backup_dir/"
# Step 4: Process each group\n\ echo "Moved $file to backup."
for prefix in "$${!file_groups[@]}"; do\n\ fi
block_ranges=($${file_groups[$$prefix]})\n\ done
num_files=$${#block_ranges[@]}\n\ done
\n\ done
# Keep the last 2 block ranges\n\ echo "Done."
files_to_move=("$${block_ranges[@]:0:num_files-2}")\n\ EOF
\n\
# Move files for the current group\n\
for file_range in "$${files_to_move[@]}"; do\n\
base="$${file_range#*:}" # Remove block range part, keeping the full filename\n\
for ext in "" ".conf" ".off"; do\n\
file="$${base}$${ext}"\n\
if [[ -f "$$file" ]]; then\n\
size=$$(stat --printf="%s" "$$file")\n\
mv "$$file" "$$backup_dir/"\n\
echo "Moved $$file to backup."\n\
fi\n\
done\n\
done\n\
done\n\
\n\
# Step 5: Calculate the space freed\n\
freed_bytes=$$(du -cb "$$backup_dir"/* | tail -1 | awk '\''{print $$1}'\'')\n\
\n\
echo "Moved files to backup. Total freed: $$freed_bytes bytes."\n\
echo "Done."' > /app/prune.sh \
RUN chmod +x /app/prune.sh RUN chmod +x /app/prune.sh
environment: environment:
- BACKUP_DIR=/data/static_files/delete_me - BACKUP_DIR=/data/static_files/delete_me