#!/bin/zsh set -e ICLOUD_BASE="/Users/treyt/Library/Mobile Documents/com~apple~CloudDocs/Untitled Folder/Still Need" REMOTE="root@10.3.3.11" REMOTE_MEDIA="/mnt/user/downloads/OFApp/media" REMOTE_PASS="Intel22" BATCH_SIZE=100 FOLDERS=("jadeteen" "tiktoknsfw" "TikTokNude" "tiktokthots") wait_for_downloads() { local folder="$1" shift local files=("$@") local total=${#files[@]} local max_wait=600 # 10 min max per batch echo " Waiting for $total files to download..." local elapsed=0 while true; do local ready=0 for f in "${files[@]}"; do local ondisk=$(du -k "$folder/$f" 2>/dev/null | cut -f1) if [ "$ondisk" -gt 0 ] 2>/dev/null; then ready=$((ready + 1)) fi done if [ $ready -ge $total ]; then echo " All $total files downloaded." return 0 fi if [ $elapsed -ge $max_wait ]; then echo " WARNING: Timed out after ${max_wait}s. $ready/$total downloaded." return 1 fi if [ $((elapsed % 15)) -eq 0 ]; then echo " $ready/$total on disk... (${elapsed}s)" fi sleep 5 elapsed=$((elapsed + 5)) done } for FOLDER in "${FOLDERS[@]}"; do SRC="$ICLOUD_BASE/$FOLDER" echo "==========================================" echo "Processing folder: $FOLDER" echo "==========================================" # Get file list FILES=("${(@f)$(ls -1 "$SRC" | sort)}") TOTAL=${#FILES[@]} echo "Total files: $TOTAL" # Download all files in batches of 100 using brctl BATCH=0 i=1 while [ $i -le $TOTAL ]; do BATCH=$((BATCH + 1)) END=$((i + BATCH_SIZE - 1)) if [ $END -gt $TOTAL ]; then END=$TOTAL; fi COUNT=$((END - i + 1)) echo "" echo "[$FOLDER] Download batch $BATCH: files $i-$END of $TOTAL ($COUNT files)" # Trigger brctl download for this batch BATCH_FILES=() for ((j=i; j<=END; j++)); do FNAME="${FILES[$j]}" BATCH_FILES+=("$FNAME") brctl download "$SRC/$FNAME" 2>/dev/null & done wait # wait for all brctl commands to return (they're async triggers) # Poll until all files are actually on disk wait_for_downloads "$SRC" "${BATCH_FILES[@]}" i=$((END + 1)) done # Verify all downloaded echo "" echo "[$FOLDER] Verifying all files on disk..." NOT_READY=0 for f in "${FILES[@]}"; do ONDISK=$(du -k "$SRC/$f" 2>/dev/null | cut -f1) if [ "$ONDISK" -eq 0 ] 2>/dev/null; then NOT_READY=$((NOT_READY + 1)) fi done if [ $NOT_READY -gt 0 ]; then echo " WARNING: $NOT_READY files still not on disk. Continuing anyway..." else echo " All $TOTAL files verified on disk." fi # Zip the entire folder ACTUAL_SIZE=$(du -sh "$SRC" | cut -f1) echo "[$FOLDER] Zipping $TOTAL files ($ACTUAL_SIZE)..." ZIP_FILE="/tmp/${FOLDER}.zip" rm -f "$ZIP_FILE" cd "$SRC" zip -q -0 "$ZIP_FILE" * 2>/dev/null cd / ZIP_SIZE=$(stat -f "%z" "$ZIP_FILE" 2>/dev/null || echo 0) echo " Zip created: $(echo "scale=1; $ZIP_SIZE / 1048576" | bc) MB" # Upload to server echo "[$FOLDER] Uploading to server..." sshpass -p "$REMOTE_PASS" scp "$ZIP_FILE" "$REMOTE:/tmp/" # Extract on server echo "[$FOLDER] Extracting on server..." sshpass -p "$REMOTE_PASS" ssh "$REMOTE" "mkdir -p '$REMOTE_MEDIA/$FOLDER' && cd '$REMOTE_MEDIA/$FOLDER' && unzip -q -o '/tmp/${FOLDER}.zip' && rm -f '/tmp/${FOLDER}.zip'" # Cleanup local zip rm -f "$ZIP_FILE" # Verify REMOTE_COUNT=$(sshpass -p "$REMOTE_PASS" ssh "$REMOTE" "ls -1 '$REMOTE_MEDIA/$FOLDER' | wc -l" | tr -d ' ') echo "" echo "[$FOLDER] Done! $TOTAL local -> $REMOTE_COUNT on server" echo "" done echo "==========================================" echo "All folders transferred!" echo "=========================================="