Sponsored Content
Top Forums Shell Programming and Scripting Help with Backup Shell Script 'ksh + awk over ssh' Post 302459679 by TigerGoods on Wednesday 6th of October 2010 01:23:49 AM
Old 10-06-2010
Code:
bash-3.00$ cat script.ksh
#!/usr/bin/ksh
#------------------------------------------------------
# ./${0##*/} ${remote_host} #Useage
#------------------------------------------------------


#------------------------------------------------------
# Definicion
#------------------------------------------------------

this_script=${0##*/}
remote_host=${1}

export TIMEFORMAT='%Y%m%d%H%M%S'              # time format for logfile names
export TIMESTAMP=$(date +"${TIMEFORMAT}")

#export MOVED=report.${TIMESTAMP}.$$

[ -z "${AUTOMATION_DIR}" ] && { echo "Please set export AUTOMATION_DIR=<automation_dir_path> in $HOME/.bashrc" ; return 1 ; }
[ -z "${COSMOS_DIR}" ] && { echo "Please set export COSMOS_DIR=<cosmos_dir_path> in $HOME/.bashrc" ; return 1 ; }
[ -z "${COSMOS_GCM_DIR}" ] && { echo "Please set export COSMOS_GCM_DIR=<cosmos_gcm_dir_path> in $HOME/.bashrc" ; return 1 ; }
[ -z "${CDS_REPOSITORY_DIR}" ] && { echo "Please set export CDS_REPOSITORY_DIR=<CDS_REPOSITORY_DIR> in $HOME/.bashrc" ; return 1 ; }

[ -z "${remote_host}" ] && { echo "Please use ${this_script} host . If the report tar file is already in tmp then type ${this_script} localhost" ; return 1 ; }

#------------------------------------------------------
# Funktions
#------------------------------------------------------

function remote_cmd
{
  #sub_count=$((sub_count + 1 )) ; _remote_cmd=$FUNC ; FUNC=remote_cmd

  loginhost="$1"
  cmd_string="$2"
  cmd_login='eval ' ;
  localhost="$(hostname)" ;

  [ "$loginhost" = 'localhost' ] || [ "$loginhost" != "$localhost" ]&& cmd_login="ssh $loginhost " ;

  #msgout "L: ${FUNC}: $cmd_login $cmd_string" ;

  $cmd_login "$cmd_string" ; ret_val=$?

  #sub_count=$(($sub_count-1)) ; FUNC=$_remote_cmd
  return $ret_val
}

function remote_cmd_cp
{
  #sub_count=$((sub_count + 1 )) ; _remote_cmd_cp=$FUNC ; FUNC=remote_cmd_cp

  loginhost="$1"
  cmd_string="$2"
  cmd_login='eval ' ;
  localhost="$(hostname)" ;
  filename=$($cmd_string | awk -F"/" '{print $3}')

  [ "$loginhost" = 'localhost' ] || [ "$loginhost" != "$localhost" ]&& cmd_login="ssh $loginhost " ;

  #msgout "L: ${FUNC}: $cmd_login $cmd_string" ;

  $cmd_login "$cmd_string" > $filename ; ret_val=$?

  #sub_count=$(($sub_count-1)) ; FUNC=$_remote_cmd
  return $ret_val
}

function remote_cmd_test
{
  _host=$1

  ARCHROOTDIR="/tmp"
  ARCHMOVED="$ARCHROOTDIR/report_moved"


  cmd_string="\
      cd ; \
      [ !-d \${ARCHROOTDIR} ] && { echo "${ARCHROOTDIR} doesn't exist!" ; return 1 ; } \
      LS_REPT=$(ls -altr report_*.tar | awk -F" " '{print $9}') ; \
      [ -z "${LS_REPT}" ] && { echo "Reports in ${ARCHROOTDIR} doesn't exist" ; return 1 ; } \
      [ !-d \${ARCHMOVED} ] && { mkdir -p -m 775 \"${ARCHMOVED}" ; } \
  "

  remote_cmd "${_host}" "$cmd_string" ; ret_val=$?
}

function remote_cmd_cp_str
{
  _host=$1

  ARCHROOTDIR="/tmp"
  ARCHMOVED="$ARCHROOTDIR/report_moved"
  LS_REPC='ls -1tr /tmp/report_* | tail -1'
  echo $LS_REPC

  cmd_string="\${LS_REPC}"

  remote_cmd_cp "${_host}" "$cmd_string" ; ret_val=$? ;
}

