Location: PHPKode > scripts > MySQL Binlog Rotate > mysql-binlog-rotate/class.binlogrotate.php
<?php
/*!*****************************************************************************************************
 *	@file		class.binlogrotate.php
 *	@author		Jos Pape
 *	@version	1.0
 *	@brief		Contains a class that rotates the binlog in a MySQL5 setting
 ******************************************************************************************************/

/*!**************************************************************************
 *	@class 		Rotate
 *	@author		Jos Pape <hide@address.com>
 *	@version	1.0
 *	@copyright	2008 Jos Pape
 *	@license	LGPL
 *	@brief 		Facilitates rotation of the binlogs
 **************************************************/
class Rotate
{
	/*!******************************
	 * holds all hosts
	 */
	var $host = array();
	
	/*!******************************
	 * holds the master of this host#
	 */
	var $master = array();
	
	/*!******************************
	 * holds the username of the host#
	 */
	var $user = array();
	
	/*!******************************
	 * holds the passowrd of this host#
	 */
	var $pass = array();
	
	/*!******************************
	 * holds the local domainname
	 * if your using a local domain instead of IP adresses fillin your local domain here, else leave empty
	 */
	var $localdomain = "";//
	
	/*!******************************
	 * Holder of all the made connections
	 */
	var $connection = array();
	
	/*!******************************
	 * Holder of all the masters NOT to update becourse a slave is down
	 */
	var $noMasterUpdate = array();
	
	/*!******************************
	 * Holder of the smalles position in the master.
	 * we need to purge to this file.
	 */
	var $masterLogPosition = array();
	
	/*!************
	 * The constructor.
	 * @return	Boolean	is always true.
	 */ 
	function Rotate()
	{
		$retVal = true;
		return $retVal;
	}
	
	/*!************
	 * Adding a host to the mysql cluster.
	 * 
	 * @return	Boolean	is always true.
	 */ 
	function addHost($host, $username, $password, $master = "")
	{
		$x = count($this->host);
		$this->host[$x] = $host;
		$this->user[$x] = $username;
		$this->pass[$x] = $password;
		if($master != "")
			$this->master[$x] = $master;
	}
	
	/*!************
	 * Usage: Setting the local domain to add behind the hosts(to connect to)
	 * Explanation: When you're using local domainnames for the mysql servers (witch is recommended) you can set the localdomain to be added behind the hoste name here. 
	 * default it's empty.  
	 * @return	Boolean	is always true.
	 */
	function setLocaldomain($domain)
	{
		$retVal = true;
		$this->localdomain = $domain;
		return $retVal; 
	}
	
	/*!************
	 * Reading all the slave binlog position
	 * 
	 * @return	Boolean	is always true.
	 */	
	function checkSlaveStatus()
	{
		$retVal = true;
		foreach($this->host AS $number => $hostname)
		{
			if(@!$this->connection[$number] = mysql_connect($this->host[$number].$this->localdomain, $this->user[$number], $this->pass[$number]))
			{	/* exclude this master from the rotation, untill this server is up and running again */
				if(isset($this->master[$number]))
					$this->noMasterUpdate[] = $this->master[$number];				
			}
			else
			{
				if(isset($this->master[$number]))
				{	/* there is no need to get the current position when there is no master..... */
					$query = "SHOW SLAVE STATUS";
					if($result = mysql_query($query, $this->connection[$number]))
					{
						$row = mysql_fetch_assoc($result);
						$currentBinlog = $row['Master_Log_File'];
						if(!isset($this->masterLogPosition[$this->master[$number]]))
						{	/* no position to compare to, so we can savely set this position */
							$this->masterLogPosition[$this->master[$number]] = $currentBinlog;
						}
						else
						{	/* we need to check witch log is older, becourse we will only remove to the oldest binlog */
							$storedlocation = str_replace("mysql-bin.","",$this->masterLogPosition[$this->master[$number]]) * 1;
							$currentlocation = str_replace("mysql-bin.","",$currentBinlog) * 1;
							if($storedlocation > $currentlocation)
								$this->masterLogPosition[$this->master[$number]] = $currentBinlog;	
						}
					}
					else
					{	/* the query failed, so we cannot determine the position, and we do nog purge the files on this master */
						$this->noMasterUpdate[] = $this->master[$number];	
					}
				}
			}
		}
		return $retVal;
	}
	
	/*!************
	 * Purge all the binlogs
	 * 
	 * @return	Boolean	is always true.
	 */	
	function RotateBinlog()
	{
		$retVal = true;
		$this->checkSlaveStatus();
		if(isset($this->masterLogPosition) && is_array($this->masterLogPosition))
		{
			foreach($this->masterLogPosition AS $masterhost => $toBinlogFile)
			{
				if(!in_array($masterhost, $this->noMasterUpdate) && $toBinlogFile != "")
				{
					// get the connection for this master
					foreach($this->host AS $number => $hostname)
					{
						if($hostname == $masterhost && isset($this->connection[$number]) && $this->connection[$number] !== false)
						{
							mysql_query("PURGE MASTER LOGS TO '".$toBinlogFile."'",$this->connection[$number]);
							break 1;
						}
					}	
				}
			}
		}
		return $retVal;	
	}	
} 
/*********
 * Star configuration
 */
$Rotate = new Rotate;
$Rotate->setLocaldomain(".domain.local");
$Rotate->addHost("mysql1", "username", "pass");
$Rotate->addHost("mysql2", "username", "pass", "mysql1");
$Rotate->addHost("mysql3", "username", "pass", "mysql1");
$Rotate->addHost("mysql4", "username", "pass", "mysql1");
$Rotate->addHost("mysql5", "username", "pass", "mysql1");
$Rotate->RotateBinlog();
// */

/*********
 * Ring configuration
 * not recommended /
$Rotate = new Rotate;
$Rotate->setLocaldomain(".domain.local");
$Rotate->addHost("mysql1", "username", "pass", "mysql5");
$Rotate->addHost("mysql2", "username", "pass", "mysql1");
$Rotate->addHost("mysql3", "username", "pass", "mysql2");
$Rotate->addHost("mysql4", "username", "pass", "mysql3");
$Rotate->addHost("mysql5", "username", "pass", "mysql4");
$Rotate->RotateBinlog();
// */

?>
Return current item: MySQL Binlog Rotate