<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>lunar.lu</title>
	
	<link>http://www.lunar.lu</link>
	<description>PHP &amp; MySQL Engineering</description>
	<pubDate>Tue, 06 Apr 2010 18:54:47 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/lunar-lu" /><feedburner:info uri="lunar-lu" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>TTF in GDF konvertieren</title>
		<link>http://feedproxy.google.com/~r/lunar-lu/~3/2L9jVfmHuwo/</link>
		<comments>http://www.lunar.lu/truetype-ttf-fonts-in-gdf-konvertieren/#comments</comments>
		<pubDate>Tue, 08 Sep 2009 12:06:21 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[gdf]]></category>

		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://www.lunar.lu/?p=299</guid>
		<description><![CDATA[

In letzter Zeit habe ich mich sehr viel mit der dynamischen Erstellung von Schritz&#252;gen mittels imageloadfont besch&#228;ftigt. 
Das ist an sich kein Problem, nur arbeitet imageloadfont mit Schriften im GDF Format und das Angebot solcher Schriften im Internet ist leider sehr begrenzt. TTF-Schriften hingegen findet man im Internet an jeder Ecke und ausserdem auch im [...]]]></description>
			<content:encoded><![CDATA[
<!-- google_ad_section_start -->
<p>In letzter Zeit habe ich mich sehr viel mit der dynamischen Erstellung von Schritz&#252;gen mittels <code>imageloadfont</code> besch&#228;ftigt. </p>
<p>Das ist an sich kein Problem, nur arbeitet <code>imageloadfont</code> mit Schriften im <strong>GDF</strong> Format und das Angebot solcher Schriften im Internet ist leider sehr begrenzt. <strong>TTF</strong>-Schriften hingegen findet man im Internet an jeder Ecke und ausserdem auch im Windows-Ordner Windows\Fonts\.</p>
<p><span id="more-299"></span>Ich habe mich daher drangesetzt und ein Konversionsprogramm geschrieben, mit dem man TrueType (TTF) Schriften in GDFs umwandeln kann.</p>
<p><strong>Das Tool ist ist &#252;ber folgende Adresse erreichbar:</strong> <a href="http://www.lunar.lu/ttf-to-gdf/convert.php">http://www.lunar.lu/ttf-to-gdf/convert.php</a></p>
<p>&#220;ber Feedback und Verbesserungsvorschl&#228;ge w&#252;rde ich mich freuen <img src='http://www.lunar.lu/wp-includes/images/smilies/icon_wink.gif' alt="Icon Wink in TTF in GDF konvertieren" class='wp-smiley' /> </p>
<!-- google_ad_section_end -->
<img src="http://feeds.feedburner.com/~r/lunar-lu/~4/2L9jVfmHuwo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.lunar.lu/truetype-ttf-fonts-in-gdf-konvertieren/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.lunar.lu/truetype-ttf-fonts-in-gdf-konvertieren/</feedburner:origLink></item>
		<item>
		<title>Prozess mitsamt allen Unterprozessen beenden</title>
		<link>http://feedproxy.google.com/~r/lunar-lu/~3/bmF1uSKUz4U/</link>
		<comments>http://www.lunar.lu/prozess-mitsamt-allen-unterprozessen-beenden/#comments</comments>
		<pubDate>Thu, 03 Sep 2009 09:54:34 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Shell Programmierung]]></category>

		<category><![CDATA[bash]]></category>

		<category><![CDATA[script]]></category>

		<category><![CDATA[server]]></category>

		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://www.lunar.lu/?p=284</guid>
		<description><![CDATA[

Auf einem meiner Server hat vor kurzem ein Script verr&#252;ckt gespielt, was zu einer sehr grossen Anzahl an Prozessen (mitsamt Unterprozessen) in der Warteschlange gef&#252;hrt hat. Um mir die Aufr&#228;umarbeiten etwas zu erleichtern, habe ich mir folgendes Script geschrieben.

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
#!/bin/ksh
&#160;
# L&#246;scht einen Prozess mitsamt allen Unterprozessen
# Der PID eines Prozesses muss als erster Parameter angegeben werden
&#160;
ppid=$1
&#160;
if [...]]]></description>
			<content:encoded><![CDATA[
<!-- google_ad_section_start -->
<p>Auf einem meiner Server hat vor kurzem ein Script verr&#252;ckt gespielt, was zu einer sehr grossen Anzahl an Prozessen (mitsamt Unterprozessen) in der Warteschlange gef&#252;hrt hat. Um mir die Aufr&#228;umarbeiten etwas zu erleichtern, habe ich mir folgendes Script geschrieben.</p>

<div class="wp_codebox"><table width="100%" ><tr id="p2843"><td class="line_numbers"><pre>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
</pre></td><td class="code" id="p284code3"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/ksh</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Löscht einen Prozess mitsamt allen Unterprozessen</span>
<span style="color: #666666; font-style: italic;"># Der PID eines Prozesses muss als erster Parameter angegeben werden</span>
&nbsp;
<span style="color: #007800;">ppid</span>=$<span style="color: #000000;">1</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-z</span> <span style="color: #007800;">$ppid</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> ; <span style="color: #000000; font-weight: bold;">then</span>
   <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Usage: $0 PID&quot;</span>
   <span style="color: #7a0874; font-weight: bold;">exit</span>;
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> kill_process_recursive
<span style="color: #7a0874; font-weight: bold;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">`</span>pgrep <span style="color: #660033;">-P</span> $<span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">`</span>
        <span style="color: #000000; font-weight: bold;">do</span>
                kill_process_recursive <span style="color: #007800;">$i</span>
        <span style="color: #000000; font-weight: bold;">done</span>
&nbsp;
        <span style="color: #7a0874; font-weight: bold;">echo</span> $<span style="color: #000000;">1</span>
        <span style="color: #666666; font-style: italic;">#kill $1</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
kill_process_recursive <span style="color: #007800;">$ppid</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">exit</span>;</pre></td></tr></table></div>

<p>Beispiel :</p>

<div class="wp_codebox"><table width="100%" ><tr id="p2844"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p284code4"><pre class="bash" style="font-family:monospace;">claude<span style="color: #000000; font-weight: bold;">@</span>hades <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>claude <span style="color: #666666; font-style: italic;"># sh kill_process_tree.sh 32461</span>
<span style="color: #000000;">2364</span>
<span style="color: #000000;">2363</span>
<span style="color: #000000;">24324</span>
<span style="color: #000000;">24334</span>
<span style="color: #000000;">24323</span>
<span style="color: #000000;">32461</span></pre></td></tr></table></div>

<p>Im Beispielscript habe ich <code>kill $1</code> kommentiert und durch <code>echo $1</code> ersetzt.<br />
Damit die Prozesse auch wirklich beendet werden, muss man diese &#196;nderung nat&#252;rlich umdrehen <img src='http://www.lunar.lu/wp-includes/images/smilies/icon_wink.gif' alt="Icon Wink in Prozess mitsamt allen Unterprozessen beenden" class='wp-smiley' /> </p>
<!-- google_ad_section_end -->
<img src="http://feeds.feedburner.com/~r/lunar-lu/~4/bmF1uSKUz4U" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.lunar.lu/prozess-mitsamt-allen-unterprozessen-beenden/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.lunar.lu/prozess-mitsamt-allen-unterprozessen-beenden/</feedburner:origLink></item>
		<item>
		<title>Geschwindigkeit eines PHP Scripts messen</title>
		<link>http://feedproxy.google.com/~r/lunar-lu/~3/FzZ7j6hReBg/</link>
		<comments>http://www.lunar.lu/geschwindigkeit-performance-php-script-messen/#comments</comments>
		<pubDate>Wed, 08 Jul 2009 17:49:51 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[PHP Objekt Orientiert]]></category>

		<category><![CDATA[klasse]]></category>

		<category><![CDATA[objekt orientiert]]></category>

		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.lunar.lu/?p=271</guid>
		<description><![CDATA[

Zur Zeit arbeite ich an einem objekt-orientierten Framework und habe, in diesem Zuge, eine Vielzahl von interessanten Klassen programmiert. Eine kleine Auswahl dieser Klassen werde ich in n&#228;chster Zeit in meinem Blog vorstellen.
Der Anfang macht hierbei die Klasse timer, mit welcher man die Geschwindigkeit eines PHP Scriptes messen kann. Hier ein Beispiel welches die Funktionsweise [...]]]></description>
			<content:encoded><![CDATA[
<!-- google_ad_section_start -->
<p>Zur Zeit arbeite ich an einem objekt-orientierten Framework und habe, in diesem Zuge, eine Vielzahl von interessanten Klassen programmiert. Eine kleine Auswahl dieser Klassen werde ich in n&#228;chster Zeit in meinem Blog vorstellen.</p>
<p>Der Anfang macht hierbei die Klasse <strong>timer</strong>, mit welcher man die Geschwindigkeit eines PHP Scriptes messen kann. Hier ein Beispiel welches die Funktionsweise der Klasse verdeutlichen soll:</p>

<div class="wp_codebox"><table width="100%" ><tr id="p2718"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p271code8"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$timer</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> timer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<a href="http://www.php.net/sleep"><span style="color: #990000;">sleep</span></a> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/rand"><span style="color: #990000;">rand</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<a href="http://www.php.net/echo"><span style="color: #990000;">echo</span></a> <span style="color: #0000ff;">&quot;Vergangene Zeit seit der letzten Messung : &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$timer</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">measure_intermediate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; Sekunden &lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
	<a href="http://www.php.net/echo"><span style="color: #990000;">echo</span></a> <span style="color: #0000ff;">&quot;Insgesamt vergangene Zeit: &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$timer</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">measure_elapsed</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; Sekunden&lt;br /&gt;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>	
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><span id="more-271"></span><br />
Ausgabe:</p>

<div class="wp_codebox"><table width="100%" ><tr id="p2719"><td class="code" id="p271code9"><pre class="bash" style="font-family:monospace;">Vergangene Zeit seit der letzten Messung : <span style="color: #000000;">2.001</span> Sekunden
Insgesamt vergangene Zeit: <span style="color: #000000;">2.001</span> Sekunden
&nbsp;
Vergangene Zeit seit der letzten Messung : <span style="color: #000000;">3</span> Sekunden
Insgesamt vergangene Zeit: <span style="color: #000000;">5</span> Sekunden
&nbsp;
Vergangene Zeit seit der letzten Messung : <span style="color: #000000;">3</span> Sekunden
Insgesamt vergangene Zeit: <span style="color: #000000;">8.001</span> Sekunden
&nbsp;
Vergangene Zeit seit der letzten Messung : <span style="color: #000000;">2.193</span> Sekunden
Insgesamt vergangene Zeit: <span style="color: #000000;">10.193</span> Sekunden</pre></td></tr></table></div>

<p>Die Klasser <strong>timer</strong>:</p>

<div class="wp_codebox"><table width="100%" ><tr id="p27110"><td class="line_numbers"><pre>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
</pre></td><td class="code" id="p271code10"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//Measure executing time</span>
<span style="color: #000000; font-weight: bold;">class</span> timer
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$time</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$last_time</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//Constructor</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct <span style="color: #009900;">&#40;</span><span style="color: #000088;">$start</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #666666; font-style: italic;">//Return the time</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> get_time<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">time</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>	
&nbsp;
&nbsp;
	<span style="color: #666666; font-style: italic;">//Return the last time</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> get_last_time<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">last_time</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>	
&nbsp;
&nbsp;
	<span style="color: #666666; font-style: italic;">//Start timer and set time</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> start<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">time</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">last_time</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_current_time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>		
&nbsp;
&nbsp;
	<span style="color: #666666; font-style: italic;">//Return the current time in microseconds</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> get_current_time<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//Return time</span>
		<span style="color: #000088;">$mtime</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/explode"><span style="color: #990000;">explode</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">,</span><a href="http://www.php.net/microtime"><span style="color: #990000;">microtime</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$mtime</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$mtime</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//Measure total time elapsed since last time</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> measure_intermediate <span style="color: #009900;">&#40;</span><span style="color: #000088;">$round</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$time</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/round"><span style="color: #990000;">round</span></a> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_current_time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_last_time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$round</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">last_time</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_current_time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$time</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>		
&nbsp;
&nbsp;
	<span style="color: #666666; font-style: italic;">//Measure total time </span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> measure_elapsed <span style="color: #009900;">&#40;</span><span style="color: #000088;">$round</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <a href="http://www.php.net/round"><span style="color: #990000;">round</span></a> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_current_time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$round</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>	
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Eine recht einfach gestrickte Klasse, daf&#252;r aber nicht minder n&#252;tzlich <img src='http://www.lunar.lu/wp-includes/images/smilies/icon_wink.gif' alt="Icon Wink in Geschwindigkeit eines PHP Scripts messen" class='wp-smiley' /> </p>
<!-- google_ad_section_end -->
<img src="http://feeds.feedburner.com/~r/lunar-lu/~4/FzZ7j6hReBg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.lunar.lu/geschwindigkeit-performance-php-script-messen/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.lunar.lu/geschwindigkeit-performance-php-script-messen/</feedburner:origLink></item>
		<item>
		<title>Selektives Kopieren von Tabellen</title>
		<link>http://feedproxy.google.com/~r/lunar-lu/~3/KEm6Zjmu8tY/</link>
		<comments>http://www.lunar.lu/selektives-kopieren-von-mysql-tabellen/#comments</comments>
		<pubDate>Wed, 08 Jul 2009 16:51:27 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[PHP & MySQL]]></category>

		<category><![CDATA[datenbank administration]]></category>

		<category><![CDATA[MySQL]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://www.lunar.lu/?p=259</guid>
		<description><![CDATA[

Ich habe vor einigen Tagen folgende Email eines Besuchers erhalten:
Ich m&#246;chte von db1 nach db2 ganz bestimmte einzelne Tabellen mit dem kompletten Inhalt kopieren und das per Cronjob zu bestimmten Zeiten. Dabei d&#252;rfen in db2 die bestimmten einzelnen Tabellen mit dem kompletten Inhalt &#252;berschrieben werden. [...] Dann w&#228;re es toll wenn ich im Skript [...] [...]]]></description>
			<content:encoded><![CDATA[
<!-- google_ad_section_start -->
<p>Ich habe vor einigen Tagen folgende Email eines Besuchers erhalten:</p>
<blockquote><p>Ich m&#246;chte von db1 nach db2 ganz bestimmte einzelne Tabellen mit dem kompletten Inhalt kopieren und das per Cronjob zu bestimmten Zeiten. Dabei d&#252;rfen in db2 die bestimmten einzelnen Tabellen mit dem kompletten Inhalt &#252;berschrieben werden. [...] Dann w&#228;re es toll wenn ich im Skript [...] auch die einzelnen Tabellen konfigurieren kann.</p></blockquote>
<p>Hier ein Skript welches die beschriebene Aufgabe l&#246;sen sollte <img src='http://www.lunar.lu/wp-includes/images/smilies/icon_wink.gif' alt="Icon Wink in Selektives Kopieren von Tabellen" class='wp-smiley' /> </p>
<p><span id="more-259"></span>Man kann hierbei die Tabellen einzeln angegeben &#8230;</p>

<div class="wp_codebox"><table width="100%" ><tr id="p25915"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p259code15"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$table_pattern_arr</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$table_pattern_arr</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'tabelle1'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$table_pattern_arr</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'tabelle2'</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>&#8230; oder aber Platzhalter verwenden:<br />
<!--more--></p>

<div class="wp_codebox"><table width="100%" ><tr id="p25916"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p259code16"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$table_pattern_arr</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$table_pattern_arr</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'tabelle%'</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Das vollst&#228;ndige Script:<br />
<!--more--></p>

<div class="wp_codebox"><table width="100%" ><tr id="p25917"><td class="line_numbers"><pre>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
</pre></td><td class="code" id="p259code17"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Verbindungsdaten</span>
<span style="color: #000088;">$database_server</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$database_username</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'root'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$database_password</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Datenbanken</span>
<span style="color: #000088;">$database_source</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'phpbb'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$database_target</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'test'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Zu kopierende Tabellen - Wildcards erlaubt</span>
<span style="color: #000088;">$table_pattern_arr</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$table_pattern_arr</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'phpbb_acl%'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Nicht vorhandene Tabellen ignorieren?</span>
<span style="color: #000088;">$ignore_non_existing_tables</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Mit Datenbank-Server verbinden</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$database_handle</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_connect"><span style="color: #990000;">mysql_connect</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$database_server</span><span style="color: #339933;">,</span> <span style="color: #000088;">$database_username</span><span style="color: #339933;">,</span> <span style="color: #000088;">$database_password</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<a href="http://www.php.net/die"><span style="color: #990000;">die</span></a><span style="color: #009900;">&#40;</span>get_database_error<span style="color: #009900;">&#40;</span><span style="color: #000088;">$database_handle</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Überprüfen ob Datenbanken vorhanden sind</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$database_source</span><span style="color: #339933;">,</span> <span style="color: #000088;">$database_target</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">AS</span> <span style="color: #000088;">$database</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SHOW DATABASES LIKE '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$database</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #339933;">,</span> <span style="color: #000088;">$database_handle</span><span style="color: #009900;">&#41;</span> or <a href="http://www.php.net/die"><span style="color: #990000;">die</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'['</span><span style="color: #339933;">.</span><span style="color: #000000; font-weight: bold;">__LINE__</span><span style="color: #339933;">.</span><span style="color: #0000ff;">']'</span><span style="color: #339933;">.</span>get_database_error<span style="color: #009900;">&#40;</span><span style="color: #000088;">$database_handle</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/mysql_num_rows"><span style="color: #990000;">mysql_num_rows</span></a> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<a href="http://www.php.net/die"><span style="color: #990000;">die</span></a> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Datenbank [&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$database</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;] wurde nicht gefunden&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;">//Tabellen durchgehen</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$table_pattern_arr</span> <span style="color: #b1b100;">AS</span> <span style="color: #000088;">$table_pattern</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">//Überprüfen ob die Tabelle vorhanden ist</span>
	<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SHOW TABLES IN &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$database_source</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; LIKE '&quot;</span><span style="color: #339933;">.</span><a href="http://www.php.net/addslashes"><span style="color: #990000;">addslashes</span></a> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$table_pattern</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #339933;">,</span> <span style="color: #000088;">$database_handle</span><span style="color: #009900;">&#41;</span> or <a href="http://www.php.net/die"><span style="color: #990000;">die</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'['</span><span style="color: #339933;">.</span><span style="color: #000000; font-weight: bold;">__LINE__</span><span style="color: #339933;">.</span><span style="color: #0000ff;">']'</span><span style="color: #339933;">.</span>get_database_error<span style="color: #009900;">&#40;</span><span style="color: #000088;">$database_handle</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//Keine Tabellen vorhanden</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/mysql_num_rows"><span style="color: #990000;">mysql_num_rows</span></a> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> <span style="color: #000088;">$ignore_non_existing_tables</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<a href="http://www.php.net/die"><span style="color: #990000;">die</span></a> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Keine Tabellen die dem Filter [&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$table_pattern</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;] entsprechen in der Datenbank [&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$database_source</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;]&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #666666; font-style: italic;">//Tabellen vorhanden</span>
	<span style="color: #b1b100;">else</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//Gefundene Tabellen duchgehen</span>
		<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_fetch_row"><span style="color: #990000;">mysql_fetch_row</span></a> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">//Name der Tabelle</span>
			<span style="color: #000088;">$table_name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>		
&nbsp;
		 	<span style="color: #666666; font-style: italic;">//Ziel Tabelle löschen falls vorhanden</span>
		 	<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;DROP TABLE IF EXISTS &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$database_target</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;.&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$table_name</span><span style="color: #339933;">;</span>
		 	<span style="color: #000088;">$sub_query</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #339933;">,</span> <span style="color: #000088;">$database_handle</span><span style="color: #009900;">&#41;</span> or <a href="http://www.php.net/die"><span style="color: #990000;">die</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'['</span><span style="color: #339933;">.</span><span style="color: #000000; font-weight: bold;">__LINE__</span><span style="color: #339933;">.</span><span style="color: #0000ff;">']'</span><span style="color: #339933;">.</span>get_database_error<span style="color: #009900;">&#40;</span><span style="color: #000088;">$database_handle</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">//Query zur Erstellung der Tabelle auslesen</span>
			<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SHOW CREATE TABLE &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$database_source</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;.&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$table_name</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #000088;">$sub_query</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #339933;">,</span> <span style="color: #000088;">$database_handle</span><span style="color: #009900;">&#41;</span> or <a href="http://www.php.net/die"><span style="color: #990000;">die</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'['</span><span style="color: #339933;">.</span><span style="color: #000000; font-weight: bold;">__LINE__</span><span style="color: #339933;">.</span><span style="color: #0000ff;">']'</span><span style="color: #339933;">.</span>get_database_error<span style="color: #009900;">&#40;</span><span style="color: #000088;">$database_handle</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$sub_query_data_arr</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_fetch_row"><span style="color: #990000;">mysql_fetch_row</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sub_query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
&nbsp;
			<span style="color: #666666; font-style: italic;">//Query zur Erstellung der neuen Tabelle konstruieren</span>
			<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/str_replace"><span style="color: #990000;">str_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;`&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$table_name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;`&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$database_target</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;.&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$table_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$sub_query_data_arr</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">//Tabelle in Ziel-Datenbank erstellen</span>
			<span style="color: #000088;">$sub_query</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #339933;">,</span> <span style="color: #000088;">$database_handle</span><span style="color: #009900;">&#41;</span> or <a href="http://www.php.net/die"><span style="color: #990000;">die</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'['</span><span style="color: #339933;">.</span><span style="color: #000000; font-weight: bold;">__LINE__</span><span style="color: #339933;">.</span><span style="color: #0000ff;">']'</span><span style="color: #339933;">.</span>get_database_error<span style="color: #009900;">&#40;</span><span style="color: #000088;">$database_handle</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">//Daten Einfügen</span>
			<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT INTO &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$database_target</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;.&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$table_name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; SELECT * FROM &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$database_source</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;.&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$table_name</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$sub_query</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #339933;">,</span> <span style="color: #000088;">$database_handle</span><span style="color: #009900;">&#41;</span> or <a href="http://www.php.net/die"><span style="color: #990000;">die</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'['</span><span style="color: #339933;">.</span><span style="color: #000000; font-weight: bold;">__LINE__</span><span style="color: #339933;">.</span><span style="color: #0000ff;">']'</span><span style="color: #339933;">.</span>get_database_error<span style="color: #009900;">&#40;</span><span style="color: #000088;">$database_handle</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>		
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Fertig</span>
<a href="http://www.php.net/mysql_close"><span style="color: #990000;">mysql_close</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$database_handle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;">//Funktionen</span>
<span style="color: #000000; font-weight: bold;">function</span> get_database_error <span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">return</span> <a href="http://www.php.net/mysql_errno"><span style="color: #990000;">mysql_errno</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' :: '</span><span style="color: #339933;">.</span><a href="http://www.php.net/mysql_error"><span style="color: #990000;">mysql_error</span></a> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$resource</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Hier zum Beispiel ein Cronjob um das Script st&#252;ndlich auszuf&#252;hren:</p>

<div class="wp_codebox"><table width="100%" ><tr id="p25918"><td class="code" id="p259code18"><pre class="bash" style="font-family:monospace;">00 <span style="color: #000000; font-weight: bold;">*/</span><span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> php <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>lunar<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">local</span><span style="color: #000000; font-weight: bold;">/</span>scripts<span style="color: #000000; font-weight: bold;">/</span>copy_tables.php</pre></td></tr></table></div>

<p>F&#252;r Anregungen und Fragen k&#246;nnt Ihr mir gerne einen Kommentar hinterlassen, oder eine <a href="http://www.lunar.lu/frage-an-den-experten/">Frage an den Experten</a> stellen.</p>
<!-- google_ad_section_end -->
<img src="http://feeds.feedburner.com/~r/lunar-lu/~4/KEm6Zjmu8tY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.lunar.lu/selektives-kopieren-von-mysql-tabellen/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.lunar.lu/selektives-kopieren-von-mysql-tabellen/</feedburner:origLink></item>
		<item>
		<title>Testsystem per Cronjob neu aufsetzen</title>
		<link>http://feedproxy.google.com/~r/lunar-lu/~3/vqkATNJi7lg/</link>
		<comments>http://www.lunar.lu/test-system-php-mysql-cronjob-kopie-anlegen-wiederherstellen/#comments</comments>
		<pubDate>Tue, 14 Apr 2009 11:55:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Tutorials]]></category>

		<category><![CDATA[bash]]></category>

		<category><![CDATA[MySQL]]></category>

		<category><![CDATA[mysqldump]]></category>

		<category><![CDATA[script]]></category>

		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://www.lunar.lu/?p=226</guid>
		<description><![CDATA[

Ich habe vor Ostern von einem meiner Leser folgende Frage erhalten:
Ich m&#246;chte ein CMS f&#252;r Besucher als Testsystem anbieten. Dadurch wird dieses System aber immer von den Besuchern umkonfiguriert und hin und wieder unbrauchbar gemacht. 
Bis jetzt habe ich immer einen Dump der Datenbank gemacht und von Hand t&#228;glich zur&#252;ck geschrieben. Ebenso auch eine Kopie [...]]]></description>
			<content:encoded><![CDATA[
<!-- google_ad_section_start -->
<p>Ich habe vor Ostern von einem meiner Leser folgende Frage erhalten:</p>
<p><em>Ich m&#246;chte ein CMS f&#252;r Besucher als Testsystem anbieten. Dadurch wird dieses System aber immer von den Besuchern umkonfiguriert und hin und wieder unbrauchbar gemacht. </p>
<p>Bis jetzt habe ich immer einen Dump der Datenbank gemacht und von Hand t&#228;glich zur&#252;ck geschrieben. Ebenso auch eine Kopie von den dazu geh&#246;renden Template-Files. K&#246;nnte man dies nicht &#252;ber ein Script automatisieren?</em></p>
<p>Ja, diesen Prozess kann man automatisieren, ich habe dieses Problem wie folgt gel&#246;st:</p>
<p><strong>1. Das Script</strong></p>
<p>&#220;ber dieses Script werden sowohl die Dateien als auch die Datenbank des Testsystems gesichert und bei Bedarf wiederhergestellt. Ich habe das Script an meinem Blog getestet, die Benutzerdaten sowie die Ordner m&#252;sst Ihr nat&#252;rlich an Eure eigene Umgebung anpassen. Weitere Erkl&#228;rungen befinden sich unterhalb des Scriptes <img src='http://www.lunar.lu/wp-includes/images/smilies/icon_wink.gif' alt="Icon Wink in Testsystem per Cronjob neu aufsetzen" class='wp-smiley' /> </p>
<p><span id="more-226"></span></p>

<div class="wp_codebox"><table width="100%" ><tr id="p22627"><td class="line_numbers"><pre>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
</pre></td><td class="code" id="p226code27"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#Zugangsdaten der Datenbank</span>
mysql_user<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;BENUTZERNAME&quot;</span>
mysql_pass<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;PASSWORT&quot;</span>
mysql_host<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;HOST&quot;</span>
mysql_database<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;DATENBANK&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#Ordner der Webseite, von der das Backup angelegt wird</span>
original_folder<span style="color: #339933;">=/</span><span style="color: #000000; font-weight: bold;">var</span><span style="color: #339933;">/</span>www<span style="color: #339933;">/</span>lunar<span style="color: #339933;">/</span><span style="color: #000000; font-weight: bold;">public</span><span style="color: #339933;">/</span>htdocs
&nbsp;
<span style="color: #666666; font-style: italic;">#Log Ordner</span>
backup_log_folder<span style="color: #339933;">=/</span><span style="color: #000000; font-weight: bold;">var</span><span style="color: #339933;">/</span>www<span style="color: #339933;">/</span>lunar<span style="color: #339933;">/</span>local<span style="color: #339933;">/</span>scripts<span style="color: #339933;">/</span>website_backup<span style="color: #339933;">/</span>logs
&nbsp;
<span style="color: #666666; font-style: italic;">#Log Dateien</span>
backup_log_info<span style="color: #339933;">=</span><span style="color: #000088;">$backup_log_folder</span><span style="color: #339933;">/</span>backup<span style="color: #339933;">.</span><a href="http://www.php.net/log"><span style="color: #990000;">log</span></a>
backup_log_error<span style="color: #339933;">=</span><span style="color: #000088;">$backup_lof_folder</span><span style="color: #339933;">/</span>backup<span style="color: #339933;">.</span>err
backup_log_date_format<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">%d</span>.%m.%Y %R&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#Backup Ordner</span>
backup_file_folder<span style="color: #339933;">=/</span><span style="color: #000000; font-weight: bold;">var</span><span style="color: #339933;">/</span>www<span style="color: #339933;">/</span>lunar<span style="color: #339933;">/</span>local<span style="color: #339933;">/</span>scripts<span style="color: #339933;">/</span>website_backup<span style="color: #339933;">/</span>backup
&nbsp;
<span style="color: #666666; font-style: italic;">#Backup Dateien</span>
backup_file_system<span style="color: #339933;">=</span><span style="color: #000088;">$backup_file_folder</span><span style="color: #339933;">/</span>backup_system<span style="color: #339933;">.</span>tar<span style="color: #339933;">.</span>gz
backup_file_database<span style="color: #339933;">=</span><span style="color: #000088;">$backup_file_folder</span><span style="color: #339933;">/</span>backup_database<span style="color: #339933;">.</span>sql<span style="color: #339933;">.</span>gz
&nbsp;
<span style="color: #666666; font-style: italic;">#Vergangene Zeit berechnen</span>
backup_start_time<span style="color: #339933;">=</span>`<a href="http://www.php.net/date"><span style="color: #990000;">date</span></a> <span style="color: #339933;">+%</span>s`
&nbsp;
<span style="color: #666666; font-style: italic;">#Sicherheitskopie erstellen</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#91;</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$1</span>&quot;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;create&quot;</span> <span style="color: #009900;">&#93;</span>
then
        <span style="color: #666666; font-style: italic;">#Backup Ordner erstellen</span>
        <a href="http://www.php.net/mkdir"><span style="color: #990000;">mkdir</span></a> <span style="color: #339933;">-</span>p <span style="color: #000088;">$backup_file_folder</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">#Backup Ordner überprüfen</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#91;</span> <span style="color: #339933;">!</span> <span style="color: #339933;">-</span>d <span style="color: #000088;">$backup_file_folder</span> <span style="color: #009900;">&#93;</span>
        then
                <a href="http://www.php.net/echo"><span style="color: #990000;">echo</span></a> <span style="color: #0000ff;">&quot;[`date +&quot;</span><span style="color: #000088;">$backup_log_date_format</span><span style="color: #0000ff;">&quot;`] Invalid directory <span style="color: #006699; font-weight: bold;">$backup_file_folder</span>, aborting&quot;</span> <span style="color: #339933;">|</span> tee <span style="color: #339933;">-</span>a <span style="color: #000088;">$backup_log_error</span>
                <a href="http://www.php.net/exit"><span style="color: #990000;">exit</span></a> <span style="color: #cc66cc;">1</span>
        fi
&nbsp;
        <span style="color: #666666; font-style: italic;">#Backup der Dateien anlegen</span>
        <span style="color: #666666; font-style: italic;">#Hierbei wechselt tar zuerst in den Wurzelordner und erstellt dann das Backup</span>
        <span style="color: #666666; font-style: italic;">#Damit dies funktioniert muss der erste Slash des Quellordners entfernt werden </span>
        <span style="color: #666666; font-style: italic;">#Diese Vorgehensweise verhindert den Hinweis &quot;tar: Removing leading `/' from member names&quot;</span>
        tar <span style="color: #339933;">-</span>czf <span style="color: #000088;">$backup_file_system</span> <span style="color: #339933;">-</span>C <span style="color: #339933;">/</span> <span style="color: #0000ff;">&quot;${original_folder#&quot;</span>$<span style="color: #009900;">&#123;</span>original_folder<span style="color: #339933;">%%</span><span style="color: #009900;">&#91;</span><span style="color: #339933;">!/</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#125;</span><span style="color: #0000ff;">&quot;}&quot;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">#Backup der Datenbank anlegen</span>
        mysqldump <span style="color: #339933;">--</span>opt <span style="color: #339933;">--</span>add<span style="color: #339933;">-</span>drop<span style="color: #339933;">-</span>database  <span style="color: #339933;">--</span>password<span style="color: #339933;">=</span><span style="color: #000088;">$mysql_pass</span> <span style="color: #339933;">--</span>user<span style="color: #339933;">=</span><span style="color: #000088;">$mysql_user</span> <span style="color: #339933;">--</span>host<span style="color: #339933;">=</span><span style="color: #000088;">$mysql_host</span> <span style="color: #339933;">--</span>databases <span style="color: #000088;">$mysql_database</span> <span style="color: #339933;">|</span> gzip <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$backup_file_database</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">#Falls der Fehler &quot;Access denied...when using LOCK TABLES&quot; auftreten sollte, dann hat der </span>
        <span style="color: #666666; font-style: italic;">#MySQL-Benutzer mit dem man arbeitet nicht die benötigten Benutzerrechte um Tabellen zu sperren.</span>
        <span style="color: #666666; font-style: italic;">#Hier kann man den Parameter --lock-tables=false (hinter --opt anhängen) verwenden falls man keinen</span>
        <span style="color: #666666; font-style: italic;">#Zugriff auf die Benutzerrechte hat</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">#Loggen</span>
        <a href="http://www.php.net/echo"><span style="color: #990000;">echo</span></a> <span style="color: #0000ff;">&quot;[`date +&quot;</span><span style="color: #000088;">$backup_log_date_format</span><span style="color: #0000ff;">&quot;`] Backup of folder [<span style="color: #006699; font-weight: bold;">$original_folder</span>] and database [<span style="color: #006699; font-weight: bold;">$mysql_database</span>] created&quot;</span> <span style="color: #339933;">|</span> tee <span style="color: #339933;">-</span>a <span style="color: #000088;">$backup_log_info</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#Webseite wiederherstellen</span>
elif <span style="color: #009900;">&#91;</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$1</span>&quot;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;restore&quot;</span> <span style="color: #009900;">&#93;</span>
then
        <span style="color: #666666; font-style: italic;">#Backup Dateien überprüfen</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#91;</span> <span style="color: #339933;">!</span> <span style="color: #339933;">-</span>f <span style="color: #000088;">$backup_file_system</span> <span style="color: #009900;">&#93;</span>
        then
                <a href="http://www.php.net/echo"><span style="color: #990000;">echo</span></a> <span style="color: #0000ff;">&quot;[`date +&quot;</span><span style="color: #000088;">$backup_log_date_format</span><span style="color: #0000ff;">&quot;`] System backup file <span style="color: #006699; font-weight: bold;">$backup_file_system</span> not found, aborting&quot;</span> <span style="color: #339933;">|</span> tee <span style="color: #339933;">-</span>a <span style="color: #000088;">$backup_log_error</span>
                <a href="http://www.php.net/exit"><span style="color: #990000;">exit</span></a> <span style="color: #cc66cc;">1</span>
        fi
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#91;</span> <span style="color: #339933;">!</span> <span style="color: #339933;">-</span>f <span style="color: #000088;">$backup_file_database</span> <span style="color: #009900;">&#93;</span>
        then
                <a href="http://www.php.net/echo"><span style="color: #990000;">echo</span></a> <span style="color: #0000ff;">&quot;[`date +&quot;</span><span style="color: #000088;">$backup_log_date_format</span><span style="color: #0000ff;">&quot;`] Database backup file <span style="color: #006699; font-weight: bold;">$backup_file_database</span> not found, aborting&quot;</span> <span style="color: #339933;">|</span> tee <span style="color: #339933;">-</span>a <span style="color: #000088;">$backup_log_error</span>
                <a href="http://www.php.net/exit"><span style="color: #990000;">exit</span></a> <span style="color: #cc66cc;">1</span>
        fi
&nbsp;
        <span style="color: #666666; font-style: italic;">#Dateien wiederherstellen</span>
        tar <span style="color: #339933;">--</span>recursive<span style="color: #339933;">-</span><a href="http://www.php.net/unlink"><span style="color: #990000;">unlink</span></a> <span style="color: #339933;">-</span>xzf <span style="color: #000088;">$backup_file_system</span> <span style="color: #339933;">-</span>C <span style="color: #339933;">/</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">#Datenbank wiederherstellen</span>
        gunzip <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$backup_file_database</span> <span style="color: #339933;">|</span> <a href="http://www.php.net/mysql"><span style="color: #990000;">mysql</span></a> <span style="color: #339933;">--</span>password<span style="color: #339933;">=</span><span style="color: #000088;">$mysql_pass</span> <span style="color: #339933;">--</span>user<span style="color: #339933;">=</span><span style="color: #000088;">$mysql_user</span> <span style="color: #339933;">--</span>host<span style="color: #339933;">=</span><span style="color: #000088;">$mysql_host</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">#Loggen</span>
        <a href="http://www.php.net/echo"><span style="color: #990000;">echo</span></a> <span style="color: #0000ff;">&quot;[`date +&quot;</span><span style="color: #000088;">$backup_log_date_format</span><span style="color: #0000ff;">&quot;`] Backup of folder [<span style="color: #006699; font-weight: bold;">$original_folder</span>] and database [<span style="color: #006699; font-weight: bold;">$mysql_database</span>] restored&quot;</span> <span style="color: #339933;">|</span> tee <span style="color: #339933;">-</span>a <span style="color: #000088;">$backup_log_info</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#Falscher Parameter</span>
<span style="color: #b1b100;">else</span>
        <a href="http://www.php.net/echo"><span style="color: #990000;">echo</span></a> <span style="color: #0000ff;">&quot;Aufruf wie folgt:&quot;</span>
        <a href="http://www.php.net/echo"><span style="color: #990000;">echo</span></a> <span style="color: #0000ff;">&quot; <span style="color: #006699; font-weight: bold;">$0</span> create   : Sicherheitskopie der Webseite anlegen&quot;</span>
        <a href="http://www.php.net/echo"><span style="color: #990000;">echo</span></a> <span style="color: #0000ff;">&quot; <span style="color: #006699; font-weight: bold;">$0</span> restore  : Webseite anhand der Sicherheitskopie wiederherstellen&quot;</span>
        <a href="http://www.php.net/exit"><span style="color: #990000;">exit</span></a> <span style="color: #cc66cc;">1</span>
fi
&nbsp;
<span style="color: #666666; font-style: italic;">#Vergangene Zeit berechnen</span>
backup_end_time<span style="color: #339933;">=</span>`<a href="http://www.php.net/date"><span style="color: #990000;">date</span></a> <span style="color: #339933;">+%</span>s`
backup_elapsed_time<span style="color: #339933;">=</span>`expr <span style="color: #000088;">$backup_end_time</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$backup_start_time</span>`
<a href="http://www.php.net/echo"><span style="color: #990000;">echo</span></a> <span style="color: #339933;">-</span>e <span style="color: #0000ff;">&quot;[`date +&quot;</span><span style="color: #000088;">$backup_log_date_format</span><span style="color: #0000ff;">&quot;`] Elapsed time &quot;</span>`<a href="http://www.php.net/date"><span style="color: #990000;">date</span></a> <span style="color: #339933;">-</span>d <span style="color: #0000ff;">&quot;1970-01-01 <span style="color: #006699; font-weight: bold;">$backup_elapsed_time</span> +sec&quot;</span> <span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;%H:%M:%S&quot;</span>`<span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #339933;">|</span> tee <span style="color: #339933;">-</span>a <span style="color: #000088;">$backup_log_info</span></pre></td></tr></table></div>

<p>Um das Script zu testen habe ich die folgenden Ordner angelegt:</p>

<div class="wp_codebox"><table width="100%" ><tr id="p22628"><td class="code" id="p226code28"><pre class="text" style="font-family:monospace;">/var/www/lunar/local/scripts/website_backup/
/var/www/lunar/local/scripts/website_backup/backup/
/var/www/lunar/local/scripts/website_backup/logs/</pre></td></tr></table></div>

<p>Das Script habe ich hier abgespeichert:</p>

<div class="wp_codebox"><table width="100%" ><tr id="p22629"><td class="code" id="p226code29"><pre class="text" style="font-family:monospace;">/var/www/lunar/local/scripts/website_backup/website_backup.sh</pre></td></tr></table></div>

<p><strong>2. Das Testsystem abspeichern</strong></p>
<p>Um das Testsystem abzuspeichern muss man das Script mit dem Parameter <code>create</code> aufrufen:</p>

<div class="wp_codebox"><table width="100%" ><tr id="p22630"><td class="code" id="p226code30"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>lunar<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">local</span><span style="color: #000000; font-weight: bold;">/</span>scripts<span style="color: #000000; font-weight: bold;">/</span>website_backup <span style="color: #666666; font-style: italic;"># sh website_backup.sh create</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>14.04.2009 <span style="color: #000000;">13</span>:<span style="color: #000000;">20</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> Backup of folder <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>lunar<span style="color: #000000; font-weight: bold;">/</span>public<span style="color: #000000; font-weight: bold;">/</span>htdocs<span style="color: #7a0874; font-weight: bold;">&#93;</span> and database <span style="color: #7a0874; font-weight: bold;">&#91;</span>lunar<span style="color: #7a0874; font-weight: bold;">&#93;</span> created
<span style="color: #7a0874; font-weight: bold;">&#91;</span>14.04.2009 <span style="color: #000000;">13</span>:<span style="color: #000000;">20</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> Elapsed <span style="color: #000000; font-weight: bold;">time</span> 00:00:01</pre></td></tr></table></div>

<p>Hierbei wurden die folgenden Dateien angelegt:</p>

<div class="wp_codebox"><table width="100%" ><tr id="p22631"><td class="code" id="p226code31"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>lunar<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">local</span><span style="color: #000000; font-weight: bold;">/</span>scripts<span style="color: #000000; font-weight: bold;">/</span>website_backup <span style="color: #666666; font-style: italic;"># ll backup/</span>
total 3.0M
<span style="color: #000000;">2009</span>-04-<span style="color: #000000;">14</span> <span style="color: #000000;">13</span>:<span style="color: #000000;">20</span> backup_database.sql.gz
<span style="color: #000000;">2009</span>-04-<span style="color: #000000;">14</span> <span style="color: #000000;">13</span>:<span style="color: #000000;">20</span> backup_system.tar.gz</pre></td></tr></table></div>

<p>Noch ein kurzer Blick auf die Logs:</p>

<div class="wp_codebox"><table width="100%" ><tr id="p22632"><td class="code" id="p226code32"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>lunar<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">local</span><span style="color: #000000; font-weight: bold;">/</span>scripts<span style="color: #000000; font-weight: bold;">/</span>website_backup <span style="color: #666666; font-style: italic;"># tail logs/backup.log </span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>14.04.2009 <span style="color: #000000;">13</span>:<span style="color: #000000;">20</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> Backup of folder <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>lunar<span style="color: #000000; font-weight: bold;">/</span>public<span style="color: #000000; font-weight: bold;">/</span>htdocs<span style="color: #7a0874; font-weight: bold;">&#93;</span> and database <span style="color: #7a0874; font-weight: bold;">&#91;</span>lunar<span style="color: #7a0874; font-weight: bold;">&#93;</span> created
<span style="color: #7a0874; font-weight: bold;">&#91;</span>14.04.2009 <span style="color: #000000;">13</span>:<span style="color: #000000;">20</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> Elapsed <span style="color: #000000; font-weight: bold;">time</span> 00:00:01</pre></td></tr></table></div>

<p><strong>3. Das Testsystem wiederherstellen</strong></p>
<p>Um das Testsystem wiederherzustellen muss man das Script mit dem Parameter <code>restore</code> aufrufen. Dies funktioniert nat&#252;rlich nur dann, wenn bereits ein Backup vorhanden ist. Die Datenbank und Dateien des Testsystems werden hierbei komplett durch die gesicherten Versionen ersetzt.</p>

<div class="wp_codebox"><table width="100%" ><tr id="p22633"><td class="code" id="p226code33"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>lunar<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">local</span><span style="color: #000000; font-weight: bold;">/</span>scripts<span style="color: #000000; font-weight: bold;">/</span>website_backup <span style="color: #666666; font-style: italic;"># sh website_backup.sh restore</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>14.04.2009 <span style="color: #000000;">13</span>:<span style="color: #000000;">31</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> Backup of folder <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>lunar<span style="color: #000000; font-weight: bold;">/</span>public<span style="color: #000000; font-weight: bold;">/</span>htdocs<span style="color: #7a0874; font-weight: bold;">&#93;</span> and database <span style="color: #7a0874; font-weight: bold;">&#91;</span>lunar<span style="color: #7a0874; font-weight: bold;">&#93;</span> restored
<span style="color: #7a0874; font-weight: bold;">&#91;</span>14.04.2009 <span style="color: #000000;">13</span>:<span style="color: #000000;">31</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> Elapsed <span style="color: #000000; font-weight: bold;">time</span> 00:00:01</pre></td></tr></table></div>

<p><strong>4. Die Wiederherstellung automatisieren</strong></p>
<p>Um die Wiederherstellung zu automatisieren bietet es sich an, zun&#228;chst eine Sicherheitskopie anzulegen (<em>Siehe Punkt 2</em>), und diese dann per Cronjob regelm&#228;ssig neu aufzuspielen.</p>
<p>Hier zum Beispiel ein Cronjob, welcher das Testsystem st&#252;ndlich wiederherstellen w&#252;rde:</p>

<div class="wp_codebox"><table width="100%" ><tr id="p22634"><td class="code" id="p226code34"><pre class="bash" style="font-family:monospace;">00 <span style="color: #000000; font-weight: bold;">*/</span><span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #c20cb9; font-weight: bold;">sh</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>lunar<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">local</span><span style="color: #000000; font-weight: bold;">/</span>scripts<span style="color: #000000; font-weight: bold;">/</span>website_backup<span style="color: #000000; font-weight: bold;">/</span>website_backup.sh</pre></td></tr></table></div>

<p>F&#252;r Anregungen und Fragen k&#246;nnt Ihr mir gerne einen Kommentar hinterlassen, oder eine <a href="http://www.lunar.lu/frage-an-den-experten/">Frage an den Experten</a> stellen.</p>
<!-- google_ad_section_end -->
<img src="http://feeds.feedburner.com/~r/lunar-lu/~4/vqkATNJi7lg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.lunar.lu/test-system-php-mysql-cronjob-kopie-anlegen-wiederherstellen/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.lunar.lu/test-system-php-mysql-cronjob-kopie-anlegen-wiederherstellen/</feedburner:origLink></item>
		<item>
		<title>MySQL - Datenbank Backup Script</title>
		<link>http://feedproxy.google.com/~r/lunar-lu/~3/wTYh0s8JQxE/</link>
		<comments>http://www.lunar.lu/mysql-datenbank-backup-script/#comments</comments>
		<pubDate>Wed, 08 Apr 2009 09:36:31 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[MySQL]]></category>

		<category><![CDATA[bash]]></category>

		<category><![CDATA[datenbank administration]]></category>

		<category><![CDATA[script]]></category>

		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://www.lunar.lu/?p=205</guid>
		<description><![CDATA[

In einem meiner vorigen Beitr&#228;ge habe ich beschrieben, wie man MySQL-Datenbanken mit mysqldump kopieren kann.
In Verbindung mit einem Cronjob eignet sich mysqldump aber auch hervorragend dazu, Sicherheitskopien von Datenbanken anzulegen. Das folgende Bash Script nutze ich, um t&#228;glich ein Backup aller MySQL-Datenbanken eines Servers anzulegen&#8230;


#!/bin/sh
&#160;
#Zugangsdaten
mysql_user=&#34;MYSQL_BENUZERNAME&#34;
mysql_pass=&#34;MYSQL_PASSWORT&#34;
mysql_host=&#34;localhost&#34;
&#160;
#Log Dateien
backup_log_info=/var/log/mysql/mysql-backup.log
backup_log_error=/var/log/mysql/mysql-backup.err
backup_log_date_format=&#34;%d.%m.%Y %R&#34;;
&#160;
#Vergangene Zeit berechnen
backup_start_time=`date +%s`
&#160;
#Backup Wurzel Ordner
backup_root_dir=/var/mysql-backup
&#160;
#Backup Tages Ordner
backup_day_dir=`date +%Y-%m-%d`
&#160;
#Backups, [...]]]></description>
			<content:encoded><![CDATA[
<!-- google_ad_section_start -->
<p>In einem meiner vorigen Beitr&#228;ge habe ich beschrieben, wie man <a href="http://www.lunar.lu/mysql-datenbanken-mysqldump-anderen-server-kopieren/">MySQL-Datenbanken mit mysqldump kopieren</a> kann.</p>
<p>In Verbindung mit einem Cronjob eignet sich <code>mysqldump</code> aber auch hervorragend dazu, Sicherheitskopien von Datenbanken anzulegen. Das folgende Bash Script nutze ich, um t&#228;glich ein Backup aller MySQL-Datenbanken eines Servers anzulegen&#8230;</p>
<p><span id="more-205"></span></p>

<div class="wp_codebox"><table width="100%" ><tr id="p20540"><td class="code" id="p205code40"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#Zugangsdaten</span>
<span style="color: #007800;">mysql_user</span>=<span style="color: #ff0000;">&quot;MYSQL_BENUZERNAME&quot;</span>
<span style="color: #007800;">mysql_pass</span>=<span style="color: #ff0000;">&quot;MYSQL_PASSWORT&quot;</span>
<span style="color: #007800;">mysql_host</span>=<span style="color: #ff0000;">&quot;localhost&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#Log Dateien</span>
<span style="color: #007800;">backup_log_info</span>=<span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>log<span style="color: #000000; font-weight: bold;">/</span>mysql<span style="color: #000000; font-weight: bold;">/</span>mysql-backup.log
<span style="color: #007800;">backup_log_error</span>=<span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>log<span style="color: #000000; font-weight: bold;">/</span>mysql<span style="color: #000000; font-weight: bold;">/</span>mysql-backup.err
<span style="color: #007800;">backup_log_date_format</span>=<span style="color: #ff0000;">&quot;%d.%m.%Y %R&quot;</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">#Vergangene Zeit berechnen</span>
<span style="color: #007800;">backup_start_time</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">date</span> +<span style="color: #000000; font-weight: bold;">%</span>s<span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#Backup Wurzel Ordner</span>
<span style="color: #007800;">backup_root_dir</span>=<span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>mysql-backup
&nbsp;
<span style="color: #666666; font-style: italic;">#Backup Tages Ordner</span>
<span style="color: #007800;">backup_day_dir</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">date</span> +<span style="color: #000000; font-weight: bold;">%</span>Y-<span style="color: #000000; font-weight: bold;">%</span>m-<span style="color: #000000; font-weight: bold;">%</span>d<span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#Backups, die älter als X Tage sind, löschen</span>
<span style="color: #007800;">backup_keep_days</span>=<span style="color: #000000;">10</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#Verschiedene Ordner, bei mehreren Backups an ein und dem selben Tag</span>
<span style="color: #007800;">i</span>=<span style="color: #000000;">1</span>;
<span style="color: #007800;">backup_version_dir</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">printf</span> <span style="color: #ff0000;">&quot;%03d&quot;</span> <span style="color: #007800;">$i</span><span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-d</span> <span style="color: #007800;">$backup_root_dir</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$backup_day_dir</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$backup_version_dir</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
        <span style="color: #000000; font-weight: bold;">do</span>
                <span style="color: #007800;">i</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">expr</span> <span style="color: #007800;">$i</span> + <span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">`</span>
                <span style="color: #007800;">backup_version_dir</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">printf</span> <span style="color: #ff0000;">&quot;%03d&quot;</span> <span style="color: #007800;">$i</span><span style="color: #000000; font-weight: bold;">`</span>
        <span style="color: #000000; font-weight: bold;">done</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#In diesem Ordner wird das Backup gespeichert</span>
<span style="color: #007800;">backup_dir</span>=<span style="color: #007800;">$backup_root_dir</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$backup_day_dir</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$backup_version_dir</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#Loggen</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;[<span style="color: #780078;">`date +&quot;$backup_log_date_format&quot;`</span>] Creating backup&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">tee</span> <span style="color: #660033;">-a</span> <span style="color: #007800;">$backup_log_info</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;[<span style="color: #780078;">`date +&quot;$backup_log_date_format&quot;`</span>] Date: <span style="color: #007800;">$backup_day_dir</span>, iteration <span style="color: #007800;">$backup_version_dir</span>&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">tee</span> <span style="color: #660033;">-a</span> <span style="color: #007800;">$backup_log_info</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;[<span style="color: #780078;">`date +&quot;$backup_log_date_format&quot;`</span>] Backup to: <span style="color: #007800;">$backup_dir</span>&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">tee</span> <span style="color: #660033;">-a</span> <span style="color: #007800;">$backup_log_info</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#Backup Ordner erstellen</span>
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #660033;">-p</span> <span style="color: #007800;">$backup_dir</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#Backup Ordner überprüfen</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #660033;">-d</span> <span style="color: #007800;">$backup_dir</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
        <span style="color: #000000; font-weight: bold;">then</span>
                <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;[<span style="color: #780078;">`date +&quot;$backup_log_date_format&quot;`</span>] Invalid directory <span style="color: #007800;">$backup_dir</span>, aborting&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">tee</span> <span style="color: #660033;">-a</span> <span style="color: #007800;">$backup_log_error</span>
                <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
        <span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#Alte Ordner löschen</span>
<span style="color: #c20cb9; font-weight: bold;">find</span>  <span style="color: #007800;">$backup_root_dir</span> <span style="color: #660033;">-maxdepth</span> <span style="color: #000000;">1</span> <span style="color: #660033;">-mtime</span> +<span style="color: #007800;">$backup_keep_days</span> <span style="color: #660033;">-type</span> d <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">xargs</span> <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-rf</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#Namen der Datenbank auslesen</span>
<span style="color: #000000; font-weight: bold;">for</span> database <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">`</span>mysql <span style="color: #660033;">-s</span> -u<span style="color: #007800;">$mysql_user</span> -p<span style="color: #007800;">$mysql_pass</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;SELECT SCHEMA_NAME FROM information_schema.SCHEMATA WHERE SCHEMA_NAME NOT IN ('mysql','information_schema')&quot;</span><span style="color: #000000; font-weight: bold;">`</span>;
        <span style="color: #000000; font-weight: bold;">do</span>
                <span style="color: #666666; font-style: italic;">#Log </span>
                <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;[<span style="color: #780078;">`date +&quot;$backup_log_date_format&quot;`</span>] Backing up <span style="color: #007800;">$database</span>&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">tee</span> <span style="color: #660033;">-a</span> <span style="color: #007800;">$backup_log_info</span>
&nbsp;
				<span style="color: #666666; font-style: italic;">#Sichern</span>
                mysqldump <span style="color: #660033;">--opt</span> <span style="color: #660033;">--password</span>=<span style="color: #007800;">$mysql_pass</span> <span style="color: #660033;">--user</span>=<span style="color: #007800;">$mysql_user</span> <span style="color: #660033;">--host</span>=<span style="color: #007800;">$mysql_host</span> <span style="color: #007800;">$database</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">gzip</span>  <span style="color: #660033;">--rsyncable</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #007800;">$backup_dir</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$database</span>.sql.gz
        <span style="color: #000000; font-weight: bold;">done</span>
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;">#Vergangene Zeit berechnen</span>
<span style="color: #007800;">backup_end_time</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">date</span> +<span style="color: #000000; font-weight: bold;">%</span>s<span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #007800;">backup_elapsed_time</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">expr</span> <span style="color: #007800;">$backup_end_time</span> - <span style="color: #007800;">$backup_start_time</span><span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;[<span style="color: #780078;">`date +&quot;$backup_log_date_format&quot;`</span>] Elapsed time &quot;</span><span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">date</span> <span style="color: #660033;">-d</span> <span style="color: #ff0000;">&quot;1970-01-01 <span style="color: #007800;">$backup_elapsed_time</span> +sec&quot;</span> +<span style="color: #ff0000;">&quot;%H:%M:%S&quot;</span><span style="color: #000000; font-weight: bold;">`</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">tee</span> <span style="color: #660033;">-a</span> <span style="color: #007800;">$backup_log_info</span></pre></td></tr></table></div>

<p>Die Ordner werden von diesem Script automatisch angelegt, und stellen sich wie folgt dar:</p>

<div class="wp_codebox"><table width="100%" ><tr id="p20541"><td class="code" id="p205code41"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>mysql-backup <span style="color: #666666; font-style: italic;"># ls -al</span>
total 56K
drwxr-xr-x  <span style="color: #000000;">3</span> root root 4.0K <span style="color: #000000;">2009</span>-03-<span style="color: #000000;">30</span> 03:<span style="color: #000000;">33</span> <span style="color: #000000;">2009</span>-03-<span style="color: #000000;">30</span>
drwxr-xr-x  <span style="color: #000000;">3</span> root root 4.0K <span style="color: #000000;">2009</span>-03-<span style="color: #000000;">31</span> 03:<span style="color: #000000;">33</span> <span style="color: #000000;">2009</span>-03-<span style="color: #000000;">31</span>
drwxr-xr-x  <span style="color: #000000;">3</span> root root 4.0K <span style="color: #000000;">2009</span>-04-01 03:<span style="color: #000000;">33</span> <span style="color: #000000;">2009</span>-04-01
drwxr-xr-x  <span style="color: #000000;">3</span> root root 4.0K <span style="color: #000000;">2009</span>-04-02 03:<span style="color: #000000;">33</span> <span style="color: #000000;">2009</span>-04-02
drwxr-xr-x  <span style="color: #000000;">3</span> root root 4.0K <span style="color: #000000;">2009</span>-04-03 03:<span style="color: #000000;">33</span> <span style="color: #000000;">2009</span>-04-03
drwxr-xr-x  <span style="color: #000000;">3</span> root root 4.0K <span style="color: #000000;">2009</span>-04-04 03:<span style="color: #000000;">33</span> <span style="color: #000000;">2009</span>-04-04
drwxr-xr-x  <span style="color: #000000;">3</span> root root 4.0K <span style="color: #000000;">2009</span>-04-05 03:<span style="color: #000000;">33</span> <span style="color: #000000;">2009</span>-04-05
drwxr-xr-x  <span style="color: #000000;">3</span> root root 4.0K <span style="color: #000000;">2009</span>-04-06 03:<span style="color: #000000;">33</span> <span style="color: #000000;">2009</span>-04-06
drwxr-xr-x  <span style="color: #000000;">3</span> root root 4.0K <span style="color: #000000;">2009</span>-04-07 03:<span style="color: #000000;">33</span> <span style="color: #000000;">2009</span>-04-07
drwxr-xr-x  <span style="color: #000000;">4</span> root root 4.0K <span style="color: #000000;">2009</span>-04-08 03:<span style="color: #000000;">33</span> <span style="color: #000000;">2009</span>-04-08</pre></td></tr></table></div>

<p>Tagt&#228;glich wird ein neuer Ordner hinzugef&#252;gt, und die Ordner, die &#228;lter als <code>backup_keep_days</code> Tage sind, gel&#246;scht. Bei mehreren Backups an ein und dem gleichen Tag, werden verschiedene Unterverzeichnisse angelegt. Am 8. April wurden zum Beispiel zwei Backups gefahren:</p>

<div class="wp_codebox"><table width="100%" ><tr id="p20542"><td class="code" id="p205code42"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>mysql-backup<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">2009</span>-04-08 <span style="color: #666666; font-style: italic;"># ls -al</span>
total 140K
drwxr-xr-x <span style="color: #000000;">2</span> root root 136K <span style="color: #000000;">2009</span>-04-08 <span style="color: #000000;">11</span>:07 001
drwxr-xr-x <span style="color: #000000;">2</span> root root 136K <span style="color: #000000;">2009</span>-04-08 <span style="color: #000000;">11</span>:<span style="color: #000000;">53</span> 002</pre></td></tr></table></div>

<p>Die Logs sehen wie folgt aus:</p>

<div class="wp_codebox"><table width="100%" ><tr id="p20543"><td class="code" id="p205code43"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>31.03.2009 03:<span style="color: #000000;">33</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> Creating backup
<span style="color: #7a0874; font-weight: bold;">&#91;</span>31.03.2009 03:<span style="color: #000000;">33</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> Date: <span style="color: #000000;">2009</span>-03-<span style="color: #000000;">31</span>, iteration 001
<span style="color: #7a0874; font-weight: bold;">&#91;</span>31.03.2009 03:<span style="color: #000000;">33</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> Backup to: <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>mysql-backup<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">2009</span>-03-<span style="color: #000000;">31</span><span style="color: #000000; font-weight: bold;">/</span>001
<span style="color: #7a0874; font-weight: bold;">&#91;</span>30.03.2009 03:<span style="color: #000000;">33</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> Backing up lunar
<span style="color: #7a0874; font-weight: bold;">&#91;</span>30.03.2009 03:<span style="color: #000000;">33</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> Elapsed <span style="color: #000000; font-weight: bold;">time</span> 00:00:<span style="color: #000000;">12</span></pre></td></tr></table></div>

<p>Der Cronjob sieht wie folgt aus:</p>

<div class="wp_codebox"><table width="100%" ><tr id="p20544"><td class="code" id="p205code44"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#Datenbank Backup</span>
<span style="color: #000000;">33</span> <span style="color: #000000;">3</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #c20cb9; font-weight: bold;">sh</span> <span style="color: #000000; font-weight: bold;">/</span>root<span style="color: #000000; font-weight: bold;">/</span>mysql-backup<span style="color: #000000; font-weight: bold;">/</span>mysql-backup.sh</pre></td></tr></table></div>

<p>Viel Spass damit <img src='http://www.lunar.lu/wp-includes/images/smilies/icon_wink.gif' alt="Icon Wink in MySQL - Datenbank Backup Script" class='wp-smiley' /> </p>
<!-- google_ad_section_end -->
<img src="http://feeds.feedburner.com/~r/lunar-lu/~4/wTYh0s8JQxE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.lunar.lu/mysql-datenbank-backup-script/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.lunar.lu/mysql-datenbank-backup-script/</feedburner:origLink></item>
		<item>
		<title>MySQL - Benutzerrechte exportieren</title>
		<link>http://feedproxy.google.com/~r/lunar-lu/~3/riIcv9j_rz8/</link>
		<comments>http://www.lunar.lu/mysql-benutzerrechte-privileges-grants-exportieren/#comments</comments>
		<pubDate>Fri, 03 Apr 2009 12:04:43 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[PHP & MySQL]]></category>

		<category><![CDATA[datenbank administration]]></category>

		<category><![CDATA[MySQL]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[script]]></category>

		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://www.lunar.lu/?p=192</guid>
		<description><![CDATA[

Ich wollte heute Morgen die Benutzerrechte (Privileges) der MySQL-Benutzerkonten eines Servers exportieren, und war recht erstaunt dar&#252;ber, zu diesem Thema weder einen Eintrag im MySQL-Handbuch, noch eine Beschreibung &#252;ber Google finden zu k&#246;nnen.
Ich habe mich also drangesetzt, und nach einiger &#220;berlegung, folgendes Script geschrieben:


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
&#60;?php
&#160;
//Mit Datenbank verbinden
if &#40; &#40;$db_handle = mysql_connect&#40;'HOST', 'BENUTZERNAME', 'PASSWORT'&#41;&#41; === false&#41;
&#123;
  [...]]]></description>
			<content:encoded><![CDATA[
<!-- google_ad_section_start -->
<p>Ich wollte heute Morgen die Benutzerrechte (Privileges) der MySQL-Benutzerkonten eines Servers exportieren, und war recht erstaunt dar&#252;ber, zu diesem Thema weder einen Eintrag im MySQL-Handbuch, noch eine Beschreibung &#252;ber Google finden zu k&#246;nnen.</p>
<p>Ich habe mich also drangesetzt, und nach einiger &#220;berlegung, folgendes Script geschrieben:</p>
<p><span id="more-192"></span></p>

<div class="wp_codebox"><table width="100%" ><tr id="p19247"><td class="line_numbers"><pre>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
</pre></td><td class="code" id="p192code47"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Mit Datenbank verbinden</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$db_handle</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_connect"><span style="color: #990000;">mysql_connect</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'HOST'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'BENUTZERNAME'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'PASSWORT'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
        <a href="http://www.php.net/die"><span style="color: #990000;">die</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/mysql_error"><span style="color: #990000;">mysql_error</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Benutzer auslesen</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT CONCAT(<span style="color: #000099; font-weight: bold;">\&quot;</span>'<span style="color: #000099; font-weight: bold;">\&quot;</span>,User, <span style="color: #000099; font-weight: bold;">\&quot;</span>'@'<span style="color: #000099; font-weight: bold;">\&quot;</span>, Host, <span style="color: #000099; font-weight: bold;">\&quot;</span>'<span style="color: #000099; font-weight: bold;">\&quot;</span>) AS username, CONCAT(<span style="color: #000099; font-weight: bold;">\&quot;</span>'<span style="color: #000099; font-weight: bold;">\&quot;</span>, Password, <span style="color: #000099; font-weight: bold;">\&quot;</span>'<span style="color: #000099; font-weight: bold;">\&quot;</span>) AS password FROM mysql.user&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db_handle</span><span style="color: #009900;">&#41;</span> or <a href="http://www.php.net/die"><span style="color: #990000;">die</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/mysql_error"><span style="color: #990000;">mysql_error</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$db_handle</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$get</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_fetch_object"><span style="color: #990000;">mysql_fetch_object</span></a> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">//Query zur Erstellung des Benutzers</span>
        <a href="http://www.php.net/echo"><span style="color: #990000;">echo</span></a> <span style="color: #0000ff;">&quot;CREATE USER &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$get</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; IDENTIFIED BY &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$get</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">password</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//Benutzerrechte auslesen</span>
        <span style="color: #000088;">$ssql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SHOW GRANTS FOR &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$get</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$squery</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ssql</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db_handle</span><span style="color: #009900;">&#41;</span> or <a href="http://www.php.net/die"><span style="color: #990000;">die</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/mysql_error"><span style="color: #990000;">mysql_error</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$db_handle</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$srow</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_fetch_array"><span style="color: #990000;">mysql_fetch_array</span></a> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$squery</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">//Query zur erstellung der Benutzerrechte</span>
                <a href="http://www.php.net/echo"><span style="color: #990000;">echo</span></a> <span style="color: #000088;">$srow</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>Wie kann man die Benutzerrechte &#252;ber dieses Script exportieren?</strong></p>
<ol>
<li>Den Quelltext in einer Datei, zum Beispiel <code>export_privileges.php</code>, speichern</li>
<li>Die Rechte exportieren: <code>export_privileges.php > privileges.sql</code></li>
<li>Die Rechte importieren: <code>mysql < privileges.sql</code></li>
<li>Die Rechte neu laden: <code>mysql -e "FLUSH PRIVILEGES;"</code></li>
</ol>
<p>Zusammenfassung:</p>

<div class="wp_codebox"><table width="100%" ><tr id="p19248"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p192code48"><pre class="php" style="font-family:monospace;">claude<span style="color: #339933;">@</span>localhost ~ $ export_privileges<span style="color: #339933;">.</span>php <span style="color: #339933;">&gt;</span> privileges<span style="color: #339933;">.</span>sql
claude<span style="color: #339933;">@</span>localhost ~ $ <a href="http://www.php.net/mysql"><span style="color: #990000;">mysql</span></a> <span style="color: #339933;">-</span>uBENUTZERNAME <span style="color: #339933;">-</span>pPASSWORT <span style="color: #339933;">-</span>hHOST <span style="color: #339933;">&lt;</span> privileges<span style="color: #339933;">.</span>sql
claude<span style="color: #339933;">@</span>localhost ~ $ <a href="http://www.php.net/mysql"><span style="color: #990000;">mysql</span></a> <span style="color: #339933;">-</span>uBENUTZERNAME <span style="color: #339933;">-</span>pPASSWORT <span style="color: #339933;">-</span>hHOST <span style="color: #339933;">-</span>e <span style="color: #0000ff;">&quot;FLUSH PRIVILEGES;&quot;</span></pre></td></tr></table></div>

<p>Fertig <img src='http://www.lunar.lu/wp-includes/images/smilies/icon_wink.gif' alt="Icon Wink in MySQL - Benutzerrechte exportieren" class='wp-smiley' /> </p>
<!-- google_ad_section_end -->
<img src="http://feeds.feedburner.com/~r/lunar-lu/~4/riIcv9j_rz8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.lunar.lu/mysql-benutzerrechte-privileges-grants-exportieren/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.lunar.lu/mysql-benutzerrechte-privileges-grants-exportieren/</feedburner:origLink></item>
		<item>
		<title>Cannot delete or update a parent row: a foreign key constraint fails</title>
		<link>http://feedproxy.google.com/~r/lunar-lu/~3/ve6PJH9ikl8/</link>
		<comments>http://www.lunar.lu/cannot-delete-or-update-a-parent-row-a-foreign-key-constraint-fails/#comments</comments>
		<pubDate>Thu, 02 Apr 2009 09:51:59 +0000</pubDate>
		<dc:creator>claude</dc:creator>
		
		<category><![CDATA[MySQL]]></category>

		<category><![CDATA[datenbank administration]]></category>

		<category><![CDATA[innodb]]></category>

		<category><![CDATA[mysqldump]]></category>

		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://www.lunar.lu/?p=162</guid>
		<description><![CDATA[

Beim Importieren von InnoDB-Datenbanken kann es unter Umst&#228;nden zu folgendem Fehler kommen:

ERROR 1217 (23000) at line 243: Cannot delete or update a parent row: a foreign key constraint fails

Wie kommt es zu diesem Fehler?
Wenn man eine Datenbank mit mysqldump exportiert, wird eine Datei mit SQL - Befehlen erzeugt. Wenn man diese Datei anschliessend importiert, werden [...]]]></description>
			<content:encoded><![CDATA[
<!-- google_ad_section_start -->
<p>Beim Importieren von InnoDB-Datenbanken kann es unter Umst&#228;nden zu folgendem Fehler kommen:</p>

<div class="wp_codebox"><table width="100%" ><tr id="p16252"><td class="code" id="p162code52"><pre class="shell" style="font-family:monospace;">ERROR 1217 (23000) at line 243: Cannot delete or update a parent row: a foreign key constraint fails</pre></td></tr></table></div>

<p><strong>Wie kommt es zu diesem Fehler?</strong></p>
<p>Wenn man eine Datenbank mit <code>mysqldump</code> exportiert, wird eine Datei mit SQL - Befehlen erzeugt. Wenn man diese Datei anschliessend importiert, werden diese Befehle nacheinander ausgef&#252;hrt und erzeugen eine Datenbank, die identisch zu der exportierten Version ist.</p>
<p>Bei InnoDB-Tabellen mit Fremdschl&#252;ssel-Beschr&#228;nkungen wird es aber mit Sicherheit zu dem oben gennanten Fehler kommen. Dies liegt daran, dass die SQL-Befehle ohne logischen Zusammenhang ausgef&#252;hrt werden.</p>
<p>Das <a href="http://dev.mysql.com/doc/refman/5.1/de/innodb-foreign-key-constraints.html">MySQL Handbuch</a> schreibt:</p>
<blockquote><p>InnoDB weist jede INSERT- oder UPDATE-Operation zur&#252;ck, die versucht, einen Fremdschl&#252;sselwert in einer Kindtabelle anzulegen, wenn kein passender Schl&#252;sselwert in der Elterntabelle vorhanden ist.</p></blockquote>
<p>Da die SQL-Befehle, wie gesagt, ohne logischen Zusammenhang (der Reihe nach) abgearbeitet werden, kann es gut sein, dass die Kindtabelle vor der entsprechenden Elterntabelle importiert wird.</p>
<p><strong>Wie umgeht man dieses Problem?</strong></p>
<p><span id="more-162"></span><br />
<em>L&#246;sung 1:</em><br />
Man editiert die Backup-Datei und f&#252;gt folgende Befehle hinzu:</p>
<ul>
<li><code>SET FOREIGN_KEY_CHECKS=0;</code> => An den Anfang der Datei</li>
<li><code>SET FOREIGN_KEY_CHECKS=1;</code> => Ans Ende der Datei</li>
</ul>

<div class="wp_codebox"><table width="100%" ><tr id="p16253"><td class="code" id="p162code53"><pre class="shell" style="font-family:monospace;">echo &quot;SET FOREIGN_KEY_CHECKS=0;&quot; &gt; backup_no_fkc.sql
cat backup.sql &gt;&gt; backup_no_fkc.sql
echo &quot;SET FOREIGN_KEY_CHECKS=1;&quot; &gt;&gt; backup_no_fkc.sql</pre></td></tr></table></div>

<p>Nach dieser &#196;nderung kann die Datei  wie gewohnt importiert werden.</p>
<p><em>In PHPMyAdmin ist es &#252;brigens m&#246;glich beim Exportieren die Option <strong>Disable foreign key checks</strong> anzukreuzen, dann werden diese beiden Befehle automatisch zur Backup-Datei hinzugef&#252;gt</em>.</p>
<p><em>L&#246;sung 2:</em><br />
Man arbeitet mit der Konsole und importiert die Datei wie folgt:</p>

<div class="wp_codebox"><table width="100%" ><tr id="p16254"><td class="code" id="p162code54"><pre class="shell" style="font-family:monospace;">mysql&gt; SET FOREIGN_KEY_CHECKS = 0;
mysql&gt; SOURCE backup.sql
mysql&gt; SET FOREIGN_KEY_CHECKS = 1;</pre></td></tr></table></div>

<p> <img src='http://www.lunar.lu/wp-includes/images/smilies/icon_wink.gif' alt="Icon Wink in Cannot delete or update a parent row: a foreign key constraint fails" class='wp-smiley' /> </p>
<!-- google_ad_section_end -->
<img src="http://feeds.feedburner.com/~r/lunar-lu/~4/ve6PJH9ikl8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.lunar.lu/cannot-delete-or-update-a-parent-row-a-foreign-key-constraint-fails/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.lunar.lu/cannot-delete-or-update-a-parent-row-a-foreign-key-constraint-fails/</feedburner:origLink></item>
		<item>
		<title>MySQL - Spalten nach Typ gruppieren</title>
		<link>http://feedproxy.google.com/~r/lunar-lu/~3/QgujF6tjkOE/</link>
		<comments>http://www.lunar.lu/mysql-spalten-nach-typgruppieren-utf8-general-ci-latin1-swedish-ci/#comments</comments>
		<pubDate>Wed, 01 Apr 2009 14:17:32 +0000</pubDate>
		<dc:creator>claude</dc:creator>
		
		<category><![CDATA[PHP & MySQL]]></category>

		<category><![CDATA[datenbank administration]]></category>

		<category><![CDATA[MySQL]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://www.lunar.lu/?p=138</guid>
		<description><![CDATA[

Ich habe mich heute mit einer etwas vernachl&#228;ssigten Datenbank besch&#228;ftigt. Diese enthielt eine bunte Mischung von Spalten in den Formaten utf8_general_ci sowie latin1_swedish_ci. 
Ziel der Aktion war es die Struktur erstmal zu analysieren und dann grundlegend zu &#252;berarbeiten.
Zuerst ging es also darum einen &#220;berblick dar&#252;ber zu erhalten, welche Spalten-Typen wie oft vorhanden waren. Zu diesem [...]]]></description>
			<content:encoded><![CDATA[
<!-- google_ad_section_start -->
<p>Ich habe mich heute mit einer <em>etwas</em> vernachl&#228;ssigten Datenbank besch&#228;ftigt. Diese enthielt eine bunte Mischung von Spalten in den Formaten <code>utf8_general_ci</code> sowie <code>latin1_swedish_ci</code>. </p>
<p>Ziel der Aktion war es die Struktur erstmal zu analysieren und dann grundlegend zu &#252;berarbeiten.</p>
<p>Zuerst ging es also darum einen &#220;berblick dar&#252;ber zu erhalten, welche Spalten-Typen wie oft vorhanden waren. Zu diesem Zweck habe ich mir folgendes Script geschrieben:</p>
<p><span id="more-138"></span></p>

<div class="wp_codebox"><table width="100%" ><tr id="p13857"><td class="line_numbers"><pre>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
</pre></td><td class="code" id="p138code57"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//Mit Datenbank-Server verbinden</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$db_handle</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_connect"><span style="color: #990000;">mysql_connect</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SERVER'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'BENUTZER_NAME'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'PASSWORD'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
        <a href="http://www.php.net/die"><span style="color: #990000;">die</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/mysql_error"><span style="color: #990000;">mysql_error</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Datenbank</span>
<span style="color: #000088;">$database</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'DATENBANK'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Tabellen auslesen</span>
<span style="color: #000088;">$tsql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SHOW TABLES FROM &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$database</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$tquery</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tsql</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db_handle</span><span style="color: #009900;">&#41;</span> or <a href="http://www.php.net/die"><span style="color: #990000;">die</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/mysql_error"><span style="color: #990000;">mysql_error</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$db_handle</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$trow</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_fetch_array"><span style="color: #990000;">mysql_fetch_array</span></a> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$tquery</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">//Aktuelle Tabelle</span>
        <span style="color: #000088;">$table</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$trow</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//Spalten auslesen</span>
        <span style="color: #000088;">$csql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SHOW FULL COLUMNS FROM &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$database</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;.&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$table</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$cquery</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$csql</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db_handle</span><span style="color: #009900;">&#41;</span> or <a href="http://www.php.net/die"><span style="color: #990000;">die</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/mysql_error"><span style="color: #990000;">mysql_error</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$db_handle</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$crow</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_fetch_row"><span style="color: #990000;">mysql_fetch_row</span></a> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$cquery</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> <a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$crow</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#123;</span>
                        <span style="color: #000088;">$column_arr</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$crow</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$table</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'.'</span><span style="color: #339933;">.</span><span style="color: #000088;">$crow</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<a href="http://www.php.net/print_r"><span style="color: #990000;">print_r</span></a> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$column_arr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>In einem meiner n&#228;chsten Artikel, werde ich beschreiben wie man die Spalten bequem in ein anderes Format konvertieren kann <img src='http://www.lunar.lu/wp-includes/images/smilies/icon_wink.gif' alt="Icon Wink in MySQL - Spalten nach Typ gruppieren" class='wp-smiley' /> </p>
<p>Auf die Datenbank meines Blogs angewendet, sieht die Ausgabe &#252;brigens so aus:</p>

<div class="wp_codebox"><table width="100%" ><tr id="p13858"><td class="line_numbers"><pre>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
</pre></td><td class="code" id="p138code58"><pre class="php" style="font-family:monospace;"><a href="http://www.php.net/array"><span style="color: #990000;">Array</span></a>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>latin1_swedish_ci<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">Array</span></a>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_comments<span style="color: #339933;">.</span>comment_author
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_comments<span style="color: #339933;">.</span>comment_author_email
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_comments<span style="color: #339933;">.</span>comment_author_url
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_comments<span style="color: #339933;">.</span>comment_author_IP
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_comments<span style="color: #339933;">.</span>comment_content
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_comments<span style="color: #339933;">.</span>comment_approved
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_comments<span style="color: #339933;">.</span>comment_agent
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_comments<span style="color: #339933;">.</span>comment_type
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_izioseo_anonym_links<span style="color: #339933;">.</span>link_url
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_izioseo_anonym_links<span style="color: #339933;">.</span>link_hash
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_izioseo_referers<span style="color: #339933;">.</span>post_url
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">11</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_izioseo_referers<span style="color: #339933;">.</span>referer_searchengine
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">12</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_izioseo_referers<span style="color: #339933;">.</span>referer_url
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">13</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_izioseo_referers<span style="color: #339933;">.</span>referer_request
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">14</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_izioseo_referers_keywords<span style="color: #339933;">.</span>referer_keyword
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">15</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_links<span style="color: #339933;">.</span>link_url
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">16</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_links<span style="color: #339933;">.</span>link_name
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">17</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_links<span style="color: #339933;">.</span>link_image
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">18</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_links<span style="color: #339933;">.</span>link_target
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">19</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_links<span style="color: #339933;">.</span>link_description
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">20</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_links<span style="color: #339933;">.</span>link_visible
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">21</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_links<span style="color: #339933;">.</span>link_rel
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">22</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_links<span style="color: #339933;">.</span>link_notes
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">23</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_links<span style="color: #339933;">.</span>link_rss
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">24</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_options<span style="color: #339933;">.</span>option_name
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">25</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_options<span style="color: #339933;">.</span>option_value
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">26</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_options<span style="color: #339933;">.</span>autoload
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">27</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_postmeta<span style="color: #339933;">.</span>meta_key
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">28</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_postmeta<span style="color: #339933;">.</span>meta_value
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">29</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_posts<span style="color: #339933;">.</span>post_content
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">30</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_posts<span style="color: #339933;">.</span>post_title
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">31</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_posts<span style="color: #339933;">.</span>post_excerpt
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">32</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_posts<span style="color: #339933;">.</span>post_status
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">33</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_posts<span style="color: #339933;">.</span>comment_status
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">34</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_posts<span style="color: #339933;">.</span>ping_status
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">35</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_posts<span style="color: #339933;">.</span>post_password
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">36</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_posts<span style="color: #339933;">.</span>post_name
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">37</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_posts<span style="color: #339933;">.</span>to_ping
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">38</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_posts<span style="color: #339933;">.</span>pinged
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">39</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_posts<span style="color: #339933;">.</span>post_content_filtered
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">40</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_posts<span style="color: #339933;">.</span>guid
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">41</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_posts<span style="color: #339933;">.</span>post_type
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">42</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_posts<span style="color: #339933;">.</span>post_mime_type
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">43</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_term_taxonomy<span style="color: #339933;">.</span>taxonomy
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">44</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_term_taxonomy<span style="color: #339933;">.</span>description
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">45</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_terms<span style="color: #339933;">.</span>name
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">46</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_terms<span style="color: #339933;">.</span>slug
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">47</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_usermeta<span style="color: #339933;">.</span>meta_key
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">48</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_usermeta<span style="color: #339933;">.</span>meta_value
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">49</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_users<span style="color: #339933;">.</span>user_login
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">50</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_users<span style="color: #339933;">.</span>user_pass
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">51</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_users<span style="color: #339933;">.</span>user_nicename
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">52</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_users<span style="color: #339933;">.</span>user_email
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">53</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_users<span style="color: #339933;">.</span>user_url
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">54</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_users<span style="color: #339933;">.</span>user_activation_key
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">55</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> wp_users<span style="color: #339933;">.</span>display_name
        <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span></pre></td></tr></table></div>
<!-- google_ad_section_end -->
<img src="http://feeds.feedburner.com/~r/lunar-lu/~4/QgujF6tjkOE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.lunar.lu/mysql-spalten-nach-typgruppieren-utf8-general-ci-latin1-swedish-ci/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.lunar.lu/mysql-spalten-nach-typgruppieren-utf8-general-ci-latin1-swedish-ci/</feedburner:origLink></item>
		<item>
		<title>MySQL - Datenbanken kopieren, duplizieren</title>
		<link>http://feedproxy.google.com/~r/lunar-lu/~3/ZNbXaLfaTiA/</link>
		<comments>http://www.lunar.lu/mysql-datenbanken-kopieren-duplizieren/#comments</comments>
		<pubDate>Tue, 24 Mar 2009 22:51:50 +0000</pubDate>
		<dc:creator>claude</dc:creator>
		
		<category><![CDATA[PHP & MySQL]]></category>

		<category><![CDATA[datenbank administration]]></category>

		<category><![CDATA[MySQL]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://www.lunar.lu/?p=118</guid>
		<description><![CDATA[

In einem meiner vorigen Artikel habe ich beschrieben, wie man mit mysqldump MySQL - Datenbanken von Server zu Server kopieren kann.
Es kommt jedoch auch oft vor, dass man eine MySQL - Datenbank duplizieren, sprich eine Kopie auf dem gleichen Server erstellen m&#246;chte. 
Dies kann zum Beispiel der Fall sein, wenn man eine &#196;nderung und/oder ein [...]]]></description>
			<content:encoded><![CDATA[
<!-- google_ad_section_start -->
<p>In einem meiner vorigen Artikel habe ich beschrieben, wie man mit <code>mysqldump</code> <a href="/mysql-datenbanken-mysqldump-anderen-server-kopieren/">MySQL - Datenbanken von Server zu Server kopieren</a> kann.</p>
<p>Es kommt jedoch auch oft vor, dass man eine MySQL - Datenbank duplizieren, sprich eine Kopie auf dem gleichen Server erstellen m&#246;chte. </p>
<p>Dies kann zum Beispiel der Fall sein, wenn man eine &#196;nderung und/oder ein Script testen -dazu aber nicht die Hauptdatenbank verwenden m&#246;chte. Es gibt leider keinen direkten Befehl um eine Datenbank zu duplizieren, daher muss man leider den Umweg &#252;ber, zum Beispiel, ein PHP-Script gehen. </p>
<p><strong>Hier stelle ich ein PHP-Script vor, welches es erm&#246;glicht, eine Kopie einer MySQL - Datenbank zu erstellen&#8230;</strong></p>
<p><span id="more-118"></span></p>

<div class="wp_codebox"><table width="100%" ><tr id="p11860"><td class="code" id="p118code60"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Mit Datenbank-Server verbinden</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$db_handle</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_connect"><span style="color: #990000;">mysql_connect</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SERVER'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'BENUTZER_NAME'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'PASSWORD'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<a href="http://www.php.net/die"><span style="color: #990000;">die</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/mysql_error"><span style="color: #990000;">mysql_error</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Quell-Datenbank</span>
<span style="color: #000088;">$database_source</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'datenbank'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Ziel-Datenbank</span>
<span style="color: #000088;">$database_target</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'datenbank_kopie'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Ziel-Datenbank erstellen</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;CREATE database &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$database_target</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db_handle</span><span style="color: #009900;">&#41;</span> or <a href="http://www.php.net/die"><span style="color: #990000;">die</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/mysql_error"><span style="color: #990000;">mysql_error</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$db_handle</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Tabellen der Quell-Datenbank auslesen</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SHOW TABLES FROM &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$database_source</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db_handle</span><span style="color: #009900;">&#41;</span> or <a href="http://www.php.net/die"><span style="color: #990000;">die</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/mysql_error"><span style="color: #990000;">mysql_error</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$db_handle</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$num_tables</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_num_rows"><span style="color: #990000;">mysql_num_rows</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$num_tables</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">//Name der Tabelle</span>
	<span style="color: #000088;">$table_name</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_result"><span style="color: #990000;">mysql_result</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #339933;">,</span> <span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>			
&nbsp;
	<span style="color: #666666; font-style: italic;">//Query zur Erstellung der Tabelle auslesen</span>
	<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SHOW CREATE TABLE &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$database_source</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;.&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$table_name</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$sub_query</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db_handle</span><span style="color: #009900;">&#41;</span> or <a href="http://www.php.net/die"><span style="color: #990000;">die</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/mysql_error"><span style="color: #990000;">mysql_error</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$db_handle</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$sub_query_data_arr</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_fetch_row"><span style="color: #990000;">mysql_fetch_row</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sub_query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
&nbsp;
	<span style="color: #666666; font-style: italic;">//Query zur Erstellung der neuen Tabelle konstruieren</span>
	<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/str_replace"><span style="color: #990000;">str_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;`&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$table_name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;`&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$database_target</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;.&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$table_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$sub_query_data_arr</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//Tabelle in Ziel-Datenbank erstellen</span>
	<span style="color: #000088;">$sub_query</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db_handle</span><span style="color: #009900;">&#41;</span> or <a href="http://www.php.net/die"><span style="color: #990000;">die</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/mysql_error"><span style="color: #990000;">mysql_error</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$db_handle</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//Daten Einfügen</span>
	<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT INTO &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$database_target</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;.&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$table_name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; SELECT * FROM &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$database_source</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;.&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$table_name</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$sub_query</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db_handle</span><span style="color: #009900;">&#41;</span> or <a href="http://www.php.net/die"><span style="color: #990000;">die</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/mysql_error"><span style="color: #990000;">mysql_error</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$db_handle</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Fertig</span>
<a href="http://www.php.net/mysql_close"><span style="color: #990000;">mysql_close</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$db_handle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Falls Ihnen dieses Script gef&#228;llt, und Sie es auf Ihrer Webseite einsetzen, so w&#252;rde ich mich &#252;ber einen Backlink zu meinem Blog freuen <img src='http://www.lunar.lu/wp-includes/images/smilies/icon_wink.gif' alt="Icon Wink in MySQL - Datenbanken kopieren, duplizieren" class='wp-smiley' /> </p>
<!-- google_ad_section_end -->
<img src="http://feeds.feedburner.com/~r/lunar-lu/~4/ZNbXaLfaTiA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.lunar.lu/mysql-datenbanken-kopieren-duplizieren/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.lunar.lu/mysql-datenbanken-kopieren-duplizieren/</feedburner:origLink></item>
	</channel>
</rss>