function remote_cmd_mv
{
  _host=$1

  ARCHROOTDIR='/tmp'
  ARCHMOVED="$ARCHROOTDIR/report_moved"
  LS_REPM=$(ls -altr report_*.tar | awk -F" " '{print $9}')
  MV_NAME=$( echo ${LS_REPM} | awk -F"." '{print $1"." $2"." $3"." $4"." $5"." "tar_moved"}' )


  cmd_string="cd /tmp \
      for list in ${LS_REPM}; do ${list} ${ARCHMOVED}/${MV_NAME}; done \
      cd - ; \
  "

  remote_cmd "${_host}" "${cmd_string}" ; ret_val=$? ;
}


function tar_test
{

  ARCHROOTDIR="/tmp"
  [ ! -d ${ARCHROOTDIR} ] && { echo "${ARCHROOTDIR} doesn't exist!" ; return 1 ; }
  cd ${ARCHROOTDIR}
  ARCHUP="$ARCHROOTDIR/report_unpacked"

  LS_REP=$(ls -altr report_*.tar | awk -F" " '{print $9}') ;
  [ -z "${LS_REP}" ] && { echo "Reports in ${ARCHROOTDIR} doesn't exist" ; return 1 ; }
  [ ! -d "${ARCHUP}" ] && { mkdir -p -m 775 ${ARCHUP} ; }
  cd - ;

  tar_cmd; ret_val=$? ;
}


