Page 1 of 1

odd problem with crontab and system()

Posted: Fri Apr 09, 2010 2:22 pm
by paulstanely45
Hello,

I am having the darndest problem trying to get a script of mine to work. The odd part is that it did work for a week without any problems. Essentially what the script does is make a mysql database backup, and the stores it on an external drive.

My PHP configuration is from entropy (PHP 5.3.0). It is running on Mac OS SERVER 10.5.8

Here is the code I am using (some snippets will be changed a bit for security reasons)

/* commons.php */

Code: Select all

<?php
	set_time_limit(60 * 60 * 24);
	
	/* CONSTANTS */
	define("MYSQL_DUMP_ROOT", '/Volumes/backup/sql-backup/');
	define("SERVER_DUMP_ROOT", '/Volumes/backup/web-backup/');
	
	/* VARIABLES */
	$now = time();
	$date_format = date("Y-M-d", $now);
	
	$external_volume_mount_fail = false;

	$mount_output = array();
	
	$mount_path = "/Volumes/backup/";
	
	$mysql_current_date_directory_fail = false;
	$mysql_current_date_directory = MYSQL_DUMP_ROOT.$date_format.'/';
	
	$server_current_date_directory_fail = false;
	$server_current_date_directory = SERVER_DUMP_ROOT.$date_format.'/';
	
	$mysql_master_dump_file = $date_format.'-MASTER_DUMP.gz';
	$mysql_master_dump_command;
	
	$dump_command;
	$dump_file;
	
	$recipients = array("james@domain.com");
	
	function send_batch_email($title, $message) {
		global $recipients;
		
		for($i = 0; $i < count($recipients); $i++) {
			mail($recipients[$i], $title, $message, "From: James Robb <james@domain.com>");
		}
		
	}
?>
/* external_volume.php */

Code: Select all

<?php

	function mount_volume() {
		global $mount_path, $external_volume_mount_fail, $mount_output;
		
		// -- CREATE MOUNT POINT
		echo "creating mount point...\n";
		system("mkdir /Volumes/backup");
		system("chmod 777 /Volumes/backup/");
		
		// -- MOUNT EXTERNAL VOLUME
		echo "mounting external volume...\n\n";
		system("mount_afp -f afp://user:pass@somedrive.com/backup $mount_path");
		
		// -- CHECK FOR MOUNTED VOLUME
		if(@is_dir($mount_path.'sql-backup/')) {
			
		} else {
			$external_volume_mount_fail = true;
			
			echo "could not mount external volume...\n\n";
			
			echo $mount_path.'sql-backup/'."\n";
		}
	}
	
	function unmount_volume() {
		global $mount_path;
		
		echo "unmounting volume...\n";
		system("umount $mount_path");
	}
	
	
?>
/* mysql_backup.php */

Code: Select all

<?php
	
	include('commons.php');
	include('external_volume.php');
	
	mount_volume();
	
	if($external_volume_mount_fail) {
		echo "notifying recipients...\n";
		send_batch_email('MYSQL BACKUP FAIL ON '.date("F d Y", $now), 'COULD NOT CONNECT TO EXTERNAL VOLUME ON '.date("F d Y", $now));
		exit;
	}
	
	// -- DIRECTORY CHECK
	
	if(is_dir($mysql_current_date_directory)) {
		echo "directory for this date already exists...\n";
	} else {
		echo "directory for this date does not exist...attemping to create\n";
		if(mkdir($mysql_current_date_directory, 0777)) {
			echo "directory created...\n";
		} else {
			echo "could not create directory....\n";
			$mysql_current_date_directory_fail = true;
		}
	}
	echo "\n";
	
	// -- IF DIRECTORY FAIL
	
	if($mysql_current_date_directory_fail) {
		echo "exiting script...\n";
		exit;
	}
	
	// -- DO MASTER DUMP
	
	echo "creating master dump...\n";
	$mysql_master_dump_command = "mysqldump --user=someuser --password=somepass --all-databases | gzip > ".$mysql_current_date_directory.$mysql_master_dump_file;
	system($mysql_master_dump_command);
	echo "master dump created...\n\n";
	
	// - GET ALL DATABASE NAMES
	
	$connection = @mysql_connect('localhost', 'someuser', 'somepass');
	$result = @mysql_list_dbs($connection);
	$db_names = array();
	
	while($row = mysql_fetch_row($result)) {
		array_push($db_names, $row[0]);
	}
	
	// -- DO INDIVIDUAL BACKUPS
	
	for($i = 0; $i < count($db_names); $i++) {
		echo "creating dump for ".$db_names[$i]." ...\n";
		$dump_file = $date_format.'-'.$db_names[$i].'.gz';
		$dump_command = "mysqldump --user=someuser --password=somepass ".$db_names[$i]." | gzip > ".$mysql_current_date_directory.$dump_file;
		system($dump_command);
		echo "dump created successfully...\n\n";
	}
	
	// -- EMAIL NOTIFICATION
	
	echo "mysql backup successfull... or so we think... notifying recipients...\n";
	
	$message = "mysql backups completed on ".date("F d Y", $now)." at ".date("h:ma", $now);
	send_batch_email($message, $message);
	
	// -- UNMOUNT VOLUME
	unmount_volume();
	
	
?>
I am running all this from the command line

php mysql_backup.php

When I run it manually, it works just fine, mounts the external volume, and transfer over the the backups.

However, when I setup a cron job to do this task it fails. I piped the results into a txt file and there was no error produced by the mount_afp command. What gets me is that this worked perfectly for a week as a cron job, and no fails to mount the drive. I can see the script creating the mount directory, but it doesnt actually mount it.

Any help would be greatly appreciated!

Re: odd problem with crontab and system()

Posted: Fri Apr 09, 2010 5:37 pm
by paulstanely45
I managed to get what I think is the error generated. Can anyone shed any light on this error?

2010-04-09 17:28:35.396 mount_afp[7464:613] CFPreferences: user home directory at /Library/WebServerBackup/99 is unavailable. User domains will be volatile.
mount_afp: AFPMountURL returned error 1, errno is 1