Script to transfer files to remote server with verification

Here is a bash script that uses rsync to transfer a set of files to another remote server and verifies the contents were transferred correctly:

#!/bin/bash

# Set the source and destination directories
src_dir="/path/to/local/files"
dst_dir="/path/to/remote/files"

# Set the hostname and username for the remote server
hostname="remote_server"
username="remote_user"

# Use rsync to transfer the files
rsync -avz $src_dir $username@$hostname:$dst_dir

# Check the exit code of the rsync command to make sure it completed successfully
if [ $? -eq 0 ]; then
  # The rsync command completed successfully, so now we will compare the contents of the source and destination directories
  # to make sure they are the same
  if cmp -s "$src_dir" "$dst_dir"; then
    # The contents of the directories are the same, so the transfer was successful
    echo "Transfer successful"
  else
    # The contents of the directories are different, so the transfer was not successful
    echo "Error: Transfer failed, contents of directories do not match"
  fi
else
  # The rsync command failed, so the transfer was not successful
  echo "Error: Transfer failed"
fi

This script transfers the files from the src_dir directory on the local machine to the dst_dir directory on the remote server using rsync. It then checks the exit code of the rsync command to make sure it completed successfully. If the rsync command was successful, the script compares the contents of the src_dir and dst_dir directories to make sure they are the same. If the directories are the same, the transfer is considered successful. If the directories are different, or if the rsync command fails, the transfer is considered not successful.

Did this tutorial help a little? How about buy me a cup of coffee?

Buy me a coffee at ko-fi.com

Please feel free to use the comments form below if you have any questions or need more explanation on anything. I do not guarantee a response.

IMPORTANT: You must thoroughy test any instructions on a production-like test environment first before trying anything on production systems. And, make sure it is tested for security, privacy, and safety. See our terms here.