This simple PHP script will provide a full dump of your MySQL database.

You need to provide database credentials, as well as an array of tables you'd like to backup. If you provide a "*" or no tables, a full database backup will be done.

The dump will be stored in a file, you'll can later copy elsewhere.





<?php
backup_tables('server host','mysql username','mysql password','mysql db name');

/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = '*')
{
	
	$link = mysql_connect($host,$user,$pass);
	mysql_select_db($name,$link);
	
	//get all of the tables
	if($tables == '*')
	{
		$tables = array();
		$result = mysql_query('SHOW TABLES');
		while($row = mysql_fetch_row($result))
		{
			$tables[] = $row[0];
		}
	}
	else
	{
		$tables = is_array($tables) ? $tables : explode(',',$tables);
	}
	
	//cycle through
	foreach($tables as $table)
	{
		$result = mysql_query('SELECT * FROM '.$table);
		$num_fields = mysql_num_fields($result);
		
		$return.= 'DROP TABLE '.$table.';';
		$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
		$return.= "\n\n".$row2[1].";\n\n";
		
		for ($i = 0; $i < $num_fields; $i++) 
		{
			while($row = mysql_fetch_row($result))
			{
				$return.= 'INSERT INTO '.$table.' VALUES(';
				for($j=0; $j < $num_fields; $j++) 
				{
					$row[$j] = addslashes($row[$j]);
					$row[$j] = ereg_replace("\n","\\n",$row[$j]);
					if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
					if ($j < ($num_fields-1)) { $return.= ','; }
				}
				$return.= ");\n";
			}
		}
		$return.="\n\n\n";
	}
	
	//save file
	$handle = fopen('db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');
	fwrite($handle,$return);
	fclose($handle);
}

Found this article interesting?
Subscribe to DomainRegister´s newsletter!

You can unsubscribe at any time by simply clicking the link in the footer of our emails. For information about our privacy practices, please visit our website.

We use Mailchimp as our marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp s privacy practices here.

  • MySQL, php, backup
  • 0 Utenti hanno trovato utile questa risposta
Hai trovato utile questa risposta?

Articoli Correlati

 How To Insert a Carriage Return in a PHP String

If you need to insert a Carriage Return in a string in PHP: Carriage return is: "\r"  But...

 How to Create a phpinfo page

The phpinfo() function outputs a huge amount of information about the system you're using, such...

 How To Make a Redirect in PHP

If you want that a page redirects automatically the user to a certain page or site (let's say,...

 How To Configure Easy WP SMTP Plugin To Use Gmail SMTP Service

Easy WP SMTP is a popular and free plugin for WordPress to send email(s) by an authenticated SMTP...

 How to automate MySQL backup using R1soft-Idera backup

Plain R1soft-Idera backup does not directly support mySQL database backup. But with this...