Massen MySQL Export WordPress via PHP

Von Christian am 8. Januar 2021

Wenn Ihr mehrere Websites transferieren wollt, benötigt Ihr einen automatisierten MySQL Export.
Hier haben wir ein Beispiel für WordPress aufgesetzt. Voraussetzung ist die Mysqldump.php sowie die aktuelle Pfadangabe.
Weil die Kontanten von WordPress eingelesen werden, müsst ihr das PHP Script öfters ausführen, bis alle Datenbanken exportiert sind.

<?php

include_once('Mysqldump.php');
$path = "PFAD";
		
function parseConfig($file) {
	
	$fh = @fopen($file, 'r');
	if ($fh) {
			while (!feof($fh)) {
					$data[] = fgets($fh);
			}
			fclose($fh);

			foreach ($data as $line) {
					if (preg_match('/define.*(DB_USER|DB_HOST|DB_PASSWORD|DB_NAME)/', $line)) {
							$conf[] = $line;
					}
			}

			if (@count($conf) < 3) {
					print "Could not find constants DB_HOST, DB_USER, DB_PASSWORD, DB_NAME\n";
					exit;
			}

			$php_code = implode($conf);

			eval($php_code);

	}
}

$websites = scandir($path);

foreach ($websites as $website) {
	$dumpfile = "" . $website . "_" . date("Y-m-d") . ".sql";
	if ( !file_exists($dumpfile)) {
		$config = $path."/".$website."/wp-config.php";
		
		if ( file_exists($config)) {
			
			$config = parseConfig($config);		
		 
			echo "Start dump\n";
			
			$dump = new Ifsnop\Mysqldump\Mysqldump("mysql:host=".DB_HOST.";dbname=".DB_NAME."", DB_USER, DB_PASSWORD);
			$dump->start($dumpfile);

			echo "-- Dump completed -- ";
			echo $dumpfile;
			
			exit;
		}
	}
}
Hosting Pilot Support Telefon

Telefonische Beratung

+49 2131 77 69 59 5
Hosting Hilfe

Online Hilfe