Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

script to zip and tar files at once.

Status
Not open for further replies.

tekpr00

IS-IT--Management
Jan 22, 2008
186
0
0
CA
hello All,

I have this in my /etc/oratab
dbase1:/orahome/app/oracle/product/11.2.04/dbbase:N
dbase2:/orahome/app/oracle/product/10.2.05/dbbase:N

The idea is to use a script to loop through each database, mindful that Database HOME is different.
Again, go into directory:
/orahome/app/oracle/admin/dbase1/adump
or
/orahome/app/oracle/admin/dbase2/adump
1) remove all files older than 7 days including old zip and tar files.
2) zip and tar the rest of the files while renaming it and put a timestamp of the day of creation of tarball.

Here is my script
#!/bin/ksh
#
# Script Name: copy_zip_tar.sh
#

ORACLE_SID=`cat /etc/oratab | grep -v "^#" | grep -v "^\*" | grep -v "^agent" | cut -d":" -f1 | sort`
while read ORACLE_SID
do
find /orahome/app/oracle/admin/'$ORACLE_SID'/adump/* -mtime +7 -exec rm{} \
tar cvf /orahome/app/oracle/admin/'$ORACLE_SID'/adump/*aud | gzip -c > '$ORACLE_SID'_aud_files`date +" %b_%d_%Y"`.tar.gz
done
exit

Above is my effort.
I have to admit that I have not tested this.
Please help & thanks for your help in advance.
 
Couple little things wrong. The 'f' parameter to 'tar' will be looking for a filename to create. It won't do what you're wanting it to do. A dash will send its output to the pipeline.

Try this...

Code:
#!/bin/ksh
# Script Name: copy_zip_tar.sh

# Set Inter-Field Separator to colon. Simplifies parsing the input file.
IFS=:

cat /etc/oratab | while read ORA_SID ORA_HOME ORA_START
do
#  find /orahome/app/oracle/admin/${ORA_SID}/adump/* -mtime +7 -exec rm {} \;
# can do the following for testing...
  find /orahome/app/oracle/admin/${ORA_SID}/adump/* -mtime +7 [b]-ls[/b]

  tar cvf - /orahome/app/oracle/admin/${ORA_SID}/adump/*aud | gzip -c > ${ORA_SID}_aud_files_$(date '+%Y%m%d').tar.gz
done

exit

I changed your date format to something that will stay sorted chronologically in a directory listing ("YYYYMMDD"). Sorry, but the format you had is a pet peeve of mine. Change it back if you want/need to.

 
Oops, sorry, your 'find' command needs some help too. I tried to edit my post, but got an error. I'll add it here.

The first parameter of a 'find' should be a directory. It should look more like this...

Code:
find /orahome/app/oracle/admin/${ORA_SID}/adump  -mtime +7 -exec rm {} \;

And as before, you can use a '-ls' to just list the file before testing instead of actually deleting them.

 
FWIW - instead of piping the output to the gzip command why not simply specify tar's -g option which will output a gzipped format file directly?

ex: tar -cgf ./mytarfile.gz files_to_include

Just saves keystrokes and exec time. My 2 cents worth.
 
Excellent suggestion. Just confirm that your 'tar' has the -g option. Some don't.

 
Hello BobMCT & SamBones,

Thanks for your inputs.

However, the requirements have changed a little bit:

In addition, the will like to add the following:
1) On the first day of each month, zip and create a tar file from existing *aud files.
2) Everyday thereafter, compress new *aud files and append it to the to the existing tar file created in (1).


fmonth=`date '+%d'`
if [ $fmonth == 01 ]
do
tar -cgf /orahome/app/oracle/admin/${ORA_SID}/adump/{ORA_SID}_aud_files_$(date '+%B%Y').tar.gz /orahome/app/oracle/admin/${ORA_SID}/adump/*aud
else
tar cvf - /orahome/app/oracle/admin/${ORA_SID}/adump/*aud | gzip -c > /orahome/app/oracle/admin/${ORA_SID}/adump/{ORA_SID}_aud_files_$(date '+%B%Y').tar.gz
done
fi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top