Dump MySQL

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I must thank friends for suggesting on title: PHP No Session.

If the follwing is very important concept, the, write me
comments, okay. Can you please re-form it?


Bimal Poudel, bimal@radio.fm
Kathmandu, NEPAL

Here is one more idea:

for(i=0; i<databaseInMySQL; i++)
{
  select database
  for(j=0; j<tablesInDatabase; j++)
  {
    select * INTO OUTFILE C:\dump\database.table.dump from
table;
    <?php:: delay(1); ::::::::::::
  }
}

This dumps all the tables in each databases into c:\dump.

The problem is: we need to delay the loop for at least a second,
or mysql will crash with illection function, the Windows'
feature!

Further more, setup a table like system.nobackuptables, and
enter as:
id database table
-----------------
1  mysql    user
...

Before dumping, check if the table is listed in the particular
(system.nobackuptable), like database is mysql and table is user
in the above. If so, do not dump it. This will let you dump only
the required databases or tables. If you like, here is a WORKING
code for you:



mysql> USE system;
mysql> desc nobackupdatabase;
+-----------+----------------------+------+-----+---------+----------------+
| Field     | Type                 | Null | Key | Default |
Extra          |
+-----------+----------------------+------+-----+---------+----------------+
| id        | smallint(3) unsigned |      | PRI | NULL    |
auto_increment |
| _database | varchar(30)          |      | UNI |         |     
          |
+-----------+----------------------+------+-----+---------+----------------+

mysql> select * from system.nobackupdatabase;
+----+-----------+
| id | _database |
+----+-----------+
|  1 | mysql     |
|  2 | poems     |
|  3 | analysis  |
|  4 | system    |
+----+-----------+

mysql> desc nobackuptable;
+-----------+----------------------+------+-----+---------+----------------+
| Field     | Type                 | Null | Key | Default |
Extra          |
+-----------+----------------------+------+-----+---------+----------------+
| id        | smallint(3) unsigned |      | PRI | NULL    |
auto_increment |
| _database | varchar(30)          |      | MUL |         |     
          |
| _table    | varchar(30)          |      |     |         |     
          |
+-----------+----------------------+------+-----+---------+----------------+

mysql> select * from nobackuptable;
+----+-----------+----------+
| id | _database | _table   |
+----+-----------+----------+
|  1 | songs     | songs_cs |
+----+-----------+----------+

mysql>






<?php
error_reporting(0);
set_time_limit(300);

print('<PRE>');



$location='c:\\\\dbs\\\\'.date('Ymdims');
mkdir($location, 0700);
	

/*
* Put the CSV files of each databases to a text file.
*/
function csvBackup($database='test', $table='test')
{
	global $location;

#	echo('<HR><FONT
COLOR=RED>'.$database.'.'.$table.'</FONT><BR>');
	$file='"'.$location.'\\\\'.$database.'___'.$table.'.csv" ';
	$query='SELECT * INTO OUTFILE '.$file.' FIELDS TERMINATED BY
\',\' OPTIONALLY ENCLOSED BY \'"\' LINES TERMINATED BY "\r\n"
FROM '.$database.'.'.$table;
	mysql_query($query);
	echo('<BR>....Backing up: '.$database.'.'.$table.'<BR>');
	echo($query);
} // csvBackup

function csvRestore($file)
{
} // csvRestore


function listColumn($table='test', $column='test')
{
	$query = 'SELECT '.$column.' FROM '.$table;
	$result = mysql_query($query) or die("Query failed : " .
mysql_error());

	$entries=array();
	while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
		foreach ($line as $col_value) {
			#echo $col_value.'<BR>';
			$entries[]=$col_value;
		}
	}
	return($entries);
} // listColumn()


/*
* Check, if array contains a value.
*/
function member($array, $value)
{
	$elements=count($array);
	$i=0;

	if($elements==0)
		return(false);
	
	for($i=0; $i<$elements; $i++)
	{
#		echo('<BR>............<B>'.$array[$i].'</B>---'.$value);
		if($array[$i]==$value)
			return(true);
		else
			continue;
	}
	return(false);
} // member



function connectDBServer()
{
	return(mysql_connect('localhost', 'bimal', ''));
} // connectDBServer()

