Wednesday, 21 January 2009

Cron PHP mysqldump ubuntu sendmail

The problem is we have a Joomla install on one of our servers, we want to back up the databases each night and mail us to confirm. Problem being that Ubuntu doesn't have sendmail installed by default and when you try and call the php mail() function it breaks saying it can't find send mail. A quick "sudo apt-get postfix" installed postfix mail, a much more competent mail system.

Next up is starting the cron tasks, I used a little guide and soon had cron running a mail script using a command line "sudo crontab -e" and editing in nano, not a big fan of vi

32 * * * * php /home/administrator/Scripts/backup-intranet.php

this emails me every 32 minutes, mails work on my gmail account, but my work account must be eating them with the spam filter.

So all good so far, the next stage is to set up the daily mysqldump to backup joomla. Adding another line into cron:

04 18 * * * mysqldump -uroot --password='******' --all-databases | gzip > /home/administrator/Scripts/joomla-backup/joomlasqlbackup_`date +%m_%d_%y`.gz

backing up at 6:04 each evening, it executes when I'm root from command line and from normal, but when I'm in cron it doesn't fire. To get around this I placed a bash script to run this and pointed cron to that:


#!/bin/bash

date=`date +%Y-%m-%d_%Hh%M`

mysqldump -uroot --password='******' --all-databases | gzip > /home/administrator/Scripts/joomla-backup/$date.gz

Make sure the folder has permissions for scripts ot write to it! And the new cron is:

/home/administrator/Scripts/databasebackup.sh

No comments:

Post a Comment