Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Mac OS X > Pipe TAR Output to file doesn't work

Pipe TAR Output to file doesn't work
Thread Tools
Fresh-Faced Recruit
Join Date: Aug 2001
Location: Germany
Status: Offline
Reply With Quote
Aug 30, 2003, 10:08 AM
 
Hi,

on a Linux box I use a cronjob with the following command (daily backup):

tar ... -f text.tar > test.log

Later I send the test.log file via eMail to my account to see if everything's allright.

Under OS X the script creates the tar archiv but the test.log file is empty.

What've I done wrong?

Mark
     
Senior User
Join Date: Jan 2001
Location: Mahwah, NJ USA
Status: Offline
Reply With Quote
Aug 30, 2003, 02:46 PM
 
Originally posted by Mark Goebl:
Hi,

on a Linux box I use a cronjob with the following command (daily backup):

tar ... -f text.tar > test.log

Later I send the test.log file via eMail to my account to see if everything's allright.

Under OS X the script creates the tar archiv but the test.log file is empty.

What've I done wrong?
You left out what "..." does in your script. Please elaborate.
-DU-...etc...
     
Mac Elite
Join Date: May 2001
Status: Offline
Reply With Quote
Aug 30, 2003, 05:20 PM
 
You won't get output if you don't specify -v for verbose.

-
     
Senior User
Join Date: Jan 2001
Location: Mahwah, NJ USA
Status: Offline
Reply With Quote
Aug 31, 2003, 06:25 AM
 
Oh OK Mark... I think I see what you are getting at. By default (at least in Red Hat Linux) cron is set to mail results to root. Take a look at your /etc/crontab file. There should be an entry like this:

MAILTO=root

You can set that to whoever you want it to go to. By default (again on Red Hat Linux) sendmail is set to ONLY send mail to the localhost. If you want it to be sent elsewhere then you will have to adjust sendmail (easy to do).

In any case...
What I usually do is set my backup jobs to run in /etc/cron.daily/ All scripts in that folder, if they have any output, the output is piped to an email and sent to root. So, in essence, your problem is already mostly solved. All you need your script to do is "echo" useful info as it progresses with its task. This can be done in several ways.

One way is to sprinkle meaningful messages throughout your script like:

Code:
. . echo Starting backup of whatever to wherever echo Doing sanity checks if [ -f /data/tobe/backedup/markerfile -a -f /backup/target/markerfile]; then { echo Sanity check PASSED... proceeding somebackup_command(s) echo Backup complete } else { echo Sanity checks FAILED } fi . . .
In the above code snippet all the echo statements that are executed will be passed to an email. The "if" statement checks to see that the data to be backed up exists and the volume it is on is mounted by checking for a unique marker file AND it checks that the volume or device that it is being backed up to exists and is mounted by cecking for another marker file. If either test returns false... then the backup should NOT proceed and will say so. If both marker files exist then the backup proceeds. The "somebackup_command(s)" statement is whatever command(s) you are using for the backup. If these commands normally echo something to the console when run manually then that data will also be entered in to the email. Many commands use the "-v" switch (including tar) for "verbose execution". Some will even take "-vv" or "-vvv" switches for different levels of verbosity.

By default /etc/cron.daily/ scripts are run at 4:02 AM. The scripts in that folder are run in ASCIIbetical order. After they have all run root will get an email with the subject:

Subject: Cron <root@localhost> run-parts /etc/cron.daily

and in body of the email will have a heading for each script that was run and the output from that script.

For me this is pretty handy for accountability. I can easily check which files were backed up, when, and where they were backed up to.

There is one caveat to the above outlined method... If you are backing up a large number of files and/or you set your backup command to be extrmeley verbose then the email could be HUGE and may cause some mail clients and/or MTAs to barf. To avoid this problem I set the output to be sent to a file instead, like this:

Code:
. . . # Date Of Manufacture DOM=`date +%d` LOG=/var/log/backup/backuplog.$DOM rm -f $LOG /my/backup/command ... &> $LOG . . .
Where DOM gives the backup log file a small numerical timestamp, LOG is full path to the file it logs to, rm -f $LOG removes any LOG file with the same timestamp, and &> will pass BOTH stderr and sdtout to the log file directly. It will create logfiles named backuplog.01, backuplog.02, backuplog.03... and so on until the there are as many as there are days in the month and once it reaches the highest number of days in any given month it will start at .01 again. It will keep a months worth of the backuplog files in /var/log/backup.

Was all that helpful?
-DU-...etc...
     
Fresh-Faced Recruit
Join Date: Aug 2001
Location: Germany
Status: Offline
Reply With Quote
Aug 31, 2003, 07:49 AM
 
I use the following tar commandline:

tar -cvvz -f Backup/daily.tar Documents >Backup/daily.log

I know i must use the -v option but my problem is that after the line above the daily.log file exists but is just empty!

Any Ideas?

Mark
     
Addicted to MacNN
Join Date: Jun 1999
Location: Las Vegas, NV, USA
Status: Offline
Reply With Quote
Aug 31, 2003, 09:21 AM
 
None of your arguments are full pathnames. Try full pathnames and see if that helps.

