<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	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:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>H.-Peter Pfeufer &#187; Webserver</title>
	<atom:link href="http://ppfeufer.de/tag/webserver/feed/" rel="self" type="application/rss+xml" />
	<link>http://ppfeufer.de</link>
	<description>Webentwickler und WordPress-Freund</description>
	<lastBuildDate>Thu, 23 May 2013 14:19:45 +0000</lastBuildDate>
	<language>de-DE</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
	<atom:link rel='hub' href='http://ppfeufer.de/?pushpress=hub'/>
		<item>
		<title>Backup der Webseite und Datenbank</title>
		<link>http://ppfeufer.de/backup-der-webseite-und-datenbank/</link>
		<comments>http://ppfeufer.de/backup-der-webseite-und-datenbank/#comments</comments>
		<pubDate>Thu, 04 Nov 2010 18:41:51 +0000</pubDate>
		<dc:creator>H.-Peter Pfeufer</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Tipps und Tricks]]></category>
		<category><![CDATA[Backup]]></category>
		<category><![CDATA[bash-Script]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[Server]]></category>
		<category><![CDATA[SSH]]></category>
		<category><![CDATA[Webserver]]></category>

		<guid isPermaLink="false">http://ppfeufer.de/?p=2349</guid>
		<description><![CDATA[Webseitenbetreiber, Inhaber und deren Admins sind letztlich in gleicher Weise betroffen von diesem Thema &#8211; Backups. Wie wichtig diese wirklich sind, muss an dieser Stelle wohl nicht mehr diskutiert werden. Sie sind einfach wichtig. Egal ob nun &#8220;nur&#8221; die Dateien, &#8230;</p><p class="read-more"><a class="more-link" href="http://ppfeufer.de/backup-der-webseite-und-datenbank/">Weiterlesen <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Webseitenbetreiber, Inhaber und deren Admins sind letztlich in gleicher Weise betroffen von diesem Thema &#8211; Backups. Wie wichtig diese wirklich sind, muss an dieser Stelle wohl nicht mehr diskutiert werden. Sie sind einfach wichtig. Egal ob nun &#8220;nur&#8221; die Dateien, aus denen eine Webseite besteht, gesichert werden, oder die Datenbank. In einem Backup sollten immer alle Daten enthalten sein. Ein solches Backup ist natürlich einfacher, wenn man direkten Zugriff auf den Server hat und nicht nur per FTP, wie bei einem reinen Webhoster. Dieser Artikel setzt demnach diesen Direktzugriff via SSH voraus und auch die Möglichkeit Cronjobs einzurichten.<br />
<span id="more-2349"></span><br />
Bevor man eine Webseite online stellt, ist es natürlich wichtig, sich Gedanken über die grundlegende Verzeichnisstruktur zu machen. Denn je übersichtlicher diese gehalten ist, desto einfacher ist das Backup.</p>
<h2>Einige Grundideen:</h2>
<p>Das Verzeichnis des Seitenbetreibers liegt beispielsweise unter:<br />
<em>/var/customers/webs/seitenbetreiber</em></p>
<p>Somit ist eine strukturelle Aufteilung der Webseiten des Seitenbetreibers sehr einfach möglich. Webseite A liegt nun zum Beispiel unter <em>/var/customers/webs/seitenbetreiber/webseite-a</em> und Webseite B unter <em>/var/customers/webs/seitenbetreiber/webseite-b</em> und so weiter und so fort. Das Backupverzeichnis ist demnach unter <em>/var/customers/webs/seitenbetreiber/backups</em>.</p>
<p>Daraus ergibt sich also folgende Struktur auf dem Server, wird das Verzeichnis des Seitenbetreibers aufgelistet.</p>
<div class="codecolorer-container text geshi syntaxhighlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:99%;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">server seitenbetreiber # ll<br />
insgesamt 16K<br />
-rwxr-xr-x 1 root  root  1,2K  4. Nov 18:29 backup-webseite-a.sh<br />
-rwxr-xr-x 1 root  root  1,2K  4. Nov 18:29 backup-webseite-b.sh<br />
drwxr-xr-x 3 root  root  4,0K  4. Nov 02:06 backups<br />
drwxrwxr-x 4 10001 10001 4,0K  4. Nov 18:29 webseite-a<br />
drwxrwxr-x 2 10001 10001 4,0K  4. Nov 00:00 webseite-b</div></td></tr></tbody></table></div>
<p>Wie hier zu sehen ist, liegen auch in diesem Verzeichnis die Backupscripte für die einzelnen Webseiten. Ich persönlich bevorzuge es, die Seiten einzeln zu &#8220;verarbeiten&#8221; da sie sich so auch separierter wieder herstellen lassen, wenn zum Beispiel ein Datenverlust nur eine Seite betrifft.</p>
<h2>Das Backupscript</h2>
<p>Dieses ist ein reines Bash-Script welches letztlich als Cronjob ausgeführt wird. Also nach einem festgelegten Zeitplan. Zur Übersicht wird durch das Script im Backupverzeichnis wieder eine eigene Verzeichnisstruktur erstellt, welche dem Schema <em>backups/webseite/Jahr-Monat</em> folgt. Also <em>backups/webseite-a/2010-11</em> als Beispiel. Auch wird in diesem Script gleich die Datenbank der Seite mit berücksichtigt und dem Backuparchiv als mySQL-Dump hinzugefügt.</p>
<p>Hierfür müssen im Script zunächst einige Variablen definiert werden.</p>
<div class="codecolorer-container bash geshi syntaxhighlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:99%;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;"># setting variables</span><br />
<span style="color: #007800;">DATE</span>=<span style="color: #000000; font-weight: bold;">`/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">date</span> <span style="color: #ff0000;">'+%Y-%m-%d'</span><span style="color: #000000; font-weight: bold;">`</span><br />
<span style="color: #007800;">MONTH</span>=<span style="color: #000000; font-weight: bold;">`/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">date</span> <span style="color: #ff0000;">'+%Y-%m'</span><span style="color: #000000; font-weight: bold;">`</span><br />
<span style="color: #007800;">CUSTOMER_DIR</span>=<span style="color: #ff0000;">&quot;/var/customers/webs/seitenbetreiber/&quot;</span><br />
<span style="color: #007800;">SOURCE_DIR</span>=<span style="color: #ff0000;">&quot;webseite/&quot;</span><br />
<span style="color: #007800;">BACKUP_DIR</span>=<span style="color: #ff0000;">&quot;backups/webseite&quot;</span><br />
<span style="color: #007800;">MYSQLDUMP_FILENAME</span>=<span style="color: #ff0000;">&quot;webseite-backup-datenbank.sql&quot;</span><br />
<span style="color: #007800;">BACKUP_FILENAME</span>=<span style="color: #ff0000;">&quot;webseite-backup-filesystem.tar.gz&quot;</span><br />
<br />
<span style="color: #007800;">DB_NAME</span>=<span style="color: #ff0000;">&quot;&quot;</span><br />
<span style="color: #007800;">DB_USER</span>=<span style="color: #ff0000;">&quot;&quot;</span><br />
<span style="color: #007800;">DB_PASSWORD</span>=<span style="color: #ff0000;">&quot;&quot;</span><br />
<span style="color: #007800;">DB_HOST</span>=<span style="color: #ff0000;">&quot;localhost&quot;</span></div></td></tr></tbody></table></div>
<p>Diese variablen werden im Laufe des Scriptes benötigt, damit man nicht immer alle Pfadangaben hinschreiben muss. Da dieses Script durch einen Cronjob gestartet wird, muss man natürlich sicher gehen, dass es auch im richtigen Ordner ausgeführt wird.</p>
<div class="codecolorer-container bash geshi syntaxhighlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:99%;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #007800;">$CUSTOMER_DIR</span></div></td></tr></tbody></table></div>
<p>Damit werden alle im Script folgenden Operationen im CUSTOMER_DIR ausgeführt.</p>
<p>Als nächstes folgt die Datenbank. Dazu wird ein mySQL-Dump erstellt. Im gleichen Atemzug wird sicher gestellt, dass der Dump auch im UTF-8 Format ist und die Daten bei der Wiederherstellung ebenfalls als UTF-8 in die Datenbank geschrieben werden.</p>
<div class="codecolorer-container bash geshi syntaxhighlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:99%;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;"># dumping mysql and storing dump in $SOURCE_DIR</span><br />
<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>mysqldump <span style="color: #660033;">--opt</span> <span style="color: #660033;">-Q</span> -u<span style="color: #007800;">$DB_USER</span> -p<span style="color: #007800;">$DB_PASSWORD</span> -h<span style="color: #007800;">$DB_HOST</span> <span style="color: #007800;">$DB_NAME</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> s<span style="color: #ff0000;">'/DEFAULT CHARSET=.*;/DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;/g'</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #007800;">$SOURCE_DIR</span><span style="color: #007800;">$DATE</span>-<span style="color: #007800;">$MYSQLDUMP_FILENAME</span></div></td></tr></tbody></table></div>
<p>Dar Dump der Datenbank wird hierbei direkt im Verzeichnis der Webseite abgelegt und ist somit im Backup enthalten. Nun können die Dateien zu einem Archiv zusammen gefasst und im Backupordner gespeichert werden.</p>
<div class="codecolorer-container bash geshi syntaxhighlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:99%;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;"># tar $SOURCEDIR</span><br />
<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: #ff0000;">&quot;<span style="color: #007800;">${BACKUP_DIR}</span>/<span style="color: #007800;">${MONTH}</span>/&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;<br />
<span style="color: #000000; font-weight: bold;">then</span><br />
&nbsp; &nbsp; <span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #007800;">$BACKUP_DIR</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$MONTH</span><span style="color: #000000; font-weight: bold;">/</span><br />
<span style="color: #000000; font-weight: bold;">fi</span><br />
<br />
<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-cpzf</span> <span style="color: #007800;">$BACKUP_DIR</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$MONTH</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$DATE</span>-<span style="color: #007800;">$BACKUP_FILENAME</span> <span style="color: #007800;">$SOURCE_DIR</span></div></td></tr></tbody></table></div>
<p>Somit liegt nun eine .tar-gz-Datei im dafür vorgesehenen Ordner. Versehen mit Datum und allem was dazu gehört.</p>
<p>Im nächsten Schritt wird der mySQL-Dump, welcher ja immer noch im Verzeichnis der Webseite liegt, wieder gelöscht, denn der soll ja nicht einfach da, er wäre ja momentan downloadbar.</p>
<div class="codecolorer-container bash geshi syntaxhighlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:99%;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;"># removing mysql-dump from $SOURCE_DIR</span><br />
<span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-f</span> <span style="color: #007800;">$SOURCE_DIR</span><span style="color: #007800;">$DATE</span>-<span style="color: #007800;">$MYSQLDUMP_FILENAME</span></div></td></tr></tbody></table></div>
<p>Damit wäre das Script eigentlich schon fertig. Doch, was ist mit der Bereinigung von &#8220;Altlasten&#8221;? Also was ist mit den alten Backups? Natürlich können Backups, wenn man sie nicht mehr braucht gelöscht werden. So ist es zum Beispiel, wenn dieses Script täglich ausgeführt wird, nicht notwendig mehr als 3 Tage zu speichern. Also kann alles was älter ist doch gelöscht werden. Dateileichen auf einem Server mag man doch sowieso nicht. Auch eventuelle leere Verzeichnisse, die durch das Löschen der Dateien entstehen können entsorgt werden.</p>
<div class="codecolorer-container bash geshi syntaxhighlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:99%;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;"># deleting backups older than 3 days</span><br />
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$(find $BACKUP_DIR/ -type f -mtime +2)</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;<br />
<span style="color: #000000; font-weight: bold;">then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #c20cb9; font-weight: bold;">find</span> <span style="color: #007800;">$BACKUP_DIR</span><span style="color: #000000; font-weight: bold;">/</span> <span style="color: #660033;">-type</span> f <span style="color: #660033;">-mtime</span> +<span style="color: #000000;">2</span> <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><br />
<span style="color: #000000; font-weight: bold;">fi</span><br />
<br />
<span style="color: #666666; font-style: italic;"># removing empty directorys</span><br />
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$(find backups/ -type d -empty)</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;<br />
<span style="color: #000000; font-weight: bold;">then</span><br />
&nbsp; &nbsp; <span style="color: #c20cb9; font-weight: bold;">find</span> <span style="color: #007800;">$BACKUP_DIR</span><span style="color: #000000; font-weight: bold;">/</span> <span style="color: #660033;">-type</span> d <span style="color: #660033;">-empty</span> <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;">-r</span><br />
<span style="color: #000000; font-weight: bold;">fi</span></div></td></tr></tbody></table></div>
<p>Damit sind nun auch sämtliche Dateileichen entfernt.</p>
<h2>Der Cronjob</h2>
<p>Damit das Script auch wirklich dann zeitgesteuert ausgeführt wird, empfiehlt es sich, dies über einen Cronjob erledigen zu lassen. In diesem Beispiel wird das Script täglich um 1:30 Uhr nachts ausgeführt.</p>
<p>Der Aufruf um Cronjobs bearbeiten zu können ist</p>
<div class="codecolorer-container bash geshi syntaxhighlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:99%;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">crontab <span style="color: #660033;">-e</span></div></td></tr></tbody></table></div>
<p>Danach gibt man nur noch die &#8220;Zeitsteuerung&#8221; und das auszuführende Script an</p>
<div class="codecolorer-container text geshi syntaxhighlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:99%;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">######## File Backup ########<br />
30 1 * * * sh /var/customers/webs/seitenbetreiber/backup-webseite.sh # Backup täglich um 1:30 nachts</div></td></tr></tbody></table></div>
<p>Nachdem das nun abgespeichert wurde, muss man eigentlich nur noch bis 1:30 Uhr in der Nacht warten und kann den Erfolg begutachten.</p>
<h2>Das komplette Script</h2>
<p>Wem es nun zu mühsam ist, sich die Teile des Scriptes hier einzeln zusammenzubauen, hier das gesamte Script.</p>
<div class="codecolorer-container bash geshi syntaxhighlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:99%;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">#!/bin/bash</span><br />
<br />
<span style="color: #666666; font-style: italic;"># setting variables</span><br />
<span style="color: #007800;">DATE</span>=<span style="color: #000000; font-weight: bold;">`/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">date</span> <span style="color: #ff0000;">'+%Y-%m-%d'</span><span style="color: #000000; font-weight: bold;">`</span><br />
<span style="color: #007800;">MONTH</span>=<span style="color: #000000; font-weight: bold;">`/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">date</span> <span style="color: #ff0000;">'+%Y-%m'</span><span style="color: #000000; font-weight: bold;">`</span><br />
<span style="color: #007800;">CUSTOMER_DIR</span>=<span style="color: #ff0000;">&quot;/var/customers/webs/seitenbetreiber/&quot;</span><br />
<span style="color: #007800;">SOURCE_DIR</span>=<span style="color: #ff0000;">&quot;webseite/&quot;</span><br />
<span style="color: #007800;">BACKUP_DIR</span>=<span style="color: #ff0000;">&quot;backups/webseite&quot;</span><br />
<span style="color: #007800;">MYSQLDUMP_FILENAME</span>=<span style="color: #ff0000;">&quot;webseite-backup-datenbank.sql&quot;</span><br />
<span style="color: #007800;">BACKUP_FILENAME</span>=<span style="color: #ff0000;">&quot;webseite-backup-filesystem.tar.gz&quot;</span><br />
<br />
<span style="color: #007800;">DB_NAME</span>=<span style="color: #ff0000;">&quot;Name der Datenbank&quot;</span><br />
<span style="color: #007800;">DB_USER</span>=<span style="color: #ff0000;">&quot;Datenbanknutzer&quot;</span><br />
<span style="color: #007800;">DB_PASSWORD</span>=<span style="color: #ff0000;">&quot;Passwort&quot;</span><br />
<span style="color: #007800;">DB_HOST</span>=<span style="color: #ff0000;">&quot;localhost&quot;</span><br />
<br />
<span style="color: #666666; font-style: italic;"># starting backup</span><br />
<span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #007800;">$CUSTOMER_DIR</span><br />
<br />
<span style="color: #666666; font-style: italic;"># dumping mysql and storing dump in $SOURCE_DIR</span><br />
<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>mysqldump <span style="color: #660033;">--opt</span> <span style="color: #660033;">-Q</span> -u<span style="color: #007800;">$DB_USER</span> -p<span style="color: #007800;">$DB_PASSWORD</span> -h<span style="color: #007800;">$DB_HOST</span> <span style="color: #007800;">$DB_NAME</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> s<span style="color: #ff0000;">'/DEFAULT CHARSET=.*;/DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;/g'</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #007800;">$SOURCE_DIR</span><span style="color: #007800;">$DATE</span>-<span style="color: #007800;">$MYSQLDUMP_FILENAME</span><br />
<br />
<span style="color: #666666; font-style: italic;"># tar $SOURCEDIR</span><br />
<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: #ff0000;">&quot;<span style="color: #007800;">${BACKUP_DIR}</span>/<span style="color: #007800;">${MONTH}</span>/&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;<br />
<span style="color: #000000; font-weight: bold;">then</span><br />
&nbsp; &nbsp; <span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #007800;">$BACKUP_DIR</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$MONTH</span><span style="color: #000000; font-weight: bold;">/</span><br />
<span style="color: #000000; font-weight: bold;">fi</span><br />
<br />
<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-cpzf</span> <span style="color: #007800;">$BACKUP_DIR</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$MONTH</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$DATE</span>-<span style="color: #007800;">$BACKUP_FILENAME</span> <span style="color: #007800;">$SOURCE_DIR</span><br />
<br />
<span style="color: #666666; font-style: italic;"># removing mysql-dump from $SOURCE_DIR</span><br />
<span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-f</span> <span style="color: #007800;">$SOURCE_DIR</span><span style="color: #007800;">$DATE</span>-<span style="color: #007800;">$MYSQLDUMP_FILENAME</span><br />
<br />
<span style="color: #666666; font-style: italic;"># deleting backups older than 3 days</span><br />
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$(find $BACKUP_DIR/ -type f -mtime +2)</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;<br />
<span style="color: #000000; font-weight: bold;">then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #c20cb9; font-weight: bold;">find</span> <span style="color: #007800;">$BACKUP_DIR</span><span style="color: #000000; font-weight: bold;">/</span> <span style="color: #660033;">-type</span> f <span style="color: #660033;">-mtime</span> +<span style="color: #000000;">2</span> <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><br />
<span style="color: #000000; font-weight: bold;">fi</span><br />
<br />
<span style="color: #666666; font-style: italic;"># removing empty directorys</span><br />
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$(find backups/ -type d -empty)</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;<br />
<span style="color: #000000; font-weight: bold;">then</span><br />
&nbsp; &nbsp; <span style="color: #c20cb9; font-weight: bold;">find</span> <span style="color: #007800;">$BACKUP_DIR</span><span style="color: #000000; font-weight: bold;">/</span> <span style="color: #660033;">-type</span> d <span style="color: #660033;">-empty</span> <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;">-r</span><br />
<span style="color: #000000; font-weight: bold;">fi</span></div></td></tr></tbody></table></div>
<p>Es sei hier noch gesagt, das dieses Script immer nur die in den Variablen angegebene Webseite sichert. Will man mehrere Webseiten sichern, so erstellt einfach mehrere Scripte und passt die entsprechenden Variablen an. Natürlich sind auch die Variablen für die Datenbank noch anzupassen :-)</p>
<p>Warum ich das so mache, habe ich eingangs ja schon erläutert. Ich finde es so einfach komfortabler.</p>
<div class="styled-box box-type-warning"><span class="box-type-icon"></span>
<div class="box-content">Die Nutzung dieses Scriptes geschieht auf <strong>eigene Gefahr</strong>. Ich übernehme <strong>keinerlei</strong> Haftung oder Verantwortung für eventuellen Datenverlust.</div>
</div>
<h2>Update</h2>
<h3>09. November 2010</h3>
<p>Kleinen Syntaxfehler in der Abfrage der alten Dateien und leeren Verzeichnisse behoben.</p>
]]></content:encoded>
			<wfw:commentRss>http://ppfeufer.de/backup-der-webseite-und-datenbank/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Erreichbarkeit eines (Web)-Servers überprüfen</title>
		<link>http://ppfeufer.de/erreichbarkeit-eines-web-servers-ueberpruefen/</link>
		<comments>http://ppfeufer.de/erreichbarkeit-eines-web-servers-ueberpruefen/#comments</comments>
		<pubDate>Thu, 04 Jun 2009 16:13:22 +0000</pubDate>
		<dc:creator>H.-Peter Pfeufer</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[Erreichbarkeit]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[PHP-Funktion]]></category>
		<category><![CDATA[Webserver]]></category>

		<guid isPermaLink="false">http://ppfeufer.de/?p=190</guid>
		<description><![CDATA[Die nachfolgende Funktion soll prüfen ob ein Webserver erreichbar ist, oder nicht. Aber, wie kommt man auf so etwas? Dazu eine kleine Vorgeschichte &#8230; Ich habe hier in meinem Netzwerk mehrere Apache-Server und auch einige meiner Freunde betreiben solche Server, &#8230;</p><p class="read-more"><a class="more-link" href="http://ppfeufer.de/erreichbarkeit-eines-web-servers-ueberpruefen/">Weiterlesen <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Die nachfolgende Funktion soll prüfen ob ein Webserver erreichbar ist, oder nicht.</p>
<p>Aber, wie kommt man auf so etwas?<br />
Dazu eine kleine Vorgeschichte &#8230;</p>
<p>Ich habe hier in meinem Netzwerk mehrere Apache-Server und auch einige meiner Freunde betreiben solche Server, welche von außen erreichbar sind.</p>
<p>Nun ist es natürlich interessant zu sehen, welche dieser &#8220;Indianer&#8221; gerade aktiv sind &#8211; nach Möglichkeit schön übersichtlich auf einer Seite.<br />
<span id="more-190"></span><br />
Erweitert wurde das irgendwann mal um auch FTP-Server zu testen, aber genug der Worte, hier das Script.</p>
<div class="codecolorer-container php geshi syntaxhighlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:99%;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #009933; font-style: italic;">/**<br />
&nbsp;* Onlinecheck<br />
&nbsp;*<br />
&nbsp;* Funktion zur Überprüfung ob ein Host<br />
&nbsp;* auf einem bestimmten Port antwortet<br />
&nbsp;*<br />
&nbsp;* @param str $url &nbsp; &nbsp; &nbsp;- Zieladresse<br />
&nbsp;* @param str $proto &nbsp; &nbsp;- Protokoll (Dieses ist in der Variable als Array angelegt<br />
&nbsp;* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; und kann somit leicht erweitert werden)<br />
&nbsp;* @param str $good &nbsp; &nbsp; - Meldung wenn Host auf dem Port erreichbar<br />
&nbsp;* @param str $bad &nbsp; &nbsp; &nbsp;- Meldung wenn Host auf dem Port nicht erreichbar<br />
&nbsp;* @return str &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;- Rückgabe<br />
&nbsp;*<br />
&nbsp;* Beispiel:<br />
&nbsp;* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$check = onlineCheck('http://domain.tld', 'http', 'online', 'offline');<br />
&nbsp;* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo $check;<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">function</span> onlineCheck<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span> <span style="color: #000088;">$proto</span><span style="color: #339933;">,</span> <span style="color: #000088;">$good</span><span style="color: #339933;">,</span> <span style="color: #000088;">$bad</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$protokoll</span> &nbsp;<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><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'http'</span> &nbsp;<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'80'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'ftp'</span> &nbsp; <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'21'</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$url</span> &nbsp; &nbsp; &nbsp; &nbsp;<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;">'http://'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$url</span> &nbsp; &nbsp; &nbsp; &nbsp;<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;">'https://'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$fp</span> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><a href="http://www.php.net/fsockopen"><span style="color: #990000;">fsockopen</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span> <span style="color: #000088;">$protokoll</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$proto</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000088;">$bad</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/fclose"><span style="color: #990000;">fclose</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000088;">$good</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>
<p><strong>Achtung:<br />
<em>!!! Verwendung auf eigene Gefahr !!!</em></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://ppfeufer.de/erreichbarkeit-eines-web-servers-ueberpruefen/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
