LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-08-2016, 01:34 PM   #1
JockVSJock
Senior Member
 
Registered: Jan 2004
Location: SATX
Distribution: RHEL/CentOS
Posts: 1,204
Blog Entries: 4

Rep: Reputation: 131Reputation: 131
Script Issue with Tar Command: Either tar file created in wrong directory or not at all


I'm written a bash script to do the following:

- using find, grab all .aud files created daily in a certain directory

- archive them in .tar.gz format in the same directory

- give the archive a meaningful name with the hostname, file type and date of files created

- using scp/ssh keys, move .tar.gz from the source to a logging server for storage

Here is the code

Code:
find /ora/oracle/admin/RHNSatDB/adump -ctime -1 -and -name '*.aud' | xargs tar -czvf /ora/oracle/admin/RHNSat/adump "$(hostname -s).aud-$(date --date="24 hours ago" '+%F').tar.gz"

sleep 30

scp /ora/oracle/admin/RHNSatDB/adump/*.tar.gz oracle@XXX.XX.XXX.XXX:/logdata/linux_servers/ameda4aisrx0234/2016/jun

sleep 30

rm -f /ora/oracle/admin/RHNSatDB/adump/*.tar.gz
Number of issues where the script isn't working.

For the xargs piece, if I don't use the absolute path, the .tar.gz file ends up where the cron job is ran from (/usr/local/bin)

If I use the absolute path of /ora/oracle/admin/RHNSatDB/adump, I get a number of errors:

Code:
[oracle@hostname bin]$ ./234copy235.cron
tar: /ora/oracle/admin/RHNSat/adump: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
tar: hostname.aud-2016-06-07.tar.gz: Cannot stat: No such file or directory
tar: Removing leading `/' from member names
/ora/oracle/admin/RHNSatDB/adump/RHNSatDB_ora_13380_20160607180002450914143795.aud
/ora/oracle/admin/RHNSatDB/adump/RHNSatDB_ora_13378_20160607180002017434143795.aud
xargs: tar: terminated by signal 13
...and not tar file.

I read thru this epic thread:

http://www.linuxquestions.org/questi...-names-269508/

Made the following change with the tar code:

Code:
/bin/tar -czf /ora/oracle/admin/RHNSatDB/adump "$(hostname -s).aud-$(date --date="24 hours ago" '+F%').tar.gz -C /ora/oracle/admin/RHNSatDB/adump"

Then I made the following changes here:
Code:
/bin/tar czPf /ora/oracle/admin/RHNSatDB/adump "$(hostname -s).aud-$(date --date="24 hours ago"" '+%F').tar.gz -C /ora/oracle/admin/RHNSatDB/adump"
This time, coming up with

Code:
/bin/tar: /ora/oracle/admin/RHNSatDB/adump: Cannot open/bin/tar: : Is a directory
/bin/tar: Error is not recoverable: exiting now
ameda4aisrx0234.aud-2016-06-07.tar.gz -C /ora/oracle/admin/RHNSatDB/adump: Cannot stat: No such file or directory
/bin/tar: Child returned status 2
/bin/tar: Error exit delayed from previous errors

For the tar files that are successfully created. I test them on the logging server and notice that when untarring them, they have the full absolute path (/ora/oracle/admin/RHNSatDB/adump), which I don't want. I just want to be able to plop this .tar.gz to a directory, untar it and look at files.


So after posting this and reading more online, I believe it has alot to do with -C and -P flags with the tar command. I'm not sure if I'm using them correctly or not.

thanks

Last edited by JockVSJock; 06-08-2016 at 01:35 PM.
 
Old 06-08-2016, 03:22 PM   #2
dab1414
Member
 
Registered: May 2011
Location: OK, USA
Distribution: Slackware 14.1_64
Posts: 70

Rep: Reputation: 50
Quote:
Originally Posted by JockVSJock View Post

Code:
find /ora/oracle/admin/RHNSatDB/adump -ctime -1 -and -name '*.aud' | xargs tar -czvf /ora/oracle/admin/RHNSat/adump "$(hostname -s).aud-$(date --date="24 hours ago" '+%F').tar.gz"
this would be the way I might change that line to
Code:
find /ora/oracle/admin/RHNSatDB/adump -ctime -1 -name '*.aud' -type f -print0 | xargs -0 tar -C /ora/oracle/admin/RHNSatDB/adump -czvf $(hostname -s).aud-$(date --date="24 hours ago" +%F).tar.gz
 
Old 06-09-2016, 10:22 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,437

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
Actually the above will have the issue that it was created in the current directory so you need a combo and then you need to input only the names and not the full path from the find, so something like:
Code:
find /ora/oracle/admin/RHNSatDB/adump -ctime -1 -and -name '*.aud' -printf "%f\n" | xargs tar -C /ora/oracle/admin/RHNSatDB/adump -czvf /ora/oracle/admin/RHNSatDB/adump/$(hostname -s).aud-$(date --date="24 hours ago" +%F).tar.gz
 
2 members found this post helpful.
Old 06-09-2016, 02:49 PM   #4
JockVSJock
Senior Member
 
Registered: Jan 2004
Location: SATX
Distribution: RHEL/CentOS
Posts: 1,204
Blog Entries: 4

Original Poster
Rep: Reputation: 131Reputation: 131
Quote:
Originally Posted by grail View Post
Actually the above will have the issue that it was created in the current directory so you need a combo and then you need to input only the names and not the full path from the find, so something like:
Code:
find /ora/oracle/admin/RHNSatDB/adump -ctime -1 -and -name '*.aud' -printf "%f\n" | xargs tar -C /ora/oracle/admin/RHNSatDB/adump -czvf /ora/oracle/admin/RHNSatDB/adump/$(hostname -s).aud-$(date --date="24 hours ago" +%F).tar.gz
Great, that worked.

I don't understand

Code:
"%f\n"
At the end of the find statement.


Also removing

Code:
" "
From where the date is modified?
 
Old 06-09-2016, 10:18 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,437

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
man find and look at -printf and its' format options to get "%f\n"

I didn't think I changed anything for the date part, so I am not sure where " " was removed from? oh, wait, you mean from around the whole date part ... no bigee, you can put those back if you like
 
  


Reply

Tags
find, tar, xargs


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how can i decompress this tar.tar file? hmmm sounds new.. tar.tar.. help ;) kublador Linux - Software 14 10-25-2016 02:48 AM
[SOLVED] command for tar'ing multiple sub-directories into a single tar file blainemiller Linux - Server 3 04-21-2011 02:05 PM
Script to find tar archives, read tar file contents, output content to an index file. bluesword1969 Linux - General 4 02-07-2011 12:15 PM
gave wrong syntax for tar as tar -cvzf file file.tgz how to recover the file gautham Linux - General 4 04-13-2005 03:15 AM


All times are GMT -5. The time now is 12:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration