Wednesday, November 25, 2015

Playing with bash file

This is my first command of bash file.. Usually i'm only adding few command which is already there (who already wrote by my SIFU)..

What I'm going to do is.. transfering file from sftp server A to server B and send to S3 amazon. The tricky is sftp part.. :) .. I'm creating 2 file here sftpcommand.txt and script.sh


sftpcommand.txt
+++++++++++++++++++
mget data-2015_11_24*
bye

+++++++++++++++++++


script.sh
++++++++
#!/bin/bash
now="$(date)"         
printf "Current date and time %s\n" "$now"      #date of the day     

now="$(date +'%Y_%m_%d')"
printf "Current date in yyyy_mm_dd format %s\n" "$now"   

#.......
echo "try command"
#echo "data-$(date -d "3 day ago" +'%Y_%m_%d')"
now="data-$(date -d "1 day ago" +'%Y_%m_%d')*"   #1day before current date with wildcard*

echo "startfilename=""$now"  #show the output of $now

echo "changing sftp command"
sed -i "1s/.*/mget $now/" sftpcommand.txt    #changing sftpcommand.txt at 1st line same value with $now

echo "sftp is running"
sftp -b sftpcommand.txt -v -P 9878 -i cthroot.pem root@sftp.server.com:   #sftp bash command of sftpcommand.txt

echo "list"
find /home/aminah/data/ -name "$now" -print  #work
echo "send to s3"
find /home/aminah/data/ -name "$now" -exec aws s3 cp '{}' s3://pathbucket/folder/ \;   #work #sending the same value to s3.. 

echo "end"
+++++++

To check the command run bash  #bash -x script.sh ; it will show the output and easy to troubleshoot. 

And its working like charm.. horey...

1 comment:

  1. You have to make authentication access using this step http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AccessingInstancesLinux.html

    ReplyDelete