Jump to content

Backup database in UTF-8


eevan79

Recommended Posts

I am using small script (with crone) to backup database every morning, zip and send me mail (with .sql/zip attached).

 

 

But backup is not in UTF-8 format, so I get this:

"Č" -> "C"

"Š" -> "S"

etc.

 

 

I have tried various solution but nothing works. Also tried utf_encode() function, but then I get strange characters.

 

 

Script looks like this:

 

function backup_tables($host,$user,$pass,$name,$tables = '*',$date)
{

$link = mysql_connect($host,$user,$pass);
mysql_select_db($name,$link);

//ALL 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);
}

//GET DATA
foreach($tables as $table)
{
	$result = mysql_query('SELECT * FROM '.$table);
	$num_fields = mysql_num_fields($result);

	$return.= 'DROP TABLE IF EXISTS '.$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] = str_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
$handle = fopen('Backup-'.$date.'.sql','w+');
fwrite($handle,$return);
fclose($handle);
}

 

 

Database Collation: utf8_binType: [/size]MyISAM

[/size]How to save .sql file in proper UTF-8 Format?[/size]

Link to comment
https://forums.phpfreaks.com/topic/225284-backup-database-in-utf-8/
Share on other sites

 

$link = mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($dbname,$link);


$backupFile = $dbname . date("d-m-Y-H-i-s") . '.gz';
$command = "mysqldump --opt -h $dbhost -u $dbuser -p $dbpass $dbname | gzip > $backupFile";
system($command);

It creates empty .gzip file. It doesnt work.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.