<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Jeremy Hooks wrote:
<blockquote
 cite="mid:bf2fbe6d0904090759s7ceded3euf48fbd8ec2a23937@mail.gmail.com"
 type="cite">
  <pre wrap="">2009/4/9 Nathan Friend <a class="moz-txt-link-rfc2396E" href="mailto:nathan.friend@gmail.com">&lt;nathan.friend@gmail.com&gt;</a>:
  </pre>
  <blockquote type="cite">
    <pre wrap="">Afternoon All,
I've writen a simple script to dump a database and would like it to run as a
cron job.&nbsp; How should I set the script permisisons?
    </pre>
  </blockquote>
  <pre wrap=""><!---->
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 wrap="">dbbackup.sh contains:

NOW=$(date +"%d-%b-%y"_"%l:%M")
FILENAME="moodle_db_backup_$NOW.sql"
mysqldump -uuser -ppassword databasename &gt; /dbbck/$FILENAME
    </pre>
  </blockquote>
  <pre wrap=""><!---->
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 wrap="">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 wrap=""><!---->
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>
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&nbsp; 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>
&nbsp;&nbsp; == send email notifying a problem with the backup==<br>
fi<br>
<br>
[googling..]&nbsp; here's a nice little revelation on bash exit code
[arrays!]:<br>
<br>
<a class="moz-txt-link-freetext" href="http://www.briandowney.net/blog/2008/05/05/bash-pipeline-exit-codes/">http://www.briandowney.net/blog/2008/05/05/bash-pipeline-exit-codes/</a><br>
<br>
Alan
</body>
</html>