Here's my updated script. It's working now.<br><br>#!/bin/bash<br>set -e<br>NOW=$(date +%d-%m-%Y-%H:%M)<br>FILENAME="moodle_db_backup_$NOW.sql"<br>/usr/bin/mysqldump -uuser -ppassword moodle>/dbbck/$FILENAME<br>
<br>I tried adding the error checking:<br><br>#!/bin/bash<br>set -e<br>NOW=$(date +%d-%m-%Y-%H:%M)<br>FILENAME="moodle_db_backup_$NOW.sql"<br>/usr/bin/mysqldump -uuser -ppassword moodle>/dbbck/$FILENAME<br>EXITSTATUS=$?<br>
if["$EXITSTATUS"!="0"] then<br>/bin/mail -s "Moodle db backup failed" root;<br>fi<br><br>But I get line 9: syntax error near unexpected token 'fi'<br><br>I guess it's not really that important as long as I check a new file appears each day. <img goomoji="330" style="margin: 0pt 0.2ex; vertical-align: middle;" src="cid:330@goomoji.gmail"><br>
<br>Cheers,<br><br>Nathan.<br><br><div class="gmail_quote">On Fri, Apr 10, 2009 at 9:19 PM, Nathan Friend <span dir="ltr"><<a href="mailto:nathan.friend@gmail.com" target="_blank">nathan.friend@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Thanks everyone, lot's of good script info.<br><br>I'll add some error checking and post my findings when I'm back at work Tuesday.<br>
<br>Colin - Yes I can do a talk on Moodle if it's of interest...<br><font color="#888888"><br>
Nathan.<br><br></font><div class="gmail_quote"><div><div></div><div>On Thu, Apr 9, 2009 at 11:18 PM, Alan @ COMM-TECH <span dir="ltr"><<a href="mailto:alan@communitytechnology.org.uk" target="_blank">alan@communitytechnology.org.uk</a>></span> wrote:<br>
</div></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div><div></div><div>
<div bgcolor="#ffffff" text="#000000"><div><div></div><div>
Jeremy Hooks wrote:
<blockquote type="cite">
<pre>2009/4/9 Nathan Friend <a href="mailto:nathan.friend@gmail.com" target="_blank"><nathan.friend@gmail.com></a>:
</pre>
<blockquote type="cite">
<pre>Afternoon All,
I've writen a simple script to dump a database and would like it to run as a
cron job. How should I set the script permisisons?
</pre>
</blockquote>
<pre>Presuming the cron job is being run by root you will probably want to
make sure the script is owned by root:
chown root.root /usr/local/script/dbbackup.sh
Then you can set the permissions with so that only root has
permissions to read/write/execute it:
chmod 700 /usr/local/script/dbbackup.sh
or
chmod u=rwx /usr/local/script/dbbackup.sh
</pre>
<blockquote type="cite">
<pre>dbbackup.sh contains:
NOW=$(date +"%d-%b-%y"_"%l:%M")
FILENAME="moodle_db_backup_$NOW.sql"
mysqldump -uuser -ppassword databasename > /dbbck/$FILENAME
</pre>
</blockquote>
<pre>Don't forget, ofcourse if you want the script to actually run you need
a shebang too, e.g.:
#!/usr/bin/bash
The date format you are using will likely cause you problems as it
will give you something like "09-Apr-09_ 8:30", a filename with a
space in it. With either a %I (12 hour, with leading zero) or %H (24
hour, with leading zero) rather than %l (12 hour, leading space).
Something else that is important to remember when writing cron scripts
is that you can't always be certain that the path variable isn't
necessarily the same as you would have when running running the script
from an interactive shell. So I would recommend changing 'mysqldump'
to '/fullpath/mysqldump'.
</pre>
<blockquote type="cite">
<pre>The cron entry is
30 20 * * * /usr/local/script/dbbackup.sh
I can run the script from for command line OK with the expected output.
However wathcing /var/log/messages I see the cron job run but no output
file...
</pre>
</blockquote>
<pre>One final thing, if your script running but you aren't getting the
results you expect, it may be worth checking the output of the
command. Normally the output from cron scripts is emailed to the user
using the local mail system. You can check this by using (say) mutt
or 'vi /var/spool/mail/root'.
I think that is everything, but I don't use cron often, so I could
have forgotten something.
Regards.
Jeremy
</pre>
</blockquote></div></div>
Cron is so reliable you tend to just set it and forget it, until you
find the backup hasn't been working for 6 months because the
destination was unreachable, or the mysql password changed or someting..<br>
<br>
I find it useful therefore to interrogate the exit code of mysqldump
so you could also have the script mail you if someting went wrong, so
right after the mysqldump command use:<br>
<br>
EXITSTATUS=$?<br>
if [ "$EXITSTATUS" != "0" ] then<br>
== send email notifying a problem with the backup==<br>
fi<br>
<br>
[googling..] here's a nice little revelation on bash exit code
[arrays!]:<br>
<br>
<a href="http://www.briandowney.net/blog/2008/05/05/bash-pipeline-exit-codes/" target="_blank">http://www.briandowney.net/blog/2008/05/05/bash-pipeline-exit-codes/</a><br><font color="#888888">
<br>
Alan
</font></div>
<br></div></div><div>_______________________________________________<br>
Kent mailing list<br>
<a href="mailto:Kent@mailman.lug.org.uk" target="_blank">Kent@mailman.lug.org.uk</a><br>
<a href="https://mailman.lug.org.uk/mailman/listinfo/kent" target="_blank">https://mailman.lug.org.uk/mailman/listinfo/kent</a><br></div></blockquote></div><br>
</blockquote></div><br>