Skip to content

Instantly share code, notes, and snippets.

@nocodehummel
Last active February 12, 2025 10:21
Show Gist options
  • Save nocodehummel/d78da9f9df87964c79f7d805539663f7 to your computer and use it in GitHub Desktop.
Save nocodehummel/d78da9f9df87964c79f7d805539663f7 to your computer and use it in GitHub Desktop.
Rclone sync script
#!/bin/bash
# Set variables
SOURCE="${SOURCE}"
DESTINATION="${DESTINATION}"
IGNORE_FILE="${IGNORE_FILE}"
EXCLUDE_OPTIONS=""
PID=$$
# Check ignore file
if [ -z "$IGNORE_FILE" ]; then
echo "IGNORE_FILE is not defined."
elif [ -f "$IGNORE_FILE" ]; then
echo "Using: $IGNORE_FILE"
EXCLUDE_OPTIONS="--exclude-from $IGNORE_FILE"
else
echo "Ignore file not found: $IGNORE_FILE"
fi
# Maximum files to be deleted
LOGFILE="/tmp/rclone_${PID}.log"
MAX_DELETION=50
# Rclone dry run to count deletions
/usr/bin/rclone sync "$SOURCE" "$DESTINATION" --dry-run $EXCLUDE_OPTIONS > "$LOGFILE" 2>&1
DRY_RUN_EXIT_CODE=$?
if [ $DRY_RUN_EXIT_CODE -ne 0 ]; then
echo "Dry-run failed: ${PID}"
exit $DRY_RUN_EXIT_CODE
elif [ -f "$LOGFILE" ]; then
echo "Dry run log: $LOGFILE"
else
echo "Dry run log is missing."
exit 1
fi
# File deletion limit check
DELETE_COUNT=$(grep -cEi "delete|remove" "$LOGFILE")
if [ $DELETE_COUNT -gt $MAX_DELETION ]; then
echo "WARNING: Files to be deleted ($DELETE_COUNT) exceeds maximum of $MAX_DELETION."
echo "Rclone sync operation aborted."
exit 1 # delete check failed
else
echo "Rclone sync delete count: ${DELETE_COUNT}"
rm "$LOGFILE"
fi
# Rclone sync source and destination
/usr/bin/rclone sync "$SOURCE" "$DESTINATION" $EXCLUDE_OPTIONS -v --stats=60m
@nocodehummel
Copy link
Author

Verified and final version to manage Rclone sync.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment