Sponsored Content
Full Discussion: Sftp files between servers
Top Forums Shell Programming and Scripting Sftp files between servers Post 302796371 by nmm_dba on Friday 19th of April 2013 12:14:25 PM
Old 04-19-2013
Sftp files between servers

I am writing a shell script to copy sybase related .dump files from one to another aix server and visa versa.

Please help me do the following

The script does the following:
- check directory exists
- copy dump files
- copy is done for multiple database
- checks databases, if one database does not exist, the script should skip that database and move to the next database.

Started the script:
==============
Code:
#!/usr/bin/ksh
# *****************************************************************************
#  Script Name:  sftp_dump_files.ksh
#  Description:  Performs sFTP put of exported dump files
#
#  Syntax:
#   sftp_dump_files.ksh -d <lv_dbname> -r <lv_rfc>
#
#  Required parameters:
#     lv_dbname      - name of schema dump to sFTP
#     lv_rfc            - change number [ format CHGXXXX ]
# *****************************************************************************
#
# Execute to setup global variables
. ${APPS}/db_batch/common/.batchenv
# *****************************************************************************
#  Function - Display parameter usage and terminate the job
# *****************************************************************************
CMD=`basename $0`
display_usage()
{
   echo "==============================================================================="
   echo "Script Name:  ${CMD}"
   echo "Description:  Performs sFTP put of exported dump files"
   echo ""
   echo "Usage:"
   echo "   ${CMD} -r <lv_rfc>"
   echo ""
   echo "Required parameters:"
   echo "   lv_dbname      - name of database dump to sFTP"
   echo "   lv_rfc            - change number [ format CHGXXXX ]"
   echo "==============================================================================="
   exit 1
}
# *****************************************************************************
#  Parse input parameter
# *****************************************************************************
set -- $(getopt u:r: $*)
if [ $? -ne 0 ]
then
   echo "ERROR: Insufficient arguments."
   display_usage
fi
count_r=0
for o in $(getopt u:r: $*)
do
   case $o in
      -u) lv_dbname="$2"; count_u=$(expr $count_u \+ 1); shift 2;;
      -r) lv_rfc="$2"; count_r=$(expr $count_r \+ 1); shift 2;;
      --) shift; break;;
   esac
done
#  Abort if there are insufficient arguments
if [ $count_u -ne 1 ] || [ $count_r -ne 1 ]
then
   echo "ERROR: Insufficient arguments."
   display_usage
fi
#  Export local variables and force uppercase
export lv_dbname="`echo ${lv_dbname} | tr '[:lower:]' '[:upper:]'`"
export lv_rfc="`echo ${lv_rfc} | tr '[:lower:]' '[:upper:]'`"
# *****************************************************************************
#  Setup local step variables
# *****************************************************************************
#  Local job variables
export lv_job_path="${DBBATCH}/CR/${lv_rfc}"
export lv_common_path="${lv_job_path}/common"
export lv_shell_path="${lv_job_path}/shell"
export lv_sql_path="${lv_job_path}/sql"
export lv_log_path="${lv_job_path}/log"
mkdir -p ${lv_common_path} ${lv_shell_path} ${lv_sql_path} ${lv_log_path}
#  Setup local step variables
lv_step_name=sftp_dump_files
lv_step_log_file=${lv_log_path}/${lv_step_name}.log
# Additional local variables
lv_retcode=0
lv_exitcode=0
# *****************************************************************************
#  Main part of the step script (sFTP put from ${LOCAL_HOST} to ${REMOTE_HOST})
# *****************************************************************************
echo "*** `date` Begin executing ${CMD} ***" | tee ${lv_step_log_file}
????????????????????????????????????
Here i have to put FOR or WHILE LOOP
# Loop through for all databases (eg: central ltts pass rd1 rpt1 teraview1 tvstage workdb)
# Skip if the one database does not exist
????????????????????????????????????

#  sFTP dump file if it exists
if [ -f ${LOCAL_EXPORT}/${lv_rfc}_${lv_dbname}.dump ]
then
   echo "Local Host: " ${LOCAL_HOST} | tee -a ${lv_step_log_file}
   echo "Local Files:" | tee -a ${lv_step_log_file}
   ls -lrt ${lv_rfc}_*.dump | tee -a ${lv_step_log_file}
   echo "" | tee -a ${lv_step_log_file}
   echo "Remote Host: " ${REMOTE_HOST} | tee -a ${lv_step_log_file}
   #  sftp the script if it is for EXPORT
   sftp sybase@${REMOTE_HOST} << EOF >> ${lv_step_log_file} 2>&1
      lcd ${LOCAL_EXPORT}
      cd ${REMOTE_EXPORT}
      mput -p ${DUMPATH}/${lv_dbname}/${lv_dbname}*.dump
      ls -lart
   quit
