LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-09-2005, 03:58 AM   #1
Joseph Schiller
Member
 
Registered: Oct 2003
Location: New Jersey
Posts: 44

Rep: Reputation: 15
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
 
Old 07-09-2005, 04:04 AM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 913Reputation: 913Reputation: 913Reputation: 913Reputation: 913Reputation: 913Reputation: 913Reputation: 913
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

Last edited by Tinkster; 07-09-2005 at 04:07 AM.
 
Old 07-09-2005, 04:04 AM   #3
chakkerz
Member
 
Registered: Dec 2002
Location: Brisbane, Australia
Distribution: RedHat (RHEL, FC, CentOS), openSuSE, Mac OS X
Posts: 653

Rep: Reputation: 32
#!/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.

Last edited by chakkerz; 07-09-2005 at 04:06 AM.
 
Old 07-09-2005, 04:13 AM   #4
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 379Reputation: 379Reputation: 379Reputation: 379
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
 
Old 07-09-2005, 04:25 AM   #5
Joseph Schiller
Member
 
Registered: Oct 2003
Location: New Jersey
Posts: 44

Original Poster
Rep: Reputation: 15
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
 
Old 07-09-2005, 04:34 AM   #6
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 379Reputation: 379Reputation: 379Reputation: 379
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"??
 
Old 07-09-2005, 04:44 AM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 913Reputation: 913Reputation: 913Reputation: 913Reputation: 913Reputation: 913Reputation: 913Reputation: 913
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
 
Old 07-09-2005, 10:28 PM   #8
Joseph Schiller
Member
 
Registered: Oct 2003
Location: New Jersey
Posts: 44

Original Poster
Rep: Reputation: 15
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
 
Old 07-10-2005, 12:26 AM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 913Reputation: 913Reputation: 913Reputation: 913Reputation: 913Reputation: 913Reputation: 913Reputation: 913
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
 
Old 07-10-2005, 03:57 PM   #10
Joseph Schiller
Member
 
Registered: Oct 2003
Location: New Jersey
Posts: 44

Original Poster
Rep: Reputation: 15
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
 
Old 07-10-2005, 04:04 PM   #11
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 913Reputation: 913Reputation: 913Reputation: 913Reputation: 913Reputation: 913Reputation: 913Reputation: 913
Read again ... all I said is that the extension has
nothing to do with this.
 
Old 07-10-2005, 07:35 PM   #12
Joseph Schiller
Member
 
Registered: Oct 2003
Location: New Jersey
Posts: 44

Original Poster
Rep: Reputation: 15
#!/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
 
Old 07-10-2005, 09:57 PM   #13
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 379Reputation: 379Reputation: 379Reputation: 379
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


Last edited by win32sux; 07-10-2005 at 10:00 PM.
 
Old 07-10-2005, 11:14 PM   #14
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 913Reputation: 913Reputation: 913Reputation: 913Reputation: 913Reputation: 913Reputation: 913Reputation: 913
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
 
Old 07-11-2005, 03:10 AM   #15
Joseph Schiller
Member
 
Registered: Oct 2003
Location: New Jersey
Posts: 44

Original Poster
Rep: Reputation: 15
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
 
  


Reply


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 On
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 03:48 AM
Mandrake 10.1 pp quirks Mc.Firestorm Linux - Software 1 03-04-2005 04:27 AM
a few little quirks..... ocularbob Slackware 5 06-10-2003 06:18 AM
Slack 9 quirks dukie Linux - Newbie 7 03-23-2003 05:28 PM
Mplayer quirks... khermans Linux - Software 0 10-10-2002 12:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:27 AM.

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