# Replace the REMOTE_HOST, REMOTE_PATH, and LOCAL_PATH variables with your actual remote host details, remote file path, and local directory path, respectively. # The LOCK_FILE variable specifies the path to the lock file, which will be used to prevent multiple instances of the script from running simultaneously. # The is_locked function checks if the lock file exists. # The create_lock function creates the lock file. # The remove_lock function removes the lock file. # The perform_rsync function performs the actual rsync -z command to copy the files from the remote machine to the local machine. # The script checks if the lock file exists. If it does, it means the previous rsync is still running, so it exits without starting a new rsync. # If the lock file doesn't exist, the script creates the lock file, performs the rsync, and then removes the lock file.
INIT_DIR=$1
REMOTE_HOST="user@ip" PORT="1234567" REMOTE_PATH="/home/user/${INIT_DIR}/file.*" LOCAL_PATH="/home/user/${INIT_DIR}/" LOCK_FILE="${LOCAL_PATH}/rsync.lock" LOG_FILE="${LOCAL_PATH}/rsync.log" # Function to check if the lock file exists is_locked() { [ -f "$LOCK_FILE" ] }
# Create the lock file create_lock() { touch"$LOCK_FILE" }
# Remove the lock file remove_lock() { rm"$LOCK_FILE" }
# Function to perform rsync perform_rsync() { rsync -azvhP -e 'ssh -p '"$PORT""$REMOTE_HOST:$REMOTE_PATH""$LOCAL_PATH" --log-file=${LOG_FILE} }
# Check if the lock file exists if is_locked; then echo"Previous rsync is still running. Exiting." exit 1 fi
# Create the lock file create_lock
# Perform rsync perform_rsync
# Remove the lock file remove_lock
This can be submitted as cron job and automatically downloads new files generated (e.g. every hour). It checks whether the previous rsync job is done before rsync-ing again, otherwise waits for another time interval defined in the crontab (it generates a 'lock' file when starting a rsync and deleting it afterwards. If the 'lock' file is present it will not run rsync)