Location: PHPKode > projects > web-cp - Web Hosting Control Panel > webcp/server/backup.php
<?  /*
	// File:	backup.php
	// Purpose:	backups
	// Author:	Peter Drake <hide@address.com>
	*/
	
mysql_connect($cfg['dbhost'], $cfg['dbuser'], $cfg['dbpass']);
mysql_select_db($cfg['dbname']);

if (is_array($domains)) {
for ($i=0; $i<sizeOf($domains); $i++) {
	if ($domains[$i] != "") {
	webcp_log(3,0,'backup.php',"Backup: Started for ".$domains[$i],0,$echo);

	$filename = $domains[$i]."-".time().".tar";
	$compressedFilename = $filename.".gz";


	if ($backupParms[1] == "on") {
		exec('tar -chf /tmp/'.$filename.' '.$cfg['webdir'].'/'.$domains[$i].' --exclude='.$domains[$i].'*.tar.gz');
	} else {
		exec('touch /tmp/'.$filename);
	}

	if ($backupParms[2] == "on") {
		$SQL = "SELECT id FROM domains WHERE domain = '" . $domains[$i] . "'";
		$dres = mysql_query($SQL);
		if ($drow = mysql_fetch_array($dres)) {
			$SQL = "SELECT username FROM users WHERE id = ".$drow["id"];
			$ures = mysql_query($SQL);
			
			for($j=0; $row=mysql_fetch_array($ures); $j++) {
				exec('tar -hrf /tmp/'.$filename.' /var/mail/'.$row["username"]);
			}
		}
	}

	exec('gzip /tmp/'.$filename);

	if ($backupParms[4] == "on") {

		$conn_id = ftp_connect($backupParms[6]);
		if (!$conn_id) {
			webcp_log(3,0,'backup.php',"Note: Connection failed to ".$backupParms[6],0,$echo);
		}
		$login_result = ftp_login($conn_id, $backupParms[7], $backupParms[8]);
		if ($backupParms[9] != "") {
			$dir_result = ftp_chdir($conn_id, $backupParms[9]);
		}
		if (ftp_put($conn_id, $compressedFilename, '/tmp/'.$compressedFilename, FTP_BINARY)) {
			webcp_log(3,0,'backup.php',"Backup: Remote Successful for ".$domains[$i],0,$echo);
		} else {
			webcp_log(3,0,'backup.php',"Backup: Remote Failed for ".$domains[$i],0,$echo);
		}
		ftp_close($conn_id);
	}

	if ($backupParms[3] == "on") {
		if (copy('/tmp/'.$compressedFilename, $cfg['webdir'].'/'.$domains[$i].'/_backups/'.$compressedFilename)) {
			webcp_log(3,0,'backup.php',"Backup: Local Successful for ".$domains[$i],0,$echo);
		} else {
			webcp_log(3,0,'backup.php',"Backup: Local Failed for ".$domains[$i],0,$echo);
		}
	}
	
	unlink('/tmp/'.$compressedFilename);

	webcp_log(3,0,'backup.php',"Backup: Completed for ".$domains[$i],0,$echo);
	}
}
}

if ($backupParms[0] == "on") {
	webcp_log(3,0,'backup.php',"Backup: Started MySQL",0,$echo);
	
	$filename = "MySQL-Backup-".time().".tar.gz";
	
	$checkDB = $cfg['prog']['mysqlcheck'].' --analyze --optimize --check-only-changed --auto-repair --all-databases -u'.$cfg['dbadminuser'].' -p'.$cfg['dbadminpass'];
	exec($checkDB);
	// webcp_log(3,0,'backup.php',$checkDB,0,$echo);

	$dumpDB = $cfg['prog']['mysqldump'].' -Ar/tmp/databases.sql -u'.$cfg['dbadminuser'].' -p'.$cfg['dbadminpass'];
	exec($dumpDB);
	// webcp_log(3,0,'backup.php',$dumpDB,0,$echo);

	exec('tar -chzf /tmp/'.$filename.' /tmp/databases.sql');

	unlink('/tmp/databases.sql');

	if ($backupParms[4] == "on") {

		$conn_id = ftp_connect($backupParms[6]);
		if (!$conn_id) {
			webcp_log(3,0,'backup.php',"Note: Connection failed to ".$backupParms[6],0,$echo);
		}
		$login_result = ftp_login($conn_id, $backupParms[7], $backupParms[8]);
		if ($backupParms[9] != "") {
			$dir_result = ftp_chdir($conn_id, $backupParms[9]);
		}
		if (ftp_put($conn_id, $filename, '/tmp/'.$filename, FTP_BINARY)) {
			webcp_log(3,0,'backup.php',"Backup: Remote MySQL Successful",0,$echo);
		} else {
			webcp_log(3,0,'backup.php',"Backup: Remote MySQL Failed",0,$echo);
		}
		ftp_close($conn_id);
	}

	if ($backupParms[3] == "on") {
		if (copy('/tmp/'.$filename, $cfg['webdir'].'/_backups/'.$filename)) {
			webcp_log(3,0,'backup.php',"Backup: Local MySQL Successful",0,$echo);
		} else {
			webcp_log(3,0,'backup.php',"Backup: Local MySQL Failed",0,$echo);
		}
	}

	unlink('/tmp/'.$filename);
	
	webcp_log(3,0,'backup.php',"Backup: Completed MySQL",0,1);
}

?>
Return current item: web-cp - Web Hosting Control Panel