function tar_cmd
{

  ARCHROOTDIR="/tmp"
  cd ${ARCHROOTDIR}
  LS_REP=$(ls -altr report_*.tar | awk -F" " '{print $9}')
  REP_HOST=$(ls -altr report_*.tar | awk -F" " '{print $9}' | cut -c 8- | cut -f1 -d'-')
  UP_NAME=$(echo ${LS_REP} | awk -F"." '{print $1"." $2"." $3"." $4"." $5"." "tar_unpacked"})
  ARCHUP="$ARCHROOTDIR/report_unpacked"

  for list in ${LS_REP} ; do
        [ ! -d ${CDS_REPOSITORY_DIR}/${REP_HOST} ] && { tar -xvf "${list}" "${CDS_REPOSITORY_DIR}" ; } && \
        { svn add "${CDS_REPOSITORY_DIR}/${REP_HOST}" ; } && \
        { svn ci "${CDS_REPOSITORY_DIR}/${REP_HOST}" ; } && \
        { ret_val=$? ; }
    done

  for list2 in ${LS_REP} ; do
        [ -d ${CDS_REPOSITORY_DIR}/${REP_HOST} ] && { tar -xvf "${list2} ${CDS_REPOSITORY_DIR}" ; } && \
        { svn co "${CDS_REPOSITORY_DIR}/${REP_HOST}" ; } && \
        { svn add --force "${CDS_REPOSITORY_DIR}/${REP_HOST}" ; } && \
        { svn ci "${CDS_REPOSITORY_DIR}/${REP_HOST}" ; } && \
        { ret_val=$? ; } && \
    done

  for list in ${LS_REP}; do { mv ${LS_REP} ${ARCHUP}/${UP_NAME} ; } done
  cd -
  return $ret_val

}

function main
{
_host=$1

[ ${_host} = 'localhost' ] && { tar_test ; }
[ ${_host} != 'localhost' ] && { remote_cmd_test "$_host"; remote_cmd_cp_str "$_host"; remote_cmd_mv "$_host"; tar_test ; }
}

#------------------------------------------------------
# Main
#------------------------------------------------------


main "$remote_host";

# EOF

Code:
bash-3.00$ ksh -xv script.ksh localhost 2>&1 | more
#!/usr/bin/ksh
#------------------------------------------------------
# ./${0##*/} ${remote_host} #Useage
#------------------------------------------------------


#------------------------------------------------------
# Definicion
#------------------------------------------------------

this_script=${0##*/}
+ this_script=script.ksh
remote_host=${1}
+ remote_host=localhost

export TIMEFORMAT='%Y%m%d%H%M%S'              # time format for logfile names
+ export TIMEFORMAT=%Y%m%d%H%M%S
export TIMESTAMP=$(date +"${TIMEFORMAT}")
+ date +%Y%m%d%H%M%S
+ export TIMESTAMP=20101005132606

#export MOVED=report.${TIMESTAMP}.$$

[ -z "${AUTOMATION_DIR}" ] && { echo "Please set export AUTOMATION_DIR=<automation_dir_path> in $HOME/.bashrc" ; return 1 ; }
+ [ -z /home/pjt130/automation ]
[ -z "${COSMOS_DIR}" ] && { echo "Please set export COSMOS_DIR=<cosmos_dir_path> in $HOME/.bashrc" ; return 1 ; }
+ [ -z /home/pjt130/cosmos ]
[ -z "${COSMOS_GCM_DIR}" ] && { echo "Please set export COSMOS_GCM_DIR=<cosmos_gcm_dir_path> in $HOME/.bashrc" ; return 1 ; }
+ [ -z /home/pjt130/cosmos_gcm ]
[ -z "${CDS_REPOSITORY_DIR}" ] && { echo "Please set export CDS_REPOSITORY_DIR=<CDS_REPOSITORY_DIR> in $HOME/.bashrc" ; return 1 ; }
+ [ -z /home/pjt130/cds_repository ]

[ -z "${remote_host}" ] && { echo "Please use ${this_script} host . If the report tar file is already in tmp then type ${this_script} localhost" ; retur
n 1 ; }
+ [ -z localhost ]

#------------------------------------------------------
# Funktions
#------------------------------------------------------

function remote_cmd
{
  #sub_count=$((sub_count + 1 )) ; _remote_cmd=$FUNC ; FUNC=remote_cmd

  loginhost="$1"
  cmd_string="$2"
  cmd_login='eval ' ;
  localhost="$(hostname)" ;

  [ "$loginhost" = 'localhost' ] || [ "$loginhost" != "$localhost" ]&& cmd_login="ssh $loginhost " ;

  #msgout "L: ${FUNC}: $cmd_login $cmd_string" ;

  $cmd_login "$cmd_string" ; ret_val=$?

  #sub_count=$(($sub_count-1)) ; FUNC=$_remote_cmd
  return $ret_val
}

function remote_cmd_cp
{
  #sub_count=$((sub_count + 1 )) ; _remote_cmd_cp=$FUNC ; FUNC=remote_cmd_cp

  loginhost="$1"
  cmd_string="$2"
  cmd_login='eval ' ;
  localhost="$(hostname)" ;
  filename=$($cmd_string | awk -F"/" '{print $3}')

  [ "$loginhost" = 'localhost' ] || [ "$loginhost" != "$localhost" ]&& cmd_login="ssh $loginhost " ;

  #msgout "L: ${FUNC}: $cmd_login $cmd_string" ;

  $cmd_login "$cmd_string" > $filename ; ret_val=$?

  #sub_count=$(($sub_count-1)) ; FUNC=$_remote_cmd
  return $ret_val
}

function remote_cmd_test
{
  _host=$1

  ARCHROOTDIR="/tmp"
  ARCHMOVED="$ARCHROOTDIR/report_moved"


  cmd_string="\
      cd ; \
      [ !-d \${ARCHROOTDIR} ] && { echo "${ARCHROOTDIR} doesn't exist!" ; return 1 ; } \
      LS_REPT=$(ls -altr report_*.tar | awk -F" " '{print $9}') ; \
      [ -z "${LS_REPT}" ] && { echo "Reports in ${ARCHROOTDIR} doesn't exist" ; return 1 ; } \
      [ !-d \${ARCHMOVED} ] && { mkdir -p -m 775 \"${ARCHMOVED}" ; } \
  "

  remote_cmd "${_host}" "$cmd_string" ; ret_val=$?
}

function remote_cmd_cp_str
{
  _host=$1

  ARCHROOTDIR="/tmp"
  ARCHMOVED="$ARCHROOTDIR/report_moved"
  LS_REPC='ls -1tr /tmp/report_* | tail -1'
  echo $LS_REPC

  cmd_string="\${LS_REPC}"

  remote_cmd_cp "${_host}" "$cmd_string" ; ret_val=$? ;
}

function remote_cmd_mv
{
  _host=$1

  ARCHROOTDIR='/tmp'
  ARCHMOVED="$ARCHROOTDIR/report_moved"
  LS_REPM=$(ls -altr report_*.tar | awk -F" " '{print $9}')
  MV_NAME=$( echo ${LS_REPM} | awk -F"." '{print $1"." $2"." $3"." $4"." $5"." "tar_moved"}' )


  cmd_string="cd script.ksh[69]: syntax error at line 112 : `"

  remote_cmd "${_host}" "$cmd_string" ; ret_val=$?
}

function remote_cmd_cp_str
{
  _host=$1

  ARCHROOTDIR="/tmp"
  ARCHMOVED="$ARCHROOTDIR/report_moved"
  LS_REPC='ls -1tr /tmp/report_* | tail -1'
  echo $LS_REPC

  cmd_string="${LS_REPC}"

  remote_cmd_cp "${_host}" "$cmd_string" ; ret_val=$? ;
}

function remote_cmd_mv
{
  _host=$1

  ARCHROOTDIR='/tmp'
  ARCHMOVED="$ARCHROOTDIR/report_moved"
  LS_REPM=$(ls -altr report_*.tar | awk -F" " "{print $9}")
  MV_NAME=$( echo ${LS_REPM} | awk -F"." "{print $1"." $2"." $3"." $4"." $5"." "tar_moved"}" )


  cmd_string="cd' unexpected
bash-3.00$



---------- Post updated 06-10-10 at 12:23 AM ---------- Previous update was 05-10-10 at 06:26 AM ----------

New day, new hope... Smilie
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Testing ssh connection from KSH script

Hi. I have a kornshell script that runs on a daily basis as a cron job. Part of what the script does is copy the folder contents from another server to the current server (server where KSH script is running). I have a scp command, as follows: scp $REMOTE_HOST:$REMOTE_FILE_DIR/* $TMP_DIR ... (8 Replies)
Discussion started by: dmilks
8 Replies

2. UNIX for Advanced & Expert Users

Use of sudoer with ssh login shell script (KSH)

Greetings all, I'm in the midst of writing a login component for a series of shell scripts. What my login script does is this: 1. Prompt for username and read in username 2. Prompt for destination host and read in destination host 3. run ssh username and destination host 4. After user keys... (0 Replies)
Discussion started by: rockysfr
0 Replies

3. Shell Programming and Scripting

ssh into a shell script (KSH)

Hi all, Just like to ask if it is possible to do the following: 1. Have a shell script that calls ssh username@destinationhost 2. Upon successful verification, we ssh into the destination host and automatically use ksh to run a shell script that resides in the destination host. (Hopefully no... (8 Replies)
Discussion started by: rockysfr
8 Replies

4. Shell Programming and Scripting

Awk in ksh shell script - help

Hello, Im a beginner. Im writing a ksh script with awk. Is it possible to assign the output of the awk to a shell variable? Like, shell_variable= awk '$1 == "shell" {abc= $2 }' /tmp/cust_det echo $shell_variable Please excuse my ignorance. Thanks in advance. (4 Replies)
Discussion started by: Nic_writes
4 Replies

5. Shell Programming and Scripting

ksh script with Interactive ssh on remote server

HI Unix Gurus, I an stuck in an interesting issue, where I am trying to execute a script on remote server after ssh. The script on remote server is interactive,. Whenever it is called it hangs where it expects input from terminal and I have to terminate it. I have searched through fourm... (12 Replies)
Discussion started by: Jeevanm
12 Replies

6. Shell Programming and Scripting

issue on ssh command in ksh shell

Hi guru, I'm making crazy cause an issue on a ksh shell I made. In this shell I want to execute unix command on a remote machine using an ssh connection like ssh user@host 'command'..... The command is very simply, is an ls on a remote directory but it give me an unexpected result. The... (4 Replies)
Discussion started by: leobdj
4 Replies

7. Shell Programming and Scripting

Help with Backup Shell Script for Network Device Configuration backup

HI all, im new to shell scripting. need your guidence for my script. i wrote one script and is attached here Im explaining the requirement of script. AIM: Shell script to run automatically as per scheduled and backup few network devices configurations. Script will contain a set of commands... (4 Replies)
Discussion started by: saichand1985
4 Replies

8. Shell Programming and Scripting

Help with ssh ksh script

Hi, I am trying to figure out a ksh script that i have and i think i found it but not sure. i am having to scp or sftp files from my remote server over 2 others to the destination. i have the rsa keys setup for the servers on my end and the username and password for the final server. ... (3 Replies)
Discussion started by: ksh_beginner
3 Replies

9. Shell Programming and Scripting

Backup script in ksh

I need a script in ksh that -automatically connect to computers in a network and identify directories in remote computers -then mount directories on to the local host where the script is runned -then backup directories in the local host and the remote systems one by one using tar command (to... (9 Replies)
Discussion started by: diw10
9 Replies

10. UNIX for Beginners Questions & Answers

How to have local shell variables in a ksh script seen on remove server in SSH block?

I have googled this and found many solutions, but none of them are working for me. I am in a korn shell, most others reference bsh, maybe that is the issue? Anyway, all I am trying to do is use a variable I have declared in my main script in a remote shell I am running through ssh. So I have a... (8 Replies)
Discussion started by: DJR
8 Replies
All times are GMT -4. The time now is 11:11 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy