Grab and import script

I need to create a script tu unzip and call other script...below is the flow. the script will run everynight at 12.

filename: /tmp/file.zip
script to import: /tmp/import.php

1) check the file.zip existing in /tmp folder...
2) if not exist, create a log /var/log/myscript.log [ date, time, file.zip is not exists ]
3) if exist, do unzip /tmp/file.zip and it will have file name file.csv
4) after unzip is finished ( which is quite big size of file.csv, > 30MB ) write log to /var/log/myscript.log [date,time, unzip is successfull 31MB]
5) then only run import script to sql, "import-command /tmp/import.php"
6) after finished, write log to /var/log/myscript.log [date, time, import success ]

how do i start?

#/bin/bash


operation1612Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

operation1612Author Commented:
finally it will remove the file.zip and file.csv
0
ravenplCommented:
#!/bin/bash
filename="/tmp/file.zip"
toimport="/tmp/import.php"
LOGTO=/var/log/myscript.log

if [ -s "$filename" ]; then
 unzip "$filename" -d /tmp #-d tells where to unzip
 [ -s "/tmp/file.csv" ] || { echo "`date`: file.csv not found in the zipfile" >> $LOGTO; exit 1; };
 echo "`date`: file.csv unzipped OK `wc -c /tmp/file.csv`" >> $LOGTO
 import-command /tmp/import.php # what You mean by import-command? Maybe simple: /tmp/import.php /tmp/file.csv # would suffice?
 echo "`date`: file.csv imported OK" >> $LOGTO
else
 echo "`date`: $filename not found" >> $LOGTO;
 exit 1;
fi
0
ravenplCommented:
If import-command return well defined exit codes

 if import-command /tmp/import.php; then
   echo "`date`: file.csv imported OK" >> $LOGTO
   rm -f "$filename" /tmp/file.csv
 else
  echo "`date`: file.csv import FAILED" >> $LOGTO
  #for any sake
  rm -f /tmp/file.csv
  mv -f "$filename" "$filename.failed"
 fi
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

ravenplCommented:
And if You like, You may add more error checking...
0
operation1612Author Commented:
like this?

#!/bin/bash
filename="/tmp/file.zip"
toimport="/tmp/import.php"
LOGTO=/var/log/myscript.log

if [ -s "$filename" ]; then
 unzip "$filename" -d /tmp #-d tells where to unzip
 [ -s "/tmp/file.csv" ] || { echo "`date`: file.csv not found in the zipfile" >> $LOGTO; exit 1; };
 echo "`date`: file.csv unzipped OK `wc -c /tmp/file.csv`" >> $LOGTO
/tmp/import.php /tmp/file.csv
 echo "`date`: file.csv imported OK" >> $LOGTO
else
 echo "`date`: $filename not found" >> $LOGTO;
 exit 1;
fi

if import-command /tmp/import.php; then
   echo "`date`: file.csv imported OK" >> $LOGTO
   rm -f "$filename" /tmp/file.csv
 else
  echo "`date`: file.csv import FAILED" >> $LOGTO
  #for any sake
  rm -f /tmp/file.csv
  mv -f "$filename" "$filename.failed"
 fi


My Question as below code..between two process, do i need to add break; between fi and if?

echo "`date`: file.csv imported OK" >> $LOGTO
else
 echo "`date`: $filename not found" >> $LOGTO;
 exit 1;
fi

if import-command /tmp/import.php; then
   echo "`date`: file.csv imported OK" >> $LOGTO
   rm -f "$filename" /tmp/file.csv
 else
  echo "`date`: file.csv import FAILED" >> $LOGTO
0
ravenplCommented:
> My Question as below code..between two process, do i need to add break; between fi and if?
I'm not sure I understand Your Q. Please type 'help if' and also note, that every ';' may be replaced with newline break. That's all.
0
operation1612Author Commented:
i mean is below code is correct because normally i saw script use command break;?