tar -cvz -f /path-to-Backup/Backup/daily.tar /path-to-Documents/Documents > /path-to-Backup/Backup/daily.log
     
Professional Poster
Join Date: Oct 1999
Location: :ИOITAↃO⅃
Status: Offline
Reply With Quote
Aug 31, 2003, 11:16 AM
 
Originally posted by Mark Goebl:
I use the following tar commandline:

tar -cvvz -f Backup/daily.tar Documents >Backup/daily.log

I know i must use the -v option but my problem is that after the line above the daily.log file exists but is just empty!

Any Ideas?

Mark
Well, does it produce any output to stdout if you don't ridirect to the logfile?

i.e. just type
tar -cvvz -f Backup/daily.tar Documents
     
Fresh-Faced Recruit
Join Date: Aug 2001
Location: Germany
Status: Offline
Reply With Quote
Aug 31, 2003, 01:47 PM
 
if I do not redirect the output I get the typical output of tar (a list of files which are put into the archiv).

I know it should work but it just did not.

On a linux-Server the commandline works, on os x it just doesn't writes anything in the file!!!
     
Addicted to MacNN
Join Date: Jun 1999
Location: Las Vegas, NV, USA
Status: Offline
Reply With Quote
Aug 31, 2003, 02:55 PM
 
Now put a space between ">" and "Backup" and try once more...

> Backup/daily.log

Chris
     
Senior User
Join Date: Jan 2000
Status: Offline
Reply With Quote
Aug 31, 2003, 03:51 PM
 
tar's verbose option prints to stderr. You're only redirecting stdout, which is why the log file is empty. It should be:


tar cvvf tarfile.tar tarsource >& /path/to/logfile


This only works if you're using a csh based shell. If you're using sh-derived shell, it would be:


tar cvvs tarfile.tar tarsource 2> /path/to/logfile
     
Senior User
Join Date: Jan 2001
Location: Mahwah, NJ USA
Status: Offline
Reply With Quote
Aug 31, 2003, 06:14 PM
 
Originally posted by Mark Goebl:
if I do not redirect the output I get the typical output of tar (a list of files which are put into the archiv).

I know it should work but it just did not.

On a linux-Server the commandline works, on os x it just doesn't writes anything in the file!!!
Well I tested it here and THIS works:

I create a directory called Backup.

mkdir /home/utidjian/Backup

I already have a /home/utidjian/Documents folder for Word documents.

As root I create a file in /etc/cron.daily called backdoc.

vi /etc/cron.daily/backdoc

In this file are the following lines:

Code:
#!/bin/sh # This will create the files daily.tar.gz and # daily.log tar -cvvz -f /home/utidjian/Backup/daily.tar.gz /home/utidjian/Documents > /home/utidjian/Backup/daily.log
chmod +x /etc/cron.daily/backdoc

The above successfully creates both files as directed and the contents of daily.log is the output from the tar command. The contents of daily.tar.gz is the compressed contents of the folder /home/utidjian/Documents. It also emails root upon completion but no listing of files backed up.

Alternatively (this also works):

I create the files and folders as above but /etc/cron.daily/backdoc now looks like this:

Code:
#!/bin/sh #does backup and writes to backup log AND sends email #to root with a complete listing. tar -cvvz -f /home/utidjian/Backup/daily.tar.gz /home/utidjian/Documents 2>&1 |tee /home/utidjian/Backup/daily.log
As you can see I copy-n-pasted almost your exact command with adjustments made for my setup. All you need to do is change the above script for your setup and it should work. If it doesn't then there is something wrong with your setup. What flavor of Linux are you using? Do you have root access? What are you using to edit the the cronjob file? What does your /etc/crontab file look like? Is crond running? Have you been checking /var/log/cron and roots email for any error messages?
-DU-...etc...
     
Addicted to MacNN
Join Date: Jun 1999
Location: Las Vegas, NV, USA
Status: Offline
Reply With Quote
Aug 31, 2003, 07:19 PM
 
Why do you use the -v switch twice? Isn't "-cvz" the same as "-cvvz"?

Chris
     
Senior User
Join Date: Jan 2001
Location: Mahwah, NJ USA
Status: Offline
Reply With Quote
Sep 1, 2003, 07:31 AM
 
Originally posted by chabig:
Why do you use the -v switch twice? Isn't "-cvz" the same as "-cvvz"?
Dunno. It doesn't hurt in this case. One can do -v, -vv, -vvv in some commands and it means verbose, very verbose, and extremely verbose respectively. In tar it doesn't add anything. In other commands it helps for different levels of debugging. fetchmail is an example that uses the -vv switch. I was just trying to use his Mark's original command as closely as possible. Only things I really changed were the .gz on the tarballs filename and full path from the root of the filesystem tree. It is best to append a .gz to a .tar file that is compressed with gzip (the -z switch). I could as easily have done:

tar -czvf ...

instead of his original command. It would have made no difference. I think the - in -czvf is also deprecated now also. One can be fairly sloppy about some things in commands and not about others. Hard to tell sometimes what will bite you and what won't. Best thing is to test it first... especially backup scripts and, of course, RTFM ;-)
-DU-...etc...
     
   
Thread Tools
Forum Links
Forum 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
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 10:16 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2