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 |
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 |
#!/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. |
Re: tar quirks?
Quote:
Code:
#!/bin/bash Quote:
Code:
cat example.tar | gzip -c > example.tar.gz |
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 |
Quote:
BTW, just curious: why are you using "-Z" instead of "-z"?? |
Quote:
Cheers, Tink |
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 |
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 Cheers, Tink |
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 |
Read again ... all I said is that the extension has
nothing to do with this. |
#!/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 |
Quote:
Code:
bash-3.00$ cat script.sh |
Quote:
uncompress compressed files, and not that names don't matter, it works as designed. Quote:
distro ... ;) ... like for win32sux the script works well here. Cheers, Tink |
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. |