Copy multiple .txt files from one server to another server

:wall:Hi all,
I have two servers : server A and server B
Weekly wise i use to receive files to server A to one particular location say /source/location . NOTE: In this location there will be other files also present other than these weekly arrival 18 files.
My task :

i need a shell script to perform this automatically every Thursday @ 3.00 PM EST. I need to send out success or failure email also after the process is done.

Either single script to perform all at once means moving files to folder in server B and getting files from server A or 2 different script like one for moving files to folder in server B and another script to get files from server A

Anything is fine for me. Please give me an idea or sample similar script with good explanation is required as am very new to shell scripting

Hmmmm.

First of all, this sounds a bit like homework. is this a homework problem ?

Second, please show your work; post your scripts, ideas and what you have done so far.

This is not a homework. This is the usual work that i use to perform every week so one of my friend suggested to automate the process to save time to spend on other works as am fully engaged. here is the one that i tried am not sure whether its correct or not i just gave a try as am very new to the shell programming and UNIX environment. This is just a part to get the files from source to target.

#!/bin/sh
HOST=source server(A)
echo "copying files from A server to B:/target/location"
'ftp -n $HOST >>END_SCRIPT
echo "connection success"
echo "logged on to A server"
cd /source/location
echo "source location log on success"
echo "starting the files transfer"
get A.txt
get B.txt
get C.txt
get D.txt
get E.txt
get F.txt
get G.txt
get H.txt
get I.txt
get J.txt
get K.txt
get L.txt
get M.txt
get N.txt
get O.txt
get P.txt
get Q.txt
get R.txt
echo "transfering of all 18 files done successfully"
bye
echo "Logged out of A server successfully"
END_SCRIPT'

EXIT_STATUS=$?
if[[ $EXIT_STATUS -eq 0 ]]
then
mailx -s\'"FTPing Files got failed"' kamegam@xxxx.com < $MSG/ftp_fail.msg
exit 1
else
mailx -s\'"FTPing Files are success"' kamegam@xxxx.com < $MSG/ftp_suc.msg
fi
exit 0

Thanks Don.I will use code tag from next time.

---------- Post updated at 02:55 PM ---------- Previous update was at 02:54 PM ----------

Do anyone know how to do the specified shell program. I have created a rough one as of my understanding that is am in 1st level of shell programing. Can anyone look into it and let me know how to correct it

What's da program?

You seem to have a few problems:

  1. You can't mix ftp commands (e.g., get ) and shell commands (e.g., echo ) that way.
  2. Single quoting a command makes the shell think the entire single quoted string is the name of a command to execute.
  3. Iinstead of redirecting the output of the ftp command it looks like you want ftp to read the input from a here-document in your script.
  4. Using the ftp -n option tells ftp not to login to your destination host.
  5. Exit status 0 indicates success; not failure.
  6. You can't use a variable ([e.g., ICODE]$MSG[/ICODE]) before you assign a value to it. (Well, you can, but you'll get an empty string from the expansion.)

Assuming that you are logged into Server B when you run this code and that /bin/sh on your system is not a pure Bourne shell (which does not recognize [[ expression ]] , maybe something more like:

#!/bin/sh
# Note that "HOST=source server(A)" only works if there are no space or ()s after the "=".
HOST="serverA"

echo "copying files from A server to B:/target/location"
ftp $HOST <<-END_SCRIPT
	verbose
	cd /source/location
	lcd /target/location
	mget [A-R].txt
	bye
END_SCRIPT
EXIT_STATUS=$?
echo "Logged out of A server with exit code $EXIT_STATUS"

if [[ $EXIT_STATUS -ne 0 ]]
then	mailx -s "FTPing Files got failed" kamegam@xxxx.com < /path/to/ftp_fail.msg
	exit 1
else	mailx -s "FTPing Files are success" kamegam@xxxx.com < /path/to/ftp_suc.msg
fi
exit 0

would work better, but this is completely untested.

Hi Don First of thank you for your reply.
you have mentioned

mget [A-R].txt

which indicates can select multiple fiels. As i have mentioned, in the source directory i have additional files apart from A-R.txt so as you mentioned should i need to give the command as like

mget A.txt B.txt to R.txt 
               or
      mget A,B,..R.txt

Did you try the ftp command get [A-R].txt ? The ftp commands:

mget [A-R].txt

and:

mget A.txt B.txt C.txt D.txt E.txt F.txt G.txt H.txt I.txt J.txt K.txt L.txt M.txt N.txt O.txt P.txt Q.txt R.txt

should produce identical results as long as all 18 of the files in the 2nd mget are present on the remote system. The 1st command will also work if any non-empty subset of those files are present on the remote system, while the 2nd command will give you an error if one or more of the 18 files listed are not found on the remote system.