How to run Shell script to File transfer on remote system.

Here we will cover how to prepare Shell script to File transfer file on remote system using ftp. Below is the procedure to ‘copy file using file transfer protocol (FTP) shell scrip’ an we will seeHow to Use Linux FTP Command to Transfer Files’. In this shell script(filetoftp.sh) we will receive notification on mail if file transfer(ftp) fail due to any reason.

 

Why we are using FTP?

 
 
File Transfer Protocol (FTP) is a standard network protocol used to transfer files to and from a remote network. To transfer file using ftp, we will require ftp server, user account and ftp client .
 
 
 
 

How to execute FTP manually?.

 
 
Manually to get and put the file on one server to another server(remote server):-
 
ftp -n 192.168.11.10
 
ftp>quote USER employee
 
ftp>quote PASS ******
 
230 OK. Current restricted directory is /
 
ftp> cd /data01
 
ftp> lcd /backup01    –> change local directory on local server.
 
ftp> mget ***.csv     –>to get the file from ftp server to local server.
 
ftp> mput ****.csv   –>to transfer file from local to ftp server.
 
 
 

How to automate FTP using shell script:-

 
 

Shell script to file transfer automatically on remote server using :- 

 
 
Script:- /data01/crontab_scripts/tada_schedule/filetoftp.sh
 
#!/bin/sh
 
# Created by Sajid Quamer
 
#CSV file to be transfer from db server to ftp server
 
cd /data05/TADA_Master_curr
 
CURR_DT=`date +%d-%m-%Y`
 
export CURR_DT
 
CSV_FILE1=TADA_CITY_MASTER_$CURR_DT.csv
 
CSV_FILE2=TADA_POLICY_MASTER_$CURR_DT.csv
 
CSV_FILE3=TADA_EMPLOYEES_MASTER_$CURR_DT.csv
 
CSV_FILE4=Leave_pending_$CURR_DT.csv
 
CSV_FILE5=CONTRACT_DATA_$CURR_DT.csv
 
CSV_FILE6=BILL_DATA_$CURR_DT.csv
 
Scriptfile=/data01/crontab_scripts/tada_schedule/tadatoftp.sh 
 
export CSV_FILE1
 
export CSV_FILE2
 
export CSV_FILE3
 
export CSV_FILE4
 
export CSV_FILE5
 
export CSV_FILE6
 
export Scriptfile
 
ftp -n << ‘EOF’
 
open 192.168.11.10
 
quote USER *******
 
quote PASS *******
 
cd /employees
 
prompt no
 
lcd /data05/TADA_Master_curr
 
mput TADA_CITY_MASTER*
 
mput TADA_POLICY_MASTER*
 
mput TADA_EMPLOYEES_MASTER*
 
mput Leave_pending_*
 
bye
 
EOF
 
echo “done”
 
 
check if a file exists or not
 
 
if [[  -f $CSV_FILE1 &&   -f $CSV_FILE2  &&  -f $CSV_FILE3 && -f $CSV_FILE4 ]]; then
 
echo “File Transfer Process was Successful  and ‘$CSV_FILE1’ exist”
 
else
 
echo “FTP File Transfer Process Failed ‘$CSV_FILE1’ Does not exist at `hostname`. Please contact System Administrator! $Scriptfile” | mailx -s “File Transfer Failed” [email protected], [email protected]
fi
 
 
 

Using shell script to file transfer, here it will check if a file exists or not.

 
 
if [[  -f $CSV_FILE1 &&   -f $CSV_FILE2  &&  -f $CSV_FILE3 && -f $CSV_FILE4 ]]; then
 
echo “File Transfer Process was Successful  and ‘$CSV_FILE2’ exist”
else
echo “FTP File Transfer Process Failed ‘$CSV_FILE2’ Does not exist at `hostname`. Please contact System Administrator! $Scriptfile” | mailx -s ” File Transfer Failed” [email protected], [email protected]
fi
 
if [[  -f $CSV_FILE1 &&   -f $CSV_FILE2  &&  -f $CSV_FILE3 && -f $CSV_FILE4 ]]; then
echo “File Transfer Process was Successful  and ‘$CSV_FILE3’ exist”
else
echo “FTP File Transfer Process Failed ‘$CSV_FILE3’ Does not exist at `hostname`. Please contact System Administrator! $Scriptfile” | mailx -s ” File Transfer Failed” [email protected], [email protected]
fi
 
if [[  -f $CSV_FILE1 &&   -f $CSV_FILE2  &&  -f $CSV_FILE3 && -f $CSV_FILE4 ]]; then
echo “File Transfer Process was Successful  and ‘$CSV_FILE4’ exist”
else
echo “FTP File Transfer Process Failed ‘$CSV_FILE4’ Does not exist at `hostname`. Please contact System Administrator! $Scriptfile” | mailx -s “File Transfer Failed” [email protected], [email protected]
fi
 
 
cd /data05/TADA_Master_curr
 
 
rm -rf $CSV_FILE1
 
rm -rf $CSV_FILE2
 
rm -rf $CSV_FILE3
 
rm -rf $CSV_FILE4
 
 

Related:-

1 thought on “How to run Shell script to File transfer on remote system.”

Leave a Comment