MySQL Backup Script without mysqldump

icon3
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...
Posted October 4, 2007 at 21:47 (UTC)

In a long time I’ve looked for a simple backup script to backup an entire database. I’ve actually not managed to find one script not relying on a system(“mysqldump [...]“) or similar nor a script that wasn’t a part of a huge solution and hence depended on a lot of other code.

So what’s a man to do? “Steal” a bit and make the rest himself. Precisely, but I did it reversely.

But here it is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php	
/**
 * Taken partially from wp-db-backup [1] and is partially self-made [2].
 *
 * [1] uses some libraries, whereas I [2] don't. This is possibly slower and less fail
 * tolerant, but then it doesn't depent on anything other than PHP and MySQL.
 *
 * I [2] am fully aware that it could be done nice with more functions, classes
 * and so on. But this is ment to be simple and do nothing else, so I've
 * permitted myself [2] to write these lines so "messy" :-).
 *
 * [1] : http://www.ilfilosofo.com/blog/wp-db-backup/
 * [2] : Mikkel Meyer Andersen (aka. mikl-dk), http://www.scienco.org 
 */
 
/*
 * Here a MySQL-connection is made and the database chosen
 */
require('../top.php');
 
/*
 * Just a nice little function seperated from the other mess
 */
function secure_addslashes($str = '') 
{
	return str_replace('\'', '\\\'', str_replace('\\', '\\\\', $str));
} 
 
$output = '';
 
/* 
 * From [1]: ensures that the different non-printable chars are printed, e.g. newline
 */
$search = array("\x00", "\x0a", "\x0d", "\x1a");
$replace = array('\0', '\n', '\r', '\Z');
 
/*
 * All the tables from the selected database are selected
 */
$result = @mysql_query("SHOW TABLES") or exception(mysql_error());
 
while ($data = mysql_fetch_array($result))
{
	/* From [1]: Used to differ between using ' to non-integer types 
	 * and use nothing to integer types
	 */
	$result_f = @mysql_query("DESCRIBE `" . $data[0] . "`") or exception(mysql_error());
 
	$ints = array();
 
	while ($data_f = mysql_fetch_assoc($result_f))
	{
		if (
				(false !== strpos(strtolower($data_f['Type']), 'tinyint')) 		||
				(false !== strpos(strtolower($data_f['Type']), 'smallint')) 	||
				(false !== strpos(strtolower($data_f['Type']), 'mediumint')) 	||
				(false !== strpos(strtolower($data_f['Type']), 'int')) 			||
				(false !== strpos(strtolower($data_f['Type']), 'bigint')) 		||
				(false !== strpos(strtolower($data_f['Type']), 'timestamp')) 
			) 
		{
				$ints[strtolower($data_f['Field'])] = "1";
		}
	}	
 
	$result_t = @mysql_query("SHOW CREATE TABLE `" . $data[0] . "`") or exception(mysql_error());
	$data_t = mysql_fetch_array($result_t);
 
	$output .= "DROP TABLE IF EXISTS `" . $data[0] . "`;";
	$output .= "\n\n";
	$output .= $data_t[1] . " ;\n";
	$output .= "\n\n";
 
	$result_d = @mysql_query("SELECT * FROM `" . $data[0] . "`") or exception(mysql_error());
	$entries = 'INSERT INTO `' . $data[0] . '` VALUES (';
 
	while ($data_d = mysql_fetch_assoc($result_d))
	{		
		$values = array();
 
		foreach ($data_d as $key => $value) 
		{		
			if ($ints[strtolower($key)]) 
			{
				$values[] = $value;
			} 
 
			else 
			{
				$values[] = "'" . str_replace($search, $replace, secure_addslashes($value)) . "'";
			}
		}
 
		$output .= " \n" . $entries . implode(', ', $values) . ') ;';
	}
 
	$output .= "\n\n\n";
}
 
echo $output;
?>

And you can add some functionality like this in order to have a compressed file (requires zlib):

100
101
102
103
$filename = date("Y-m-d") . '.gz';
$zp = gzopen($filename, "w9");
gzwrite($zp, $output);
gzclose($zp);

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.