/
bacula_DBcopy_MySQL2sqlite.php
155 lines (129 loc) · 3.51 KB
/
bacula_DBcopy_MySQL2sqlite.php
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/*
* Script for copy all data from MySQL Bacula v.3.0 DB into Sqlite Bacula v.3.0 DB
* begin: 2009.09.14
*
*/
$tables = array(
'Job',
'JobMedia',
'File',
'UnsavedFiles',
'BaseFiles',
'Media',
'Client',
'Pool',
'FileSet',
'Path',
'Filename',
'Counters',
'Version',
'CDImages',
'Device',
'Storage',
'MediaType',
// 'Status',
'Log',
'Location',
'LocationLog'
);
function my_gettime()
{
$ptime = explode(' ', microtime());
$rtime = $ptime[1] + $ptime[0];
return $rtime;
}
function my_diff_time($start_time)
{
$end_time = my_gettime();
$diff_time = number_format($end_time - $start_time, 5);
return $diff_time;
}
function my_MySQL_db_connect()
{
// http://www.php.net/manual/ru/ref.mysql.php
$dblocation = "localhost";
$dbuser = "root";
$dbpassword = "";
$dbname = "bacula";
$db = mysql_connect($dblocation, $dbuser, $dbpassword)
or die("[MySQL]: Could not connect.\n" . mysql_error());
mysql_query("SET CHARACTER SET 'utf8';", $db);
mysql_query("SET NAMES utf8;", $db);
mysql_select_db($dbname, $db)
or die("[MySQL]: Could not select database.\n" . mysql_error());
echo "Connect [MySQL] '$dbname' OK.\n";
return $db;
}
function my_Sqlite_db_connect()
{
// http://by.php.net/manual/en/book.pdo.php
$dbname = '/tmp/webacula/sqlite/bacula.db';
$db = new PDO('sqlite:'.$dbname, '', '') or die("[Sqlite] error connect.");
echo "Connect [Sqlite] '$dbname' OK.\n";
return $db;
}
function my_copy_table($table_name)
{
$dbIN = my_MySQL_db_connect();
$sql = "SELECT * FROM $table_name";
$resIN = mysql_query($sql);
if ( !$resIN ) {
echo "Could not successfully run query ($sql) from DB: " , mysql_error(), "\n";
return;
}
if ( mysql_num_rows($resIN) == 0) {
echo "No rows in $table_name found.\n";
return;
}
$dbOUT = my_Sqlite_db_connect();
$i = 0;
echo "\n";
while( $row = mysql_fetch_assoc($resIN) )
{
$columns = '';
$values = '';
//print_r($row); exit; // debug
foreach($row as $key => $value) {
//echo "$key = $value\n"; // debug
$columns = $columns . $key . ' ,';
$values .= $dbOUT->quote($value) . ' ,';
}
$columns = rtrim($columns, ',');
$values = rtrim($values, ',');
unset($row);
// http://by.php.net/manual/en/book.pdo.php
$query = "INSERT INTO $table_name ($columns) VALUES ($values)";
//echo $query, "\n";
$resOUT = $dbOUT->query($query);
if (!$resOUT) {
echo "\n\nERROR : array([0] SQLSTATE error code, [1] Driver-specific error code, [2] Driver-specific error message)\n";
print_r($dbOUT->errorInfo());
echo "\n\n";
echo "SQL QUERY : $query \n\n";
exit;
}
$resOUT->closeCursor();
unset($query);
unset($resOUT);
$i++;
//echo "."; // progress bar
}
echo "\n";
// close MySQL
if ($resIN) mysql_free_result($resIN);
if ($dbIN) mysql_close($dbIN);
// close Sqlite
unset($queryOUT);
unset($resOUT);
unset($dbOUT);
echo "******* Table : '$table_name' , insert ", $i, " row(s)\n";
}
// ************************** MAIN program
$start_time = my_gettime();
foreach ($tables as $table) {
echo "\n*******", $table, "\n";
my_copy_table($table);
}
echo "\n" , my_diff_time($start_time), "sec\n\n";
?>