Location: PHPKode > projects > HuMo-gen > admin/include/backup.php
<?php
// *** Safety line ***
if (!defined('ADMIN_PAGE')){ exit; }

echo '<h1 align=center>'.$language["backup"].'</h1>';

echo $language["backup_intro"];

echo '<h2>*** Deze backup methode is nog EXPERIMENTEEL / This backup method is still EXPERIMENTAL ***</h2>';

echo '<form method="post" action="'.$_SERVER['PHP_SELF'].'">';
echo '<input type="hidden" name="page" value="'.$page.'">';

echo '<table class="humo" border="1" cellspacing="0" width="100%">';
echo '<tr><td>'.$language["backup_mail"].'</td><td><input type="text" name="e-mail" value="" size="30"></td></tr>';

$backup_bestand=DATABASE_NAME;
print '<tr><td>'.$language["backup_file"].'</td><td><input type="text" name="backup_bestand" value="'.$backup_bestand.'" size="30"></td></tr>';

echo '<tr><td><br></td><td><input type="Submit" name="backup" value="'.$language["backup"].'"></td></tr>';
echo '</table>';

echo '</form>';

if (isset($_POST['backup'])){
/*    Mysql Backupper
*    Backup your MySql Without PhpMyAdmin.
*
*    Made by Han van der Veen (hide@address.com)
*    http://haneev.nl
*/

// config hier mag je dingen veranderen
$cfg['naam']    = DATABASE_USERNAME; // inlognaam mysql
$cfg['ww']      = DATABASE_PASSWORD; // wachtwoord mysql
$cfg['host']    = DATABASE_HOST; // host, meestal localhost
$cfg['db']      = DATABASE_NAME; // database
$cfg['bes']     = 1; // schrijven in bestand, 1=ja,0=nee
$cfg['view']    = 2; // 0=query's weergeven+error's, 1= alleen querys,2= alleen errors,3=niks
$cfg['str']     = 1; // structuur, 1=ja,2=nee
$cfg['data']    = 1; // data, 1=ja,0=nee
$cfg['dbs']     = 1; // met create db?, 1=ja,0=nee

// een data array
$key = array("PRI"=>"PRIMARY KEY","UNI"=>"UNIQUE KEY","MUL"=>"KEY",""=>"");

// connect to database
$err = array(); // error array
$arr = array(); // stuctuur array
// connect to host
//if(mysql_connect($cfg['host'],$cfg['naam'],$cfg['ww'])) {
//  // select db
//  if(!mysql_select_db($cfg['db'])) {
//    $err[] = 'Verbinding DB mislukt';
//  }
//} else {
//  // host mislukt
//  $err[] = 'Verbinding host mislukt';
//}

$querys[] = "-- Generate By Backupper\n";
$querys[] = "-- MySql Dump\n";
$querys[] = "-- Make your dump without PhpMyAdmin\n";
$querys[] = "-- Version: 1.0\n";
$querys[] = "-- \n";
//$querys[] = "-- Made By Han van der veen\n";
//$querys[] = "-- http://haneev.nl (hide@address.com)\n";
//$querys[] = "--\n";
$querys[] = "-- Database: ".$cfg['db']."\n";
$querys[] = "-- ";
if($cfg['view'] <= 1) {
	echo "<pre>";
	for($i=0;$i<count($querys);$i++) {
		echo $querys[$i];
	}
	echo "</pre>";
}

/*
if($cfg['dbs'] == 1) {
	if($cfg['bes'] == 1) {
		$querys[] = "-- \n";
		$querys[] = "-- Create Database \n";
		$querys[] = "CREATE DATABASE ".$cfg['db'].";\n";
		$querys[] = "USE ".$cfg['db'].";\n";
		$querys[] = "-- Rest \n\n";
	}
	if($cfg['view'] <= 1) {
		echo "<pre>";
		echo "-- \n";
		echo "-- create Database \n";
		echo "CREATE DATABASE ".$cfg['db'].";\n";
		echo "USE ".$cfg['db'].";\n";
		echo "-- rest \n\n";
		echo "</pre>";
	}
}
*/

// verkrijg tables

//$sql="SHOW TABLES FROM ".$cfg['db'];
$sql="SHOW TABLES";
$get_tab = mysql_query($sql);
while($x = mysql_fetch_array($get_tab)) {
	// structuur vars
	$s = "-- \n";
	$s .= "-- Structuur van tabel: ".$x[0]."\n";
	$s .= "-- \n\n";
	$s .= "CREATE TABLE ".$x[0]." ( \n";
	$k = ''; // keys

	// *** Voorkomen dat er teveel komma's in de querie komen ***
	$veld=false;
	// get tabellen
	$get_v = mysql_query("SHOW COLUMNS FROM ".$x[0]);
	// EIGENWIJS DING
	while($iks = mysql_fetch_assoc($get_v)) {
		$temp = $iks['Field'];
		// zet in array voor de data
		if(!isset($velden[$x[0]][$temp])) {
			$velden[$x[0]][$temp] = $temp;
		}
		// table maken, stuctuur
		if($cfg['str'] == 1) {
			//if(!isset($arr[$x[0]][$ike['Field']])) {
			if(!isset($arr[$x[0]][$iks['Field']])) {
				// in array proppen
				$arr[$x[0]][$iks['Field']] = $iks['Field'];
				// null berekenen
				$null = (!empty($iks['Null']) && $iks['Null'] == "YES") ? "NULL" : "NOT NULL";
				// bij de string zetten
				if ($veld==true){ $s.=",\n";   }
				//$s .= $iks['Field']." ".$iks['Type']." ".$null." ".$iks['Extra'].",\n";
				$s .= $iks['Field']." ".$iks['Type']." ".$null." ".$iks['Extra'];
				$veld=true;
				// als key niet leeg is
				if(!empty($iks['Key'])) {
				  //$k .= $key[$iks['Key']]." (".$iks['Field']."),\n";
				  $k .= ",\n".$key[$iks['Key']]." (".$iks['Field'].")";
				}
			}
		}
	}

	// structuur weergeven
	//$var = $s.substr($k,0,-2)."\n);";
	$var = $s.$k."\n);";
	if($cfg['view'] <= 1 && $cfg['str'] == 1) {
		echo '<pre>'.$var.'</pre>';
	}
	// structuur in bestand
	if($cfg['str'] == 1) {
		$querys[] = "\n";
		$querys[] = $var;
		$querys[] = "\n";
	}

	// data
	$data = mysql_query("SELECT * FROM ".$x[0]);
	// zet alla data in array's
	while($z = mysql_fetch_array($data)) {
		// velden en data
		$imkeys = ""; // keys str
		$imdata = ""; // data str
		foreach($velden[$x[0]] as $val) {
			// zet data in een str
			$imkeys .= $val.",";
			$imdata2=addslashes($z[$val]);
			//$imdata .= "'".$z[$val]."',";
			$imdata .= "'".$imdata2."',";
		}
		// query maken
		$q = "INSERT INTO ".$x[0]." (".substr($imkeys,0,-1).") VALUES (".substr($imdata,0,-1).");\n";
		// weergeven
		if($cfg['view'] <= 1 && $cfg['data'] == 1) {
			echo $q.'<br>';
		}
		// query in bestand
		if($cfg['data'] == 1) {
			$querys[] = $q;
		}
		// dellen
		unset($q);
	} // data
} // grote while, tables

// bestand afhandeling
//if($cfg['bes']==1) {

//$backup_bestand='backup_tmp/'.$cfg['db'].'.sql';
$backup_bestand='backup_tmp/'.$_POST['backup_bestand'].'.sql';
$dat = '';
// openen
$fp = fopen($backup_bestand,'w');     //cfg['db'].'.sql','w');
// geen bestand aanmaken
if(!$fp) {
	$err[] = 'Kan bestand niet openen';
}
// inladen in var
foreach($querys as $content) {
	$dat .= $content;
}
// schrijven
$write = fwrite($fp,$dat);
// als schrijven is mislukt
if(!$write) {
	$err[] = 'Kan niet in bestand schrijven';
}
// sluiten
fclose($fp);
//}

// Hier bestand zippen en mailen
// Do the backup.
//$sResult = passthru($sBackupCommand . "; gzip $sFilename");

exec("gzip $backup_bestand");

/*
$backup_zip=$backup_bestand.'.zip';
$zip = new ZipArchive();
$filename = $backup_zip;
if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
		exit("cannot open <$filename>\n");
}
$zip->addFile($backup_bestand);
//$zip->addFromString("testfilephp.txt" . time(), "#1 This is a test string added as testfilephp.txt.\n");
//$zip->addFromString("testfilephp2.txt" . time(), "#2 This is a test string added as testfilephp2.txt.\n");
//$zip->addFile($thisdir . "/too.php","/testfromfile.php");
//echo "numfiles: " . $zip->numFiles . "\n";
echo "status:" . $zip->status . "\n";
$zip->close();
*/

//$sFilename .= ".gz";

// Create the message.
//$sFilename = "backup-[whatever-it-is]-" . date("Y-m-d") . ".[extension]";
$sFilename = $backup_bestand.'.gz';

echo 'Backup bestand: '.$sFilename.'<br>';
echo 'Wordt gemaild naar: '.$_POST['e-mail'].'<br>';

//  -- E-mail Address --
//	This is the e-mail address to which the message will be sent.
//$sEmailAddress = "[your e-mail address]";
$sEmailAddress = $_POST['e-mail'];

//  -- E-mail Subject --
//	This is the subject that will be on the e-mail you receive.
//$sEmailSubject = "[something meaningful]";
$sEmailSubject = "[BACKUP HuMo-gen database]";

//  -- E-mail Message --
//	This is the text of the message that will be sent.
//$sMessage = "Compressed database backup file $sFilename.gz attached.";
$sMessage = "Compressed database backup file $sFilename attached.";

$sMimeBoundary = "<<<:" . md5(time());
$sData = chunk_split(base64_encode(implode("", file($sFilename))));

$sHeaders = "From: $sEmailAddress\r\n"
		. "MIME-Version: 1.0\r\n"
		. "Content-type: multipart/mixed;\r\n"
		. " boundary=\"$sMimeBoundary\"\r\n";

$sContent = "This is a multi-part message in MIME format.\r\n\r\n"
		. "--$sMimeBoundary\r\n"
		. "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"
		. "Content-Transfer-Encoding: 7bit\r\n\r\n"
		. $sMessage."\r\n"
		. "--$sMimeBoundary\r\n"
		. "Content-Disposition: attachment;\r\n"
		. "Content-Type: Application/Octet-Stream; name=\"$sFilename\"\r\n"
		. "Content-Transfer-Encoding: base64\r\n\r\n"
		. $sData."\r\n"
		. "--$sMimeBoundary\r\n";

// Send the message.
mail($sEmailAddress, $sEmailSubject, $sContent, $sHeaders);

// Bestand weer verwijderen
//unlink($backup_bestand);
unlink($sFilename);

// error afhandeling
if(count($err) == 0) {
	echo '<br><b>Exporteren is Gelukt</b><br>';
} else {
	if($cfg['view'] == 0 || $cfg['view'] == 2) {
		echo 'Deze errors zijn opgetreden<br>';
		foreach($err as $val) {
			echo $val .'<br/>';
		}
	}
}
/*
*  Einde backupper
*/

} // Einde backup procedure

?>
Return current item: HuMo-gen