Sftp files between servers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sftp files between servers
# 1  
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...
# 2  
Old 04-19-2013
I don't think sftp will accept this format (often called a HERE):-
Code:
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

You may have to put the commands in a batch file and refer to them with a -b flag. The sftp will also abort if there is an error and you can check for that.

You will have to ensure that when you manually run sftp sybase@${REMOTE_HOST} that it connects you without prompting for authentication. Do the local cd before you start sftp and your batch file can then just contain:-
Code:
cd REMOTE_EXPORT
mput -p DUMPATH/lv_dbname/lv_dbname*.dump
ls -lart
quit

I'm not sure that you can code variables into it, but you could easily build it each time.


For your loop, what feeds into it? If the database names are in a file, then you could wrap that section in :-

Code:
while read database
do
  {main contents of that block}
done < database-list-file

If you need to skip if the file to send doesn't exist, just remove the exit 1 statement as this will terminate your script. I think you have a suitable test for file existing already.

I hope that this helps.




Robin
Liverpool/Blackburn
UK
This User Gave Thanks to rbatte1 For This Post:
# 3  
Old 04-19-2013
Thanks rbatte1,
Yes, we are running this as shell....environment is sybase/aix
What is "main contents of that block" you mentioned

Code:
while read database
do
  {main contents of that block}
done < database-list-file

It should sftp all dump files per database and send logs to different logfiles.

Last edited by nmm_dba; 04-19-2013 at 03:09 PM..
# 4  
Old 04-22-2013
Just using the code you put up, I was looking at this bit:-
Code:
:
:
????????????????????????????????????
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
:
:

Do you have a list of the files you want to try to send, or if they're all in one place, is there anything else in that directory?

The loop could be based on either, or even the parameters on the command line like yours might be.



Robin
This User Gave Thanks to rbatte1 For This Post:
# 5  
Old 04-22-2013
Thanks Robin,

There is a directory for each databases(below are names) which contais only only .dump files.

Directory Structure:
[sybase]: /dump/sybase15.5
drwxr-x--- 2 sybase dbo 256 Apr 21 22:30 teraview1
drwxr-x--- 2 sybase dbo 256 Apr 21 22:31 rpt1
drwxr-x--- 2 sybase dbo 256 Apr 21 22:32 rd1
drwxr-x--- 2 sybase dbo 256 Apr 21 22:33 central
drwxr-x--- 2 sybase dbo 256 Apr 21 22:35 workdb
drwxr-x--- 2 sybase dbo 256 Apr 21 22:35 dbccdb

We have .batchenv file which is used to set environment. The script read this file first for path, database list etc as shown below .

Code:
# Host servername. To be used notification message
SERVERNAME=`hostname`
# DR Servername. To be used in ftp process, it will be set to nothing if there is no DR environment for this host
DRSERVERNAME=mrktrvdbuxtr02
# ASE servername. To be user on isql -S option
ASENAME=tv_training_mr02
# Replication Server name. If there is no replication server , it will be set to nothing.
REPLNAME=
# DR ASE servername. it will be set to nothing if there is no DR server.
DRASENAME=tv_traininig_st02
# Replication server manager name. If there is no replication server , it will be set to nothing.
RSMNAME=
# path for Application batch
APPBATCH=${APPS}/app_batch
# path for Database batch
DBBATCH=${APPS}/db_batch
# path for common batch
COMMON=${DBBATCH}/common
#  password for user in .dbpfileuser file
DBP=`cat ${COMMON}/.dbpfile`
#  password for user in .rplpfileuser
RPLP=
# path of the password for user in .dbpfileuser file
DBPF=${COMMON}/.dbpfile
RPLPF=
# user to be connect on isql -U option
BATCHUSER=`cat ${COMMON}/.dbpfileuser`
REPLUSER=
# call the checkscript to validate and e-mail notification
VERIFY_SCRIPT=${COMMON}/verification.sh
NOTIFY_SCRIPT=${COMMON}/notification.sh
# For ASE environment only
VERIFY_CHECKSTORAGE=${COMMON}/verify_checkstorage.sh
# success or fail generic scripts to write to patrol monitor log
STATUS_SCRIPT=${COMMON}/status.sh
# storing all patrol monitored log directory
APPPATROL_LOG=${APPBATCH}/patrol_mon_log
DBPATROL_LOG=${DBBATCH}/patrol_mon_log
# ERR control file
ERR_CTL=${COMMON}/ERR.ctl
FTP_ERR_CTL=${COMMON}/FTP_ERR.ctl
# backup directory
DUMPPATH=/dump/sybase15.5
FULLDUMPPATH=/dump/sybase15.5
DUMPPATH1=/dump/sybase15.5/dump1
DUMPPATH2=/dump/sybase15.5/dump2
# DR backup directory
DRDUMPPATH=
# main e-mail recipient list
EMAIL_LIST=''
# 16K Data Cache Size for Day time and Night time
DAYDCSIZE=20M
NIGHTDCSIZE=200M
# Execute dump transaction log to a file for application databases
TRANFILE=no
# Execute dump transaction with truncate only for databases listed
# Note dbccdb uses housekeep_dbcc.sh due to specific requirements and shouldn't be included
TRANTRUNCDBLIST="master model sybsystemdb sybsystemprocs teraview1 rd1 central rpt1 workdb"

The script should sftp all dump files for all DBs. Even if one database is missing it should skip that and move to next one without failing..

We are not exporting anything. Just copying the .dump files for all databases.

Appreciate Your Help!!!

Last edited by nmm_dba; 04-24-2013 at 09:50 AM..
# 6  
Old 04-24-2013
Hi Robin,
Awaiting your reply to my above post...
# 7  
Old 04-24-2013
Sorry, rather busy yesterday.

You could set up the loop something like:-
Code:
for dumpfile in `find /dump/sybase15.5 -type f -name "*.dump"`
do
   #Setup the batch file for sftp here if you have variations
   sftp -b batchfile sybase@${REMOTEHOST}
   if [ $? -ne 0 ]
   then
      print "An error occured with sending file $dumpfile"
   fi
done

Does that get you anywhere?



Robin
This User Gave Thanks to rbatte1 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question