Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- set -euo pipefail
- BASE="/home/mcp/logs"
- START_DATE="20260401"
- # 7 GB reserve margin, in MiB
- RESERVE_MB=7168
- # File you supply for duplication
- TMPFILE="/path/to/source/file.csv"
- # Output filename inside each dated log folder
- OUTFILE="file.csv"
- if [[ ! -f "$TMPFILE" ]]; then
- echo "Missing TMPFILE: $TMPFILE"
- exit 1
- fi
- current="$START_DATE"
- while true; do
- current=$(date -d "${current} -1 day" +%Y%m%d)
- year=$(date -d "$current" +%Y)
- month=$(date -d "$current" +%-m)
- day=$(date -d "$current" +%-d)
- dir="$BASE/$year/$month/$day"
- mkdir -p "$dir"
- free_kb=$(df --output=avail "$BASE" | tail -n 1)
- free_mb=$((free_kb / 1024))
- usable_mb=$((free_mb - RESERVE_MB))
- if (( usable_mb <= 0 )); then
- echo "Stopping: reserve margin reached. Free=${free_mb}MB Reserve=${RESERVE_MB}MB"
- break
- fi
- outfile="$dir/$OUTFILE"
- echo "Writing $outfile"
- cp "$TMPFILE" "$outfile"
- done
- echo "Done."
Advertisement
Add Comment
Please, Sign In to add comment