EOF
else
   echo "FAILURE : Dump file ${LOCAL_EXPORT}/${lv_rfc}_${lv_dbname}.dump does not exist." | tee -a ${lv_step_log_file}
   echo "*** `date` Failed while executing ${CMD} ***" | tee -a ${lv_step_log_file}
   exit 1
fi
echo "*** `date` Finish executing ${CMD} ***" | tee -a ${lv_step_log_file}
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Final:
#   Following are executed only all the previous steps are executed successfully to update
#   successful status in log files and notify recipient through email
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
echo "${lv_job_name} on ${SERVERNAME} finished at `date`" >> ${lv_job_log_file}
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Exit batch script with return code
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
exit ${lv_exitcode}

===================

---------- Post updated at 11:41 AM ---------- Previous update was at 11:29 AM ----------

Main part is looking to LOOP thorough all databases :

????????????????????????????????????
Here i have to put FOR or WHILE LOOP
# Loop through for all databases (eg: central ltts pass rd1 rpt1 teraview1 tvstage workdb)
# Skip if the one database does not exist
????????????????????????????????????

---------- Post updated at 12:14 PM ---------- Previous update was at 11:41 AM ----------

Can anyone help please...
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

need script to connect sftp servers

Dear friends, i need to connect sftp server from my home directory using script . Please can anyone help me on this. (1 Reply)
Discussion started by: kittusri9
1 Replies

2. Shell Programming and Scripting

SFTP Transfer Across Multiple Servers

Hello everyone, Have a question which I'm not entirely sure about, but will ask anyway. We have three servers (A/B/C), 'A' being the local host, 'B' being a SFTP server on the DMZ, and 'C' being the intended remote host. The task is to transfer file/s from server 'A' to server 'C' via server... (0 Replies)
Discussion started by: Cameron
0 Replies

3. UNIX for Dummies Questions & Answers

Unable to scp/sftp between two servers

I have four servers that for all intents and purposes are the same (I have the same profile on all four), North, South, Brooklyn & Queens. I have a script that scp's a file from Queens to brooklyn, and it runs just fine. I tried to replicate the script on South, to transfer a file to North, and... (1 Reply)
Discussion started by: DeCoTwc
1 Replies

4. UNIX for Dummies Questions & Answers

Need to calculate sftp time beteen two servers.

Hi, I need to calculate time taken by a 100kb file for sftp between two servers. I am new to unix can some body please tell me how do this. Thanks in advance... Vivek (2 Replies)
Discussion started by: koulvivek
2 Replies

5. Shell Programming and Scripting

SFTP files to two servers

Hi, I need to write a unix shell script which sends files to a server1 from my server via sftp. Then it needs to again SFTP files from that server1 to another server2. And finally remove the transferred files in the intermediate server1. Please provide me pointers on how to achieve the same. All... (3 Replies)
Discussion started by: vharsha
3 Replies

6. Red Hat

Chroot sftp users, remote sftp login shows wrong timestamp on files

Hello, I have a weird issue, I have RHEL 5.7 running with openssh5.2 where sftpgroup OS group is chroot. I see the difference difference in timestamp on files, when I login via ssh and SFTP, I see four hour difference, is something missing in my configuration. #pwd... (8 Replies)
Discussion started by: bobby320
8 Replies

7. Shell Programming and Scripting

Renaming multiple files in sftp server in a get files script

Hi, In sftp script to get files, I have to rename all the files which I am picking. Rename command does not work here. Is there any way to do this? I am using #!/bin/ksh For eg: sftp user@host <<EOF cd /path get *.txt rename *.txt *.txt.done ... (7 Replies)
Discussion started by: jhilmil
7 Replies

8. Shell Programming and Scripting

Check files and archive the files using sftp

Hi, I have to check the files in another server using sftp to do that, below is the code i am going with #!/bin/bash export SRC_FOLDER=$1 export ARC_FOLDER=$2 HOST=as07u3456 USER=relfag sftp ${USER}@${HOST} <<EOF cd $SRC_FOLDER/DSCOR ls bye EOF echo "done" whatever the files i... (8 Replies)
Discussion started by: ursrami
8 Replies

9. UNIX for Advanced & Expert Users

How to setup sftp beteen two servers?

Hi Could you please help me out how to configure between two server I don't have admin idea to setup the Sftp server the requirements is we want to send a file to vendor so we need sftp configuration so that can we can send file through sftp Please let me know what should I ask to vendor... (1 Reply)
Discussion started by: jagu
1 Replies

10. UNIX for Advanced & Expert Users

Connect direct - SFTP - List of servers that I can connect

Greetings Experts, I am working for a bank client and have a question on connect-direct and SFTP. We are using Linux RedHat servers. We use connect-direct to transfer (NDM) files from one server to another server. At times, we manually transfer the files using SFTP from one server to another... (2 Replies)
Discussion started by: chill3chee
2 Replies
All times are GMT -4. The time now is 04:46 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy