<?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>FinalFrag.be &#187; backup</title>
	<atom:link href="http://finalfrag.be/blog/tag/backup/feed/" rel="self" type="application/rss+xml" />
	<link>http://finalfrag.be/blog</link>
	<description>De persoonlijke website van Massimo 'FinalFrag' Mertens</description>
	<lastBuildDate>Tue, 04 Aug 2009 07:08:07 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Automatisch elke dag een&#160;backup</title>
		<link>http://finalfrag.be/blog/2009/06/automatisch-elke-dag-een-backup/</link>
		<comments>http://finalfrag.be/blog/2009/06/automatisch-elke-dag-een-backup/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 15:25:54 +0000</pubDate>
		<dc:creator>FinalFrag</dc:creator>
				<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[Programmeren]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://finalfrag.be/blog/?p=232</guid>
		<description><![CDATA[Backups zijn vandaag belangrijker dan ooit. Je bestanden staan waarschijnlijk wel ergens op een server die dagelijks backups neemt in jouw plaats. Je denkt dan nogal snel: &#8216;als er iets fout gaat, hebben ze toch een backup&#8217;. Toch blijft het altijd een heel gedoe wanneer je wil beroep doen op deze backups.
Wanneer je aan een [...]]]></description>
			<content:encoded><![CDATA[<p>Backups zijn vandaag belangrijker dan ooit. Je bestanden staan waarschijnlijk wel ergens op een server die dagelijks backups neemt in jouw plaats. Je denkt dan nogal snel: &#8216;als er iets fout gaat, hebben ze toch een backup&#8217;. Toch blijft het altijd een heel gedoe wanneer je wil beroep doen op deze backups.</p>
<p>Wanneer je aan een klein projectje werkt, is het makkelijk om zelf even elke dag na het programmeren snel even alle bestanden af te halen. Wanneer je echter aan grote projecten werkt, zoals ik nu aan <a target="_blank" href="http://www.spacetarion.com"  title="Spacetarion">Spacetarion</a>, kan het afhalen van alle bestanden al snel 5 tot 10 minuten duren. Daarom had ik nood aan een automatische oplossing.<span id="more-232"></span> Omdat ik deze best wel handig in gebruik vind, deel ik mijn opgedane kennis graag met jullie. Als fervente <a target="_blank" href="http://www.codeigniter.com"  title="CodeIgniter">CodeIgniter</a> fan, heb ik dit natuurlijk met behulp van dit framework gedaan.</p>
<h3>Wat heb ik allemaal nodig?</h3>
<p>Er zijn 2 dingen die nodig zijn om dit werkend te krijgen. Eerst en vooral moet je toegang hebben tot cronjobs. Dit hangt af van je webhost. De meeste fatsoenlijke hosts, zoals <a target="_blank" href="http://www.futureweb.be"  title="Futureweb">Futureweb</a> en <a target="_blank" href="http://www.combell.com"  title="Combell">Combell</a>, ondersteunen cronjobs. De cronjob zorgt ervoor dat het script automatisch elke 24 uur wordt uitgevoerd. Eigenlijk zal het script dus ook werken zonder cronjobs, maar dan moet je er zelf aan denken elke dag de controller een bezoekje te brengen.</p>
<p>Een andere voorwaarde is dat je je CodeIgniter system map niet buiten de web root hebt staan. <a href="http://finalfrag.be/blog/2009/06/ci-installeren-buiten-de-www-map/"  title="CI installeren buiten de www map">CI buiten de web root installeren</a> is veiliger omdat mensen geen toegang kunnen krijgen tot de bestanden in de installatie. Het nadeel is echter dat ook de &#8216;gebruiker&#8217; die PHP uitvoert (Apache) ook geen rechten heeft om bestanden buiten de root te lezen. Aangezien het script dat we gaan gebruiken van elk bestand de inhoud moet kunnen lezen, zal dit enkel werken indien CI volledig in de web root staat. Meestal is deze root de map &#8216;www&#8217; of &#8216;public_html&#8217;. Het kan zijn dat je bij jouw host enkel toegang hebt tot de web root. Indien je nergens een map &#8216;www&#8217; of &#8216;public_html&#8217; terugvind, is dit het geval en kan je het script gebruiken.</p>
<p>Noot: wanneer je je system map wel in de www map hebt staan, kan je deze nog altijd afschermen van het publiek door een klein .htaccess bestand in de system map te zetten. De inhoud van dit .htaccess bestand moet dan het volgende zijn:</p>

<div class="wp_syntax"><div class="code"><pre class="plain" style="font-family:monospace;">order allow,deny
deny from any</pre></div></div>

<p>Op die manier zal iedereen (behalve de Apache gebruiker) een 403 Forbidden error krijgen. Zo zijn je bestanden toch beschermd (wel minder goed dan gewoon alles buiten de www map te zetten natuurlijk) en kan je toch dit backup script gebruiken.</p>
<h3>De controller</h3>
<p>We maken in de map system/application/controllers een nieuw bestand aan met als naam backup.php. We voorzien in dit bestand 2 functies: 1 functie die elke dag een backup zal nemen en 1 functie die elke maandag (dit wordt bepaald in de cronjob) de laatste backup per e-mail zal versturen. De inhoud van Backup.php is als volgt:</p>

<div class="wp_syntax"><table><tr><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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> Backup <span style="color: #000000; font-weight: bold;">extends</span> Controller
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'PASSWORD'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'your_password'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'MAX_BACKUPS'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">14</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'BACKUP_DIR'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'/welke/map/je/wil/backuppen/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> index<span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</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: #000088;">$password</span> <span style="color: #339933;">!==</span> PASSWORD<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Get out'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">library</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'zip'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Bestanden backuppen</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">zip</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read_dir</span><span style="color: #009900;">&#40;</span>BACKUP_DIR<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Database backuppen</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbutil</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$backup</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbutil</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">backup</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'format'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'txt'</span><span style="color: #009900;">&#41;</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;">zip</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add_data</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'database.txt'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$backup</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Het zip bestand opslaan</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">zip</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">archive</span><span style="color: #009900;">&#40;</span>BACKUP_DIR <span style="color: #339933;">.</span> <span style="color: #0000ff;">'backups/'</span> <span style="color: #339933;">.</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'YmdHis'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' - '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">zip</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_file_num</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' files.zip'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Overbodige backups verwijderen</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">helper</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'file'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$files</span> <span style="color: #339933;">=</span> get_filenames<span style="color: #009900;">&#40;</span>BACKUP_DIR <span style="color: #339933;">.</span> <span style="color: #0000ff;">'backups/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Controleren of er al backups zijn</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$files</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: #990000;">sort</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$files</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #000088;">$delete</span> <span style="color: #339933;">=</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$files</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> MAX_BACKUPS<span style="color: #339933;">;</span>
			<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;">$delete</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: #990000;">unlink</span><span style="color: #009900;">&#40;</span>BACKUP_DIR <span style="color: #339933;">.</span> <span style="color: #0000ff;">'backups/'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$files</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</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: #000000; font-weight: bold;">function</span> mail_backup<span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</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: #000088;">$password</span> <span style="color: #339933;">!==</span> PASSWORD<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Get out'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">helper</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'file'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$files</span> <span style="color: #339933;">=</span> get_filenames<span style="color: #009900;">&#40;</span>BACKUP_DIR <span style="color: #339933;">.</span> <span style="color: #0000ff;">'backups/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Controleren of er al backups zijn</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$files</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: #990000;">rsort</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$files</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">// De laatste backup mailen</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">library</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'email'</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;">email</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">from</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'no-reply@example.com'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Backup system'</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;">email</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">to</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'you@example.com'</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;">email</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">subject</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Your backup'</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;">email</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">message</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Your backup'</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;">email</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">attach</span><span style="color: #009900;">&#40;</span>BACKUP_DIR <span style="color: #339933;">.</span> <span style="color: #0000ff;">'backups/'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$files</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</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;">email</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">send</span><span style="color: #009900;">&#40;</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></pre></td></tr></table></div>

<h3>De cronjob</h3>
<p>De cronjob zal ervoor zorgen dat de Backup controller elke 24 uur opgeroepen wordt. Deze controller zal dan een backup nemen en het bestand opslaan op de server. De manier om cronjobs in te stellen is bij elke host meestal anders. Ik kan dus niet stap voor stap uitleggen hoe je dit moet doen. Het commando dat moet worden uitgevoerd is echter altijd hetzelfde, en ziet er als volgt uit:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="plain" style="font-family:monospace;">0 0 * * * wget http://www.jouwsite.be/backup/index/your_password/ -O /dev/null</pre></td></tr></table></div>

<p>Deze cronjob zal elke dag om 0:00 worden uitgevoerd. Dit tijdstip kan je zelf aanpassen. De structuur van de argumenten is &#8216;minuut&#8217;, &#8216;uur&#8217;, &#8216;dag van de maand&#8217;, &#8216;maand&#8217;, &#8216;dag van de week&#8217;. Indien je 12 0 * * 1 gebruikt, zal er elke maandag om 12 uur &#8217;s middags een backup worden genomen.</p>
<p>Je moet natuurlijk ook de &#8216;jouwsite.be&#8217; vervangen door het domein waarop jouw site staat. De &#8216;your_password&#8217; op het einde is het wachtwoord dat je hebt gedefiniëerd in de controller. De &#8216;-O /dev/null&#8217; zorgt ervoor dat de cronjob geen logfile zal schrijven naar de website. Indien je dit weglaat zal er elke keer de cronjob wordt uitgevoerd een bestand in de root van je website bijkomen. Na verloop van tijd kan dit aantal flink oplopen. Het is dus een goed idee om deze logfiles weg te laten.</p>
<p>Je kan ook een aparte cronjob aanmaken om de laatste backup per e-mail te ontvangen. Persoonlijk neem ik elke dag een backup, en krijg ik elke maandag om 6 uur &#8217;s morgens graag de laatste backup in mijn inbox. Deze cronjob ziet er als volgt uit:</p>

<div class="wp_syntax"><div class="code"><pre class="plain" style="font-family:monospace;">0 6 * * 1 wget http://www.jouwsite.be/backup/mail_backup/your_password -O /dev/null</pre></div></div>

<h3>Het resultaat</h3>
<p>Het resultaat is best wel duidelijk. Je webserver zal nu elke dag een backup nemen van alle bestanden EN de volledige database. Ook zal je elke maandag een e-mail krijgen waarin de laatst genomen backup zal zitten. Ook zullen op de server maximaal 14 backups worden bijhouden, zo heb je iets of wat controle over hoeveel plaats de verschillende backups innemen.</p>
<p>Zoals je misschien al gemerkt hebt, zijn alle werken op deze website gelicenseerd onder een Creative Commons licentie. Wanneer je mijn werk wil gebruiken kan je dat vrij doen, zolang je mijn naam bij het werk zet en het niet dient voor commerciële doeleinden. Wanneer je mijn naam liever niet bij het werk wil zetten of de scripts toch wil gebruiken voor commerciële doeleinden, dien je eerst een schriftelijke toestemming aan mij te vragen.</p>
]]></content:encoded>
			<wfw:commentRss>http://finalfrag.be/blog/2009/06/automatisch-elke-dag-een-backup/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
