Register
It is currently Sun Apr 20, 2014 1:13 pm

script doesn't log info


All times are UTC - 6 hours


Post new topic Reply to topic  [ 11 posts ] 
Author Message
 PostPosted: Thu Feb 04, 2010 8:16 am   

Joined: Thu Feb 04, 2010 8:06 am
Posts: 11
HI All,
My backup script works well except for logging the info. I have run "script" to debug the operation but when I do this everything runs perfectly. The problem must be some dumb thing I am not seeing. I hope some other eyes will help. BTW the script runs as a cron job. It does the backups (checked), it creates the date variable (checked) it creates and properly dates the info file (checked) but then doesn't put any info into the file. If the same script is run from the command line, everything including file info is done as expected. I'm at a loss, so any help will be greatly appreciated.
Code:
#! /bin/bash

# backup changed or new files
rsync -rtpogvc /home/jimm/Documents/ /media/removeable/backup/

# set yesterdays date
date=$(date --date='1 day ago' +%Y-%m-%d)

# check what was backed up and put the info in a file
ls -la /media/removeable/backup/ | grep $date > /home/jimm/BackupLogs/backup.$date


Thanks,
Jimm


Top
 Profile  
 PostPosted: Fri Feb 05, 2010 1:08 am   
User avatar

Joined: Sat Jun 13, 2009 8:53 pm
Posts: 73
Location: Texas!
Hmm... I'm not sure. Try redirecting stderr to the file as well so you can see any errors that occur:

Code:
ls -la /media/removeable/backup/ | grep $date &> /home/jimm/BackupLogs/backup.$date


Top
 Profile  
 PostPosted: Fri Feb 05, 2010 8:28 am   

Joined: Thu Feb 04, 2010 8:06 am
Posts: 11
Thanks for the thought thobbs, but no joy. I set stderr to go to the backuplog file but the script still wrote nothing. Since it does write when I run it from the cli I was thinking that maybe it is a permission issue but I create the input file as 777 and still nothing writes. I also turned off selinux just in case it wasn't announcing a permission error but still nothing. This is getting frustrating. I appreciate your help. Thanks.
Jimm


Top
 Profile  
 PostPosted: Fri Feb 05, 2010 9:01 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 512
what's the date format of ls -la output?


Top
 Profile  
 PostPosted: Fri Feb 05, 2010 2:21 pm   

Joined: Thu Feb 04, 2010 8:06 am
Posts: 11
The date format is the same as the Y-m-d in the variable. That is why the script does create the log file properly when run from the cli. What is frustrating me is that the same script, run from cron, won't write to the file. Thanks.


Top
 Profile  
 PostPosted: Fri Feb 05, 2010 3:21 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 512
what's your OS?

GNU/Linux doesn't seem to have such an output for ls -la :(


Top
 Profile  
 PostPosted: Fri Feb 05, 2010 3:26 pm   

Joined: Thu Feb 04, 2010 8:06 am
Posts: 11
OS is fedora 11 and a sample ls -la output is:
-rw-rw-r--. 1 jimm jimm 18 2010-02-04 00:00 testdate.txt
From the cli grep recognizes 2010-02-04 and filters based on that.
Thanks


Top
 Profile  
 PostPosted: Fri Feb 05, 2010 3:40 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 512
don't you have an alias for ls, which would use --full-time option?

in console enter alias -p


Top
 Profile  
 PostPosted: Fri Feb 05, 2010 3:59 pm   

Joined: Thu Feb 04, 2010 8:06 am
Posts: 11
--full-time wouldn't add anything to this script as the date part of the output is the same format and the rest of full-time is superfluous for my filter. Good ideas though, thanks.


Top
 Profile  
 PostPosted: Sat Feb 06, 2010 3:16 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 512
I think I got it: that's locales (by default cron's one is C, not user's).
try this:
Code:
LC_TIME=en_US ls -la /path/yourDirectory | grep "$(date +'%Y%-%m-%d')" > /path/logfile


Top
 Profile  
 PostPosted: Sun Feb 07, 2010 8:06 am   

Joined: Thu Feb 04, 2010 8:06 am
Posts: 11
Thanks a million Watael you hit the nail on the head and the script is now recording my backups very well. Not only that but your prior post got me thinking along the same lines, so I tried a different approach last night and it worked as well. Now we have 2 ways to control the time issue. I'll post my code below in case it helps anyone else.
Code:
ls -la --time-style='+%Y-%m-%d' /path/to/files/ | grep $date  >> /path/to/BackupLogs/backup.$date

Thanks again,
Jimm


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 13 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP