LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   tar quirks? (https://www.linuxquestions.org/questions/linux-general-1/tar-quirks-341518/)

Joseph Schiller 07-09-2005 02:58 AM

tar quirks?
 
Hi,

I've been trying to create a simple backup archive using tar, but I'm getting inconsistent results. Here's the script:

#!/bin/bash
# simple backup
OF="/var/archive/backup-$(date +%Y%m%d).tgz
IN="/home/some_file"
tar -cfZ $OF $IN

This creates a 66 byte backup-20050709.tgz file which is empty. What happened to the file? It seems the 'Z' option to compress the file does not work, or I cannot extract the file. I foud that the only way I can make the script work is omit the 'Z' option and forget about pipelines.

#!/bin/bash
OF="/var/archive/backup-$(date +%Y%m%d).tar
IN="/home/some_file"
tar -cvf $OF $IN
gzip $OF

I could not concatenate the command using the pipe | symbol, as in 'tar -cvf $OF $IN | gzip' this generates an error, and nothing gets written. Also, naming the file with 'tgz' extension does not create a valid gzip archive. I'm using the GNU tar 1.13.25 which came with my distro. How can I pipeline an archive to gzip? The above script is sort of misleading in that the output file is first archived then compressed and I don't seem to trust the $OF variable to work 100% even though shell forks seperate processes for these.

Stephane Chazelas has posted a mo' better solution archiving files modified in last 24 hours, and he does not pipe anything to be zipped either:

#!/bin/bash
BACKUPFILE=backup-$(date +%m%d%Y)
ARCHIVE=${ 1:-$BACKUPFILE }
tar cvf - `find . -mtime -1 -type f -print0` | xargs -0 tar rvf "$ARCHIVE.tar"

I could not make this work though. All I get is an emtpy archive. Any suggestions?

Regards,

Joseph

Tinkster 07-09-2005 03:04 AM

The quirk is not with tar but with your usage,
the ORDER of the switches is significant ...

just try
tar cZf instead of cfZ

And read "info tar" :}



Cheers,
Tink

chakkerz 07-09-2005 03:04 AM

#!/bin/bash
# simple backup
OF="/var/archive/backup-$(date +%Y%m%d).tgz"
IN="/home/some_file"
tar czf $OF $IN


The z has to be lowercase
you're missing a " in the OF line
that should do the trick

Edit

Actually it appears that the Z doesn't have to be lowercase - i stand corrected.

win32sux 07-09-2005 03:13 AM

Re: tar quirks?
 
Quote:

Originally posted by Joseph Schiller
#!/bin/bash
# simple backup
OF="/var/archive/backup-$(date +%Y%m%d).tgz
IN="/home/some_file"
tar -cfZ $OF $IN

This creates a 66 byte backup-20050709.tgz file which is empty. What happened to the file? It seems the 'Z' option to compress the file does not work, or I cannot extract the file. I foud that the only way I can make the script work is omit the 'Z' option and forget about pipelines.

try this:
Code:

#!/bin/bash
# simple backup
DATE=`date +%Y%m%d`
OF="/var/archive/backup-$DATE.tgz"
IN="/home/some_directory"
tar cvzf $OF $IN

Quote:

How can I pipeline an archive to gzip?
like this:
Code:

cat example.tar | gzip -c > example.tar.gz

Joseph Schiller 07-09-2005 03:25 AM

Hi,

Changing the command to 'tar -cZf $OF $IN' generates the comforting error message

tar: Cowardly refusing to create an empty archive
Try 'tar --help' for more information.

I believe tar -cZf is a valid command, but it doesn't archive an archive in an archive, I see an empty file of 65 bytes.

Joe

win32sux 07-09-2005 03:34 AM

Quote:

Originally posted by Joseph Schiller
tar: Cowardly refusing to create an empty archive
you are using a valid $IN, right??

BTW, just curious: why are you using "-Z" instead of "-z"??

Tinkster 07-09-2005 03:44 AM

Quote:

Originally posted by Joseph Schiller
Hi,

Changing the command to 'tar -cZf $OF $IN' generates the comforting error message

tar: Cowardly refusing to create an empty archive
Try 'tar --help' for more information.

I believe tar -cZf is a valid command, but it doesn't archive an archive in an archive, I see an empty file of 65 bytes.

Joe

I'd love to see $OF and $IN expanded ... it works well here.


Cheers,
Tink

Joseph Schiller 07-09-2005 09:28 PM

Hi,

I have an annotations file from OpenOffice's 'Write' called /home/joe/notes.sxw which I'd like to archive with each project's iteration and a time stamp for easier retrieval later. I don't need to pull out Big_Guns_Script for that purpose, but I don't want to remember all the mumbo-jumbo every time I need to run it. "tar -cZf /var/archive/backup-$(date +%Y%m%d).tgz /home/joe/notes.sxw" has all the look 'n feel for the purpose, too bad it doesn't also work. Changing to a lower case 'z' actually works, and I did not see what's in front of me nose, these are two different compression algorithms z=gzip Z=compress. Ergo .tgz won't takeoff with Z option. It pays to read the fine print, sometimes. If we are going to make the script actually work here's the solution "tar -cZf /var/archive/backup-$(date +%Y%m%d).Z /home/joe/notes.sxw"