function listDatabases()
{
	$dbname='';
	$databaseList=array();
	$link=connectDBServer();
	$db_list = @mysql_list_dbs($link);
	while ($dbname = @mysql_fetch_object($db_list)) {
		$database=$dbname->Database;
		$databaseList[]=$database;
		$result = @mysql_list_tables($dbname->Database);
	}
	return($databaseList);
} // listDatabases()

function listTables($database='test')
{
#	echo('Listing for: '.$database.'<BR>');

	$tableList=array();
	connectDBServer();
	$result = mysql_list_tables($database);
	
	while ($row = mysql_fetch_row($result)) {
		$tableList[]=$row[0];
#		print_r($row);
	}
	#print_r($tableList);
#	echo('<BR>...'.$tableList[0]);
	return($tableList);
} // listTables()


function loopAllTables()
{
	$databaseList=listDatabases();
	$nobackupDatabase=listColumn('system.nobackupdatabase',
'_database');

	$tableList=array();
	$DBElements=count($databaseList);
	$TBElements=0;
	$i=0;
	$j=0;

	echo('Not required to backup:<BR>');
	print_r($nobackupDatabase);
	echo('Not BACKED UP TABLES:<BR>');
	$blockedTables=listColumn('system.nobackuptable',
'CONCAT(_database, ".", _table)');
	print_r($blockedTables);
	echo('Available Database list:<BR>');
	print_r($databaseList);
	echo('<HR>');

	for($i=0; $i<$DBElements; $i++)
	{
#		echo('Member test: '.$databaseList[$i]);
		if(member($nobackupDatabase, $databaseList[$i]))
		{
#			echo('<BR>Forbidding: '.$databaseList[$i].'<BR>');
			continue;
		}

		$tableList=array();

#		echo('<BR>....'.$i.'<BR>');
		$tableList=listTables($databaseList[$i]);
#		print_r($tableList);
		$TBElements=count($tableList);
#		echo('<h1>'.$TBElements.'</h1>');
		$blockedTables=array();
		set_time_limit(30);
		for($j=0; $j<$TBElements; $j++)
		{
			$blockedTables=listColumn('system.nobackuptable	WHERE
_database="'.$databaseList[$i].'"', 'CONCAT(_database, ".",
_table)');
			if(member($blockedTables,
$databaseList[$i].'.'.$tableList[$j]))
			{
#				echo('<BR>NO backing up:.....<FONT
COLOR=GREEN>'.$tableList[$j].'</FONT>');
				continue;
			}
			else
			{
#				echo('<BR>Yes: '.$tableList[$j]);
				csvBackup($databaseList[$i], $tableList[$j]);
			}
			sleep(1);
		}
#		print_r($tableList);
	} // for
	echo('<HR>');
} // loopAllTables()

loopAllTables();


/*
echo('<HR><FONT COLOR=RED>');
$blockedDatabases=listColumn('system.nobackupdatabase',
'_database');
$db='mysql';
if(member($blockedDatabases, $db))
	echo('<HR>Need not backup: '.$db."<BR>");
print_r($blockedDatabases);


$tb='mysql.user';
$blockedTables=listColumn('system.nobackuptable	WHERE _database
LIKE "%s%"', 'CONCAT(_database, ".", _table)');
if(member($blockedTables, $tb))
	echo('<HR>Need not backup: <FONT
COLOR=RED>'.$tb.'</FONT><BR>');
print_r($blockedTables);
*/
?>





=====
Bimal Poudel
(Information Management)
Kathmandu, Nepal

http://fakenepal.tripod.com


		
__________________________________
Do you Yahoo!?
Yahoo! Mail - 50x more storage than other providers!
http://promotions.yahoo.com/new_mail


------------------------ Yahoo! Groups Sponsor --------------------~--> 
Yahoo! Domains - Claim yours for only $14.70
http://us.click.yahoo.com/Z1wmxD/DREIAA/yQLSAA/saFolB/TM
--------------------------------------------------------------------~-> 

PHP Data object relational mapping generator - http://www.meta-language.net/ 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/php-objects/

<*> To unsubscribe from this group, send an email to:
    php-objects-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 


[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Soap]     [Kernel Newbies]     [Yosemite]     [Yosemite Campsites]

  Powered by Linux