#!/bin/bash
filename="/tmp/file.zip"
toimport="/tmp/import.php"
LOGTO=/var/log/myscript.log

if [ -s "$filename" ]; then
 unzip "$filename" -d /tmp #-d tells where to unzip
 [ -s "/tmp/file.csv" ] || { echo "`date`: file.csv not found in the zipfile" >> $LOGTO; exit 1; };
 echo "`date`: file.csv unzipped OK `wc -c /tmp/file.csv`" >> $LOGTO
/tmp/import.php /tmp/file.csv
 echo "`date`: file.csv imported OK" >> $LOGTO
else
 echo "`date`: $filename not found" >> $LOGTO;
 exit 1;
fi

if import-command /tmp/import.php; then
   echo "`date`: file.csv imported OK" >> $LOGTO
   rm -f "$filename" /tmp/file.csv
 else
  echo "`date`: file.csv import FAILED" >> $LOGTO
  #for any sake
  rm -f /tmp/file.csv
  mv -f "$filename" "$filename.failed"
 fi
0
ravenplCommented:
break exits from within 'loop', not 'if'.

#!/bin/bash
filename="/tmp/file.zip"
toimport="/tmp/import.php"
LOGTO=/var/log/myscript.log

if [ -s "$filename" ]; then
 unzip "$filename" -d /tmp #-d tells where to unzip
 [ -s "/tmp/file.csv" ] || { echo "`date`: file.csv not found in the zipfile" >> $LOGTO; exit 1; };
 echo "`date`: file.csv unzipped OK `wc -c /tmp/file.csv`" >> $LOGTO
 if import-command /tmp/import.php; then # what is the real import-command?
   echo "`date`: file.csv imported OK" >> $LOGTO
   rm -f "$filename" /tmp/file.csv
 else
  echo "`date`: file.csv import FAILED" >> $LOGTO
  #for any sake
  rm -f /tmp/file.csv
  mv -f "$filename" "$filename.failed"
 fi
else
 echo "`date`: $filename not found" >> $LOGTO;
 exit 1;
fi
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
operation1612Author Commented:
the real import command is /usr/local/bin/php -q

/usr/local/bin/php -q /tm/import.php

the import.php will import the csv to mysql
0
operation1612Author Commented:
can i do this?  > toimport="/usr/local/bin/php -q"
0
ravenplCommented:
rather toimport="/usr/local/bin/php -q /tmp/import.php"
but one advice, don't keep the import.php in /tmp dir, it;s cleaned periodically.
0
operation1612Author Commented:
ok thank you...
toimport="/usr/local/bin/php -q /tmp/import.php"

so finally in this line [if import-command /tmp/import.php;] i should change to :

if $toimport; then

correct?
0
ravenplCommented:
correct
0
operation1612Author Commented:
thank you
0
operation1612Author Commented:
i need to ask one little question related to this thread.

i test the script and it works fine...i want to move the existing file.zip to /backup/. after process is finished, script will transfer to backup directory and name it by the date. for example fileDDMMYYYY.zip

is it this correct?

#for any sake
  rm -f /tmp/file.csv
  mv -f "$filename" "/backup/$filename.'date' "  <<<<<<
0
ravenplCommented:
yes, it's crrect, but it's executed only, if import fails
0
operation1612Author Commented:
if import not fails? the command also will move to /backup directory is it?
0
ravenplCommented:
Yes, but it have to copied to the other branch of 'if' as well then (instead rm -f)
0
operation1612Author Commented:
ok great...i test it and yes the file is move to folder /backup by filename.date, because of the filename is quite big...so i plan to keep it for 4 file only based on the latest date...mean after script run again in future it will do checking either the filename.date in backup directory is not more than 4. old file by date will be remove...

do i need to create new question?
0
ravenplCommented:
rather try searching for nswer here, or for Yourself
hint: find -mtime
0
operation1612Author Commented:
ok great...thanks... the -mtime...i got it
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.