February 9, 20251 yr I'm in the process of building a script to confirm that I am Transferring more files than Deleting prior to executing an rclone sync. It all works very nicely but I cannot seem to grep the deleted and transferred values from the logs. Simple test script below, and full script with system alerts, etc below that. Can anyone tell me what I'm doing wrong with the grep? It always returns null but it works when testing in regex validators. #!/bin/bash # Example rclone output rclone_output="Deleted: 24603 (files), 3457 (dirs), 5.602 TiB (freed) Transferred: 22682 / 22682, 100%" # Function to extract the first group of numbers extract_first_number() { echo "$1" | grep -Eo '[0-9]+' | head } # Parse Transferred and Deleted counts, extracting only the first group of numbers transferred_count=$(echo "$rclone_output" | grep 'Transferred: [0-9]+' | tail -1 | extract_first_number) deleted_count=$(echo "$rclone_output" | grep 'Deleted: [0-9]+' | tail -1 | extract_first_number) # Output the extracted values echo "Expected Deleted: 24603, Extracted Deleted: $deleted_count" echo "Expected Transferred: 22682, Extracted Transferred: $transferred_count" Here's the larger script with the logic and the alerts, someone might find it helpful. #!/bin/bash # Define your rclone remote and paths LOCAL="unraid:/array/" REMOTE="Synology:/backup/" BACKUPNAME="Backup Array" # Perform a dry run rclone_output=$(docker exec Nacho-Rclone-Native-GUI rclone sync --dry-run "$LOCAL" "$REMOTE" -v) # Wait for 5 seconds sleep 5 # Function to extract the first group of numbers extract_first_number() { echo "$1" | grep -o '[0-9]\+' | head -1 } # Parse Transferred and Deleted counts, extracting only the first group of numbers transferred_count=$(echo "$rclone_output" | grep 'Transferred: [0-9]*' | tail -1 | grep -o '[0-9]\+' ) deleted_count=$(echo "$rclone_output" | grep 'Deleted: [0-9]*' | tail -1 | grep -o '[0-9]\+' ) echo "Transferred count ($transferred_count) and Deleted count ($deleted_count)" # Check if Transferred count is greater than Deleted count if [ "$transferred_count" -gt "$deleted_count" ]; then echo "Syncing as Transferred count ($transferred_count) is greater than Deleted count ($deleted_count)" # Execute the sync command and capture the output sync_output=$(docker exec Nacho-Rclone-Native-GUI rclone sync "$LOCAL" "$REMOTE" -v --transfers=1) if [ $? -eq 0 ]; then # Parse the final Transferred and Deleted counts from the sync output, ensuring they are integers final_transferred_count=$(echo "$sync_output" | grep -oP 'Transferred: *\d+ / \d+,' | tail -1 | awk '{print $2}') final_deleted_count=$(echo "$sync_output" | grep -oP 'Deleted: *\d+ ' | tail -1 | awk '{print $2}') final_transferred_count=${final_transferred_count:-0} final_deleted_count=${final_deleted_count:-0} # Trigger Unraid system alert on success #logger "rclone sync successful: Transferred $final_transferred_count files, Deleted $final_deleted_count files" /usr/local/emhttp/webGui/scripts/notify -i normal -s "$BACKUPNAME" -d "Rclone Backup was successful, $final_transferred_count files transfered, $final_deleted_count deleted." else #logger "rclone sync failed" /usr/local/emhttp/webGui/scripts/notify -i warning -s "$BACKUPNAME" -d "Rclone Backup completed, but more files were deleted than transfered, $final_transferred_count files transfered, $final_deleted_count deleted!" fi else echo "Skipping sync as Transferred count ($transferred_count) is not greater than Deleted count ($deleted_count)" #logger "rclone sync skipped: Transferred count ($transferred_count) is not greater than Deleted count ($deleted_count)" /usr/local/emhttp/webGui/scripts/notify -i normal -s "$BACKUPNAME" -d "Rclone Backup was skipped as Transferred count ($transferred_count) is not greater than Deleted count ($deleted_count)" fi Edited February 9, 20251 yr by Kboogie
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.