Pages

Tuesday, October 14, 2014

Linux bash script by example. scp files from server A to server B. Then ftp files from server B to server C.

General
Example for ksh script on a Linux server.

Server A generates files in format X1.
On server A, using crontab, call bash script.
The script would send files to sever B.

On server B, using crontab, call bash script.
The script would call java lar file, to format files to format X2, and then send formatted files to server B, using ftp.

crontab and scripts

On server A

crontab entry
#run every hour, one minute past the hour.
01 0-23/1 * * * /homedir/ftm/ftm_new.sh

script
/homedir/ftm/ftm_new.sh

#!/bin/bash
if [ ! -d "/homedir/ftm/temp/" ]; then
  mkdir /homedir/ftm/temp/
fi

xxx=`date --date "$dte -1 hour" +%d/%m/%Y" "%H`
xx1=`date --date "$dte -1 hour" +%Y-%m-%d-%H`

cd /homedir/product/cust/data/
if [ -f file_name.$(date --date "$dte -1 hour" +%Y-%m-%d-%H)* ]
then
    cp file_name.$(date --date "$dte -1 hour" +%Y-%m-%d-%H)* /homedir/ftm/temp/
else
        echo "#/bin/bash" > do.sh
        echo "awk -F \";\" '\$63~\""$xxx"\" {print}' file_name.dat>/homedir/ftm/temp/file_name."$xx1".dat">> do.sh
        chmod 777 do.sh
        ./do.sh
fi
cd /homedir/ftm/temp/
scp /homedir/ftm/temp/* user@999.999.999.999:/homedir/product/cust/incoming/

if [ "$?" -eq 0 ]; then
        rm /homedir/ftm/temp/*
else
        send_snmp.sh Critical "Failed to copy files to Server"
fi


On server B
crontab entry
05 0-23/1 * * * /homedir/product/cust/bin/process_file_util.sh
#run every hour, five minutes past the hour.

script
#!/bin/bash

#$baseDir=/homedir/product/cust/
#$inputDir=$baseDir.incoming
#$tempDir=$baseDir.temp
date1=`date +"%d/%m/%Y %H:%M:%S"`

echo "Start Process "$date1 >> /homedir/product/cust/logs/log.txt

cd /homedir/product/cust/bin

mv /homedir/product/cust/incoming/file_name*.dat /homedir/product/cust/temp
if [ "$?" -eq 0 ]; then

  echo "Files successfully copied to converter temp dir" >> /homedir/product/cust/logs/log.txt

  # Initialize the $JAVA_HOME environment variable
  source /homedir/product/cust/conf/user_profile

  # Run the Java utility to convert the SDR files to output format
  $JAVA_HOME/bin/java -cp ../lib/fileutil.jar com.homedir.utils.FileUtil -inputfolder /homedir/product/cust/temp -outputfolder /homedir/product/cust/output/ -param1 PRM01 >> /homedir/product/cust/logs/log.txt
  # $1 input folder of files ( ../data )
  # $2 output folder of generated files (../data)
  # $3 code name (e.g. PRM01)

  # Check if the converter was able to run
  if [ "$?" -eq 0 ]; then

    echo "Converter successfull run" >> /homedir/product/cust/logs/log.txt

    # Change directory to the converter's output directory
    cd  /homedir/product/cust/output/

# Create the FTP INI file to send all files to output
    echo "ftp -n -i 999.999.999.999 <<END_SCRIPT" > /homedir/product/cust/bin/ftp.sh
    echo "quote USER my_user"  >> /homedir/product/cust/bin/ftp.sh
    echo "quote PASS my_pass" >> /homedir/product/cust/bin/ftp.sh

    # We write to the ftp.sh script all files to be sent to output, except the first file modified,
    # as this file was sent in the previous time the script was run,
    # and was only left in the directory to allow the converter to start numbering the files from this file number
    echo "lcd /homedir/product/cust/output/" >> /homedir/product/cust/bin/ftp.sh
    for f in `ls -tr FILE_NAME* | sed 1,1d`; do
        echo "put "$f >> /homedir/product/cust/bin/ftp.sh
    done
    echo "bye" >> /homedir/product/cust/bin/ftp.sh
    echo "END_SCRIPT" >> /homedir/product/cust/bin/ftp.sh

    # Run the shell script for FTP delivery
    cd /homedir/product/cust/bin/
    chmod 777 ftp.sh
    ./ftp.sh

    # Check if the FTP script completed successfully
    if [ "$?" -eq 0 ]; then
      echo "FTP completed successfully" >> /homedir/product/cust/logs/log.txt

      # Move all converted files, besides the last file, to the archive directory
      # we leave the last file, so that the converter will know from what number to start numbering the new files it has converted
      cd /homedir/product/cust/output/
      for f in `ls -t FILE_NAME* | sed 1,1d`; do
        mv $f /homedir/product/cust/archive/$f
      done

      # Delete all of the *.completed files from the temporary staging directory
      rm /homedir/product/cust/temp/file_name*.completed
      echo "Process End" >> /homedir/product/cust/logs/log.txt
    else
      echo "Problem sending the files via FTP!" >> /homedir/product/cust/logs/log.txt
    fi
  else
    echo "Problem Running the Java Converter!" >> /homedir/product/cust/logs/log.txt
  fi
else
  echo "Could not copy files from the incoming directory to the converter temp directory" >> /homedir/product/cust/logs/log.txt
fi

No comments:

Post a Comment