Last active
February 12, 2025 10:21
-
-
Save nocodehummel/d78da9f9df87964c79f7d805539663f7 to your computer and use it in GitHub Desktop.
Rclone sync script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Verified and final version to manage Rclone sync.