Main Page

Sunday, 14 February 2021

Shell Script to use rsync to copy the files to remote servers

 2) Shell Script to use rsync to copy the files to remote servers

#!/bin/sh
## Author: Pavan Bandaru
## Date: 9/28/2018
set -x


display_usage() {
  echo "This script must be run with ...."
  echo -e "\nUsage:\n$0 [hostfile.txt] \n"
}


if [ $# -eq 0 ]; then
    display_usage
    exit 1
fi


hostfile=$1
for host in `cat<$hostfile`
do
  hostname=$(echo $host|awk -F"," '{print $1}')
  echo "------------------------------------------------"
  echo "connecting to $hostname"
  rsync -r localdir ${hostname}:/home/pavan/

RESULT=$?
if [ $RESULT -ne 0 ]; then
  echo Login Failed
fi

done

Note: Keep the remote host list on the host file. 
hostfile.txt
host1
host2
host3
host4

No comments:

Post a Comment