BashScripts.org
http://bashscripts.org/forum/

script doesn't log info
http://bashscripts.org/forum/viewtopic.php?f=7&t=985
Page 1 of 1

Author:  jimm [ Thu Feb 04, 2010 8:16 am ]
Post subject:  script doesn't log info

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

Author:  thobbs [ Fri Feb 05, 2010 1:08 am ]
Post subject:  Re: script doesn't log info

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

Author:  jimm [ Fri Feb 05, 2010 8:28 am ]
Post subject:  Re: script doesn't log info

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

Author:  Watael [ Fri Feb 05, 2010 9:01 am ]
Post subject:  Re: script doesn't log info

what's the date format of ls -la output?

Author:  jimm [ Fri Feb 05, 2010 2:21 pm ]
Post subject:  Re: script doesn't log info

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.

Author:  Watael [ Fri Feb 05, 2010 3:21 pm ]
Post subject:  Re: script doesn't log info

what's your OS?

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

Author:  jimm [ Fri Feb 05, 2010 3:26 pm ]
Post subject:  Re: script doesn't log info

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

Author:  Watael [ Fri Feb 05, 2010 3:40 pm ]
Post subject:  Re: script doesn't log info

don't you have an alias for ls, which would use --full-time option?

in console enter alias -p

Author:  jimm [ Fri Feb 05, 2010 3:59 pm ]
Post subject:  Re: script doesn't log info

--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.

Author:  Watael [ Sat Feb 06, 2010 3:16 pm ]
Post subject:  Re: script doesn't log info

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

Author:  jimm [ Sun Feb 07, 2010 8:06 am ]
Post subject:  Re: script doesn't log info

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

Page 1 of 1 All times are UTC - 6 hours
© 2000, 2002, 2005, 2007 phpBB Group • http://www.phpbb.com