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
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"
scp /ora/oracle/admin/RHNSatDB/adump/*.tar.gz oracle@XXX.XX.XXX.XXX:/logdata/linux_servers/ameda4aisrx0234/2016/jun
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:
[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
xargs: tar: terminated by signal 13
...and not tar file.
I read thru this epic thread:
Made the following change with the tar 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:
/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
/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.