Executing CakePHP script using Windows Scheduled Task

In Windows adding a scheduled task is just not as straight forward as adding a CRON job using Linux. (Don’t get me started on troubleshooting a Windows Schedule Task that did not run for some reason.) However, it is not so difficult once you get it figured out. Here is what I did:

In this case I wanted to run a CakePHP script as a CRON job, or more accurately, as a Windows Scheduled Task since this customer insisted I create the application and use a Windows server. (I used XAMPP, so it wasn’t too bad.)

In order to run the script and take full advantage of the models in CakePHP it required that I use the CakePHP shell. Luckily the CakePHP developers created a ‘cake.bat’ script that enables this to happen on a Windows machine.  Normally on a Windows or Linux server you can navigate, via command line, to the ‘app’ folder and execute the ‘cake  name_of_script’ command, but using Windows Scheduled Tasks you need to execute the bat file.

Windows Scheduled Task Settings:
Run: C:\path\to\cake.bat  script_name {without the extension .php}
Start in: C:\path\to\app\folder
Run As: type in the appropriate users

Then of course you will need to go to the Schedule tab and set in the schedule you desire for your script.

Here is a screenshot: (you can see the default folders for xampp were used)

IMPORTANT: This entire process assumes that you have already created your script and placed it in the appropriate directory “/app/vendors/shells/{name_of_script.php}”.  It also assumes you understand how to create a cronjob for CakePHP to use.  (see below for a sample)

Sample content of ‘script_name.php’:

class ScriptNameShell extends Shell {
 
	var $uses = array('model1','model2');
 
	/**
	 * the main function is kicked off like a contructor
	 *
	 */
	function main() {
		echo 'Doing something.';
 
		$callingSomething = $this->otherFunction();
 
		echo $callingSomething;
	}
 
	function otherFunction() {
		$content = 'This is content from otherFunction.';
 
		return $content;
	}
}

Backup files from Linux to a Windows server

Ok, this may be my last disaster recovery and backup blog for a long time. As you can probably tell from the title this blog entry is all about keeping backup strategies as cheap as possible.

My strategy is to backup all of my Windows and Linux servers to one central Windows server that is running a Tivoli backup agent. All of my servers are hosted elsewhere, and since it costs $99.00 per server to backup I am getting the most for my money by only backing a single server to tape/SAN. However that single server carries all of the files that need to be remotely backed up to tape/SAN.

My earlier posts show how to backup the Windows servers:
Windows backup bat script using xcopy

Also, how to backup the Windows Domain Controller:
Backup Windows Domain Controller using NTBACKUP via cmd

And I also showed how to backup a Linux server to a local file:
Linux backup using CRON to local directory

Now I will show how I moved the files backed up on the Linux servers to the Windows server prior to tape/SAN backup. I have decided to use Samba and mount a directory pointing to a shared folder on the Windows server. Lets begin:
Continue reading Backup files from Linux to a Windows server

Linux backup using CRON to local directory

As many have pointed out I am on a backup and disaster recovery kick lately. Some would say that it is about time, others are simply glad to see that data is now being backed up. I have found that it is easiest to zip up files on a local machine prior to moving them to a final destination. So lets get started:

I have multiple Linux servers with many websites on each, as well as database. So I created a script that simply tar’s the files, then gzips them with the date in the filename for archiving.

Here is the file named ‘backupall.sh’ that I save in a place reachable by the user I will use to schedule this cronjob:

#!/bin/sh
date
echo "############### Backing up files on the system... ###############"
 
backupfilename=server_file_backup_`date '+%Y-%m-%d'`
 
echo "----- First do the sql by deleting the old file and dumping the current data -----"
rm -f /tmp/backup.sql
mysqldump --user=mysqluser --password=password --all-databases --add-drop-table > /tmp/backup.sql
 
echo "----- Now tar, then zip up all files to be saved -----"
tar cvf /directory/to/store/file/${backupfilename}.tar /home/* /var/www/html/* /usr/local/svn/* /etc/php.ini /etc/httpd/conf/httpd.conf /tmp/backup.sql /var/trac/*
gzip /directory/to/store/file/${backupfilename}.tar
rm /directory/to/store/file/${backupfilename}.tar
chmod 666 /directory/to/store/file/${backupfilename}.tar.gz
 
echo "############### Completed backing up system... ###############"
date

Continue reading Linux backup using CRON to local directory