Skip to content

Instantly share code, notes, and snippets.

@charlesbmi
Last active June 10, 2024 16:16
Show Gist options
  • Save charlesbmi/2cd72477f2fddec2f872e84076f14558 to your computer and use it in GitHub Desktop.
Save charlesbmi/2cd72477f2fddec2f872e84076f14558 to your computer and use it in GitHub Desktop.
rsync with retries
#!/bin/bash
#
# General Usage:
# ./rsync_script.sh [additional rsync options] <source> <destination>
# Example with Additional Options:
# ./rsync_script.sh --bwlimit=1000 --info=progress2 /path/to/source user@destination:/path/to/destination
# Check if sufficient arguments are provided
if [ $# -lt 2 ]; then
echo "Usage: $0 [additional rsync options] <source> <destination>"
exit 1
fi
# Configuration
MAX_RETRIES=1440 # number of minutes in the day
SLEEP_TIME=60 # seconds between retries
RSYNC_OPTS='-avz --human-readable --progress --exclude=".*"' # Default rsync options with .DS_Store/hidden-file exclusion
LOG_DIR="$HOME/rsync_log" # Directory to store logs
LOG_FILE="$LOG_DIR/rsync_log_$(date +%Y%m%d-%H%M%S).txt" # Log file name with timestamp
# Extract source and destination from command-line arguments
SOURCE="${@: -2:1}"
DESTINATION="${@: -1}"
# Accept additional rsync options from command line, excluding the last two arguments which are source and destination
if [ $# -gt 2 ]; then
RSYNC_OPTS+=" ${@:1:$#-2}"
fi
# Handle Ctrl-C (SIGINT) interruption
trap 'echo "Rsync process interrupted. Exiting."; exit 1' SIGINT
# Create log directory if it doesn't exist
mkdir -p $LOG_DIR
# Initial attempt count
attempt_num=1
while [ $attempt_num -le $MAX_RETRIES ]; do
echo "Attempt $attempt_num of $MAX_RETRIES"
rsync $RSYNC_OPTS "$SOURCE" "$DESTINATION" | tee -a $LOG_FILE
# Check if rsync was successful
if [ $? -eq 0 ]; then
echo "Rsync completed successfully."
echo "Success at $(date)" >> $LOG_FILE
exit 0
else
echo "Rsync attempt failed. Retrying in $SLEEP_TIME seconds..."
echo "Failure at attempt $attempt_num on $(date)" >> $LOG_FILE
sleep $SLEEP_TIME
attempt_num=$((attempt_num + 1))
fi
done
echo "Rsync failed after $MAX_RETRIES attempts."
echo "Final failure at $(date)" >> $LOG_FILE
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment