• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 340
  • Last Modified:

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


0
operation1612
Asked:
operation1612
  • 11
  • 10
1 Solution
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 11
  • 10
Tackle projects and never again get stuck behind a technical roadblock.
Join Now