Joe

Tinkster 07-09-2005 11:26 PM

Nice theory ... the thing is that tar doesn't give an
owls hoot about the name of the file, not to speak
of an extension.
Code:

tar cZf test$(date +%Y%m%d-%H%M%S).tgz xorg.conf.new
tar cZf test$(date +%Y%m%d-%H%M%S).Z xorg.conf.new

ls -ltr | tail -n 2
-rw-r--r--    1 root  root    1979 2005-07-10 16:25 test20050710-162532.tgz
-rw-r--r--    1 root  root    1979 2005-07-10 16:25 test20050710-162535.Z


Cheers,
Tink

Joseph Schiller 07-10-2005 02:57 PM

Hi,

Not a theory. See 'man tar' and scroll down to the last two options. Compress is using LZW compression algorithm the use of which is discouraged beacause the underlying compression algorithm is patented. To decompress .Z archives we can use either "uncompress" or "gunzip". But trying to create gunzipped files with compress did not work for me in the original script above.

Joe

Tinkster 07-10-2005 03:04 PM

Read again ... all I said is that the extension has
nothing to do with this.

Joseph Schiller 07-10-2005 06:35 PM

#!/bin/bash
# to be or not to be?

cp /var/log/boot.msg boot.msg

IF="boot.msg"
OF="boot_msg-$(date +%Y%m%d).tgz"
if [ -e boot.msg ]; then
tar -cZf $OF $IF # use 'compress'
wait
rm boot.msg
wait
tar -xvzf $OF # use 'gunzip'
fi
if [ -e boot.msg ]; then
echo "Success!"
else
echo "Sorry, file does not exist!"
fi
exit 0

You'll need to copy to a file and make the script runnable by chmod +x before you try it. On my machine running the script produces following output:

gzip: stdin: not in gzip format
tar: Child returned status 1
tar: Error exit delayed from previous errors
Sorry, file does not exist!

Although you can produce a boot_msg.tgz file with 'tar -cZf', the file is actually garbage and cannot be restored.

Joe

win32sux 07-10-2005 08:57 PM

Quote:

Originally posted by Joseph Schiller
#!/bin/bash
# to be or not to be?

cp /var/log/boot.msg boot.msg

IF="boot.msg"
OF="boot_msg-$(date +%Y%m%d).tgz"
if [ -e boot.msg ]; then
tar -cZf $OF $IF # use 'compress'
wait
rm boot.msg
wait
tar -xvzf $OF # use 'gunzip'
fi
if [ -e boot.msg ]; then
echo "Success!"
else
echo "Sorry, file does not exist!"
fi
exit 0

You'll need to copy to a file and make the script runnable by chmod +x before you try it. On my machine running the script produces following output:

gzip: stdin: not in gzip format
tar: Child returned status 1
tar: Error exit delayed from previous errors
Sorry, file does not exist!

Although you can produce a boot_msg.tgz file with 'tar -cZf', the file is actually garbage and cannot be restored.

Joe

executing that script on my slackware box i get the success message:
Code:

bash-3.00$ cat script.sh
#!/bin/bash
# to be or not to be?

cp /var/log/xdm.log boot.msg

IF="boot.msg"
OF="boot_msg-$(date +%Y%m%d).tgz"
if [ -e boot.msg ]; then
tar -cZf $OF $IF # use 'compress'
wait
rm boot.msg
wait
tar -xvzf $OF # use 'gunzip'
fi
if [ -e boot.msg ]; then
echo "Success!"
else
echo "Sorry, file does not exist!"
fi
exit 0
bash-3.00$ ./script.sh
boot.msg
Success!

* all i edited is the cp command at the start cuz i don't have /var/log/boot.msg


Tinkster 07-10-2005 10:14 PM

Quote:

Originally posted by Joseph Schiller
tar -cZf $OF $IF # use 'compress'
wait
rm boot.msg
wait
tar -xvzf $OF # use 'gunzip'
fi
if [ -e boot.msg ]; then
echo "Success!"
else
echo "Sorry, file does not exist!"
fi
exit 0
The fact aside that you're proving that gunzip can
uncompress compressed files, and not that names
don't matter, it works as designed.

Quote:

You'll need to copy to a file and make the script runnable by chmod +x before you try it. On my machine running the script produces following output:

gzip: stdin: not in gzip format
tar: Child returned status 1
tar: Error exit delayed from previous errors
Sorry, file does not exist!

Although you can produce a boot_msg.tgz file with 'tar -cZf', the file is actually garbage and cannot be restored.
Heh - you must be using a poorly slapped together
distro ... ;) ... like for win32sux the script works well
here.


Cheers,
Tink

Joseph Schiller 07-11-2005 02:10 AM

Hi,

I'm running SuSE 9.1 which comes with tar-1.13.25-298. It is sort of experimental, the odd number indicating it's fully equipped with bugs. If you don't mind me asking, which version of tar is on your box?
Maybe it's time to upgrade.

Joe


All times are GMT -5. The time now is 07:59 AM.