Location: PHPKode > projects > web-cp - Web Hosting Control Panel > webcp/server/cron/schedbackups.inc.phps
<?  /*
	// File:	schedbackups.inc.phps
	// Purpose:	scheduled backups
	// Author:	Peter Drake <hide@address.com>
	*/

$SQL = "SELECT domain, files, email, databases, remote, local, ftpserver, username, directory, DECODE(password,'".$cfg['key']."') AS password FROM backups WHERE recurrance = '" . $schedbackups . "'";
$backupresult = mysql_query($SQL);

for ($i=0; $domainrow=mysql_fetch_array($backupresult); $i++) {
	
if ($domainrow["domain"] != "") {
	webcp_log(3,0,'schedbackups.inc.phps',"SchedBackup: Started for ".$domainrow["domain"],0,$echo);

	$filename = $domainrow["domain"]."-".time().".tar";
	$compressedFilename = $filename.".gz";

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

	if ($domainrow["mail"] == 1) {
		$SQL = "SELECT id FROM domains WHERE domain = '" . $domainrow["domain"] . "'";
		$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 ($domainrow["remote"] == 1) {

		$conn_id = ftp_connect($domainrow["ftpserver"]);
		if (!$conn_id) {
			webcp_log(3,0,'schedbackups.inc.phps',"Note: Connection failed to ".$domainrow["ftpserver"],0,$echo);
		}
		$login_result = ftp_login($conn_id, $domainrow["username"], $domainrow["password"]);
		if ($domainrow["directory"] != "") {
			$dir_result = ftp_chdir($conn_id, $domainrow["directory"]);
		}
		if (ftp_put($conn_id, $compressedFilename, '/tmp/'.$compressedFilename, FTP_BINARY)) {
			webcp_log(3,0,'schedbackups.inc.phps',"SchedBackup: Remote Successful for ".$domainrow["domain"],0,$echo);
		} else {
			webcp_log(3,0,'schedbackups.inc.phps',"SchedBackup: Remote Failed for ".$domainrow["domain"],0,$echo);
		}
		ftp_close($conn_id);
	}

	if ($domainrow["local"] == 1) {
		if (copy('/tmp/'.$compressedFilename, $cfg['webdir'].'/'.$domainrow["domain"].'/_backups/'.$compressedFilename)) {
			webcp_log(3,0,'schedbackups.inc.phps',"SchedBackup: Local Successful for ".$domainrow["domain"],0,$echo);
		} else {
			webcp_log(3,0,'schedbackups.inc.phps',"SchedBackup: Local Failed for ".$domainrow["domain"],0,$echo);
		}
	}
	
	unlink('/tmp/'.$compressedFilename);

	webcp_log(3,0,'schedbackups.inc.phps',"SchedBackup: Completed for ".$domainrow["domain"],0,$echo);

}

if ($domainrow["databases"] == 1) {
	webcp_log(3,0,'schedbackups.inc.phps',"SchedBackup: 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,'schedbackups.inc.phps',$checkDB,0,$echo);

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

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

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

	if ($domainrow["remote"] == 1) {

		$conn_id = ftp_connect($domainrow["ftpserver"]);
		if (!$conn_id) {
			webcp_log(3,0,'schedbackups.inc.phps',"Note: Connection failed to ".$domainrow["ftpserver"],0,$echo);
		}
		$login_result = ftp_login($conn_id, $domainrow["username"], $domainrow["password"]);
		if ($domainrow["directory"] != "") {
			$dir_result = ftp_chdir($conn_id, $domainrow["directory"]);
		}
		if (ftp_put($conn_id, $filename, '/tmp/'.$filename, FTP_BINARY)) {
			webcp_log(3,0,'schedbackups.inc.phps',"SchedBackup: Remote MySQL Successful",0,$echo);
		} else {
			webcp_log(3,0,'schedbackups.inc.phps',"SchedBackup: Remote MySQL Failed",0,$echo);
		}
		ftp_close($conn_id);
	}

	if ($domainrow["local"] == 1) {
		if (copy('/tmp/'.$filename, $cfg['webdir'].'/_backups/'.$filename)) {
			webcp_log(3,0,'schedbackups.inc.phps',"SchedBackup: Local MySQL Successful",0,$echo);
		} else {
			webcp_log(3,0,'schedbackups.inc.phps',"SchedBackup: Local MySQL Failed",0,$echo);
		}
	}

	unlink('/tmp/'.$filename);
	
	webcp_log(3,0,'schedbackups.inc.phps',"SchedBackup: Completed MySQL",0,1);
}

}

webcp_log(3,0,"schedbackups.inc.phps",$schedbackups." scheduled backups ran.",0);
?>
Return current item: web-cp - Web Hosting Control Panel