[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Email output + logging too?
From: |
Sharon Kimble |
Subject: |
Re: [Help-bash] Email output + logging too? |
Date: |
Wed, 18 Sep 2013 15:55:21 +0100 |
On Sun, 15 Sep 2013 10:39:41 +1000
Matthew Cengia <address@hidden> wrote:
> On 2013-09-14 09:15, Sharon Kimble wrote:
> > I have this script which uses obnam to back up my /home/$USER
> > directory, and it outputs to 2 logs, and should also output to an
> > email.
>
> Use 'tee' for this:
>
> du -sh /media/boudiccas/backup/obnam-home 2>&1 | tee -a "$LOGGING2"
>
> echo 'Sending Backup report : Backup of obnam-home completed',
> $(date \ -R) 'logged to' 2>&1 | tee -a "$LOGFILE"
>
> To send the output of these commands to both their respective files,
> and to stdout, which will get emailed out by cron.
Thanks for this, I've just been taking time to fine-tune it, and I'll
put the complete script at the end for further comments.
>
> Also, don't use capitalised variable names:
>
> By convention, environment variables (PATH, EDITOR, SHELL,
> ...) and internal shell variables (BASH_VERSION, RANDOM, ...) are
> fully capitalized. All other variable names should be lowercase. Since
> variable names are case-sensitive, this convention avoids
> accidentally overriding environmental and internal variables.
>
> And finally, use more quotes, specifically around $LOGGING2,
> $LOGFILE, and $(date ...):
>
> "Double quote" _every_ expansion, and anything that could contain a
> special character, eg. "$var", "$@", "address@hidden", "$(command)".
> Use 'single quotes' to make something literal, eg. 'Costs $5 USD'. See
> <http://mywiki.wooledge.org/Quotes>,
> <http://mywiki.wooledge.org/Arguments> and
> <http://wiki.bash-hackers.org/syntax/words>.
>
> Good luck!
>
This is my 'notify-send' script
=======================================================
#!/bin/bash -e
####################################################
# Variables
LOGFILE="/home/boudiccas/logs/obnam.txt"
LOGGING2="/home/boudiccas/logs/du.txt"
####################################################
# Log all stdout+stderr
exec > >(tee -a /home/boudiccas/logs/obnam.txt) 2>&1
notify-send "Starting main backup..."
obnam backup
if [ "$?" -ne 0 ]; then
notify-send "Unable to finish main backup."
exit 1
else
notify-send "Finished main backup."
du -sh /media/boudiccas/backup/obnam-home
echo 'Sending Backup report : Backup of obnam-home completed', $(date /
-R) 'logged to' $LOGFILE
echo '####################################'
fi
===============================================
which gives this as its output -
===============================================
Subject: Cron <address@hidden>
DISPLAY=:0 /home/boudiccas/bin/notify-send #backup /home
Date:Wed, 18 Sep 2013 14:12:20 +0100
Backed up 12985 files (of 159530 found), uploaded 182.7 MiB in 10m44s /
at 290.6 KiB/s average speed
517G /media/boudiccas/backup/obnam-home
===============================================
For some reason its not processing the two "echo" lines, and I cant see
why. I've tried various things to get them to work but without success.
Any ideas why please?
Thanks
Sharon.
--
A taste of linux = http://www.sharons.org.uk
efever = http://www.efever.blogspot.com/
efever = http://sharon04.livejournal.com/
Debian testing, Fluxbox 1.3.5, LibreOffice 4.1.1.2
Registered Linux user 334501
signature.asc
Description: PGP signature