Hi all. I'm hitting a problem creating a tar archive in one directory from files located in a different directory. It fails when I replace the absolute paths with variables in the script but works if I just run tar on the cmdln. E.g.
I don't understand why you have your tar command embedded in a process substitution:
$(tar -zcvf $STAGE/${FILENAME} ${LOG}/*)
This would cause the output of tar (and you have it in verbose mode) being collected into a shell-variable:
tar_log=$(tar -zcvf $STAGE/${FILENAME} ${LOG}/*)
As you are not doing this I can only imagine the output of tar is being interpreted by your shell process.
As for the leading "/" error, tar is attempting to prevent absolute file paths to be entered into the archive so you don't overwrite anything when extracting the archive. I don't know the -C switch but imagine it should be used thus:
Yeah the 'tar: Removing leading `/' from member names' warning is there for our own benefit so I guess it's simpler to just do everything in 3 steps and get around any issues ...
cd ${LOG} && tar -zcf ${FILENAME} * && cp ${FILENAME} ${STAGE}
How about pax
The compression is not POSIX, but it should work almost everywhere.
Notice you cannot append to compressed archive with pax, if you require such feature.
pax -wzf ${STAGE}/mybackup.tar.gz ${LOG}
Will create compressed archive from ${LOG} directory (everything in it) into ${STAGE}/mybackup.tar.gz
It also supports to substitute paths inside archive in one go, and a lot more, in one line.
Check it out online or on these forums!