<?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>Web Development Blog &#187; cron</title>
	<atom:link href="http://www.web-development-blog.com/archives/tag/cron/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.web-development-blog.com</link>
	<description>Web development tutorials, SEO articles and PHP script resources</description>
	<lastBuildDate>Sun, 25 Jul 2010 14:38:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Create custom website backups using CRON</title>
		<link>http://www.web-development-blog.com/archives/create-custom-website-backups-using-cron/</link>
		<comments>http://www.web-development-blog.com/archives/create-custom-website-backups-using-cron/#comments</comments>
		<pubDate>Sat, 17 Oct 2009 13:09:54 +0000</pubDate>
		<dc:creator>Olaf</dc:creator>
				<category><![CDATA[Reviews]]></category>
		<category><![CDATA[Web Hosting]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[cron]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[hosting]]></category>
		<category><![CDATA[rsync]]></category>
		<category><![CDATA[webfaction]]></category>

		<guid isPermaLink="false">http://www.web-development-blog.com/?p=507</guid>
		<description><![CDATA[Several weeks ago I was looking for some alternative hosting for Media Temple and found WebFaction. While searching the net I found a few reviews and all of them are very good. Because their shared hosting package look different from others, I took an account. Why Webfaction? Webfaction is a 100% web hosting provider offering [...]]]></description>
			<content:encoded><![CDATA[<!--S-ButtonZ 1.1.5 Start--><!--S-ButtonZ 1.1.5 End--><p>Several weeks ago I was looking for some alternative hosting for Media Temple and found <a rel="nofollow" href="http://www.webfaction.com/?affiliate=finalwebsites">WebFaction</a>. While searching the net I found a few reviews and all of them are very good. Because their shared hosting package look different from others, I took an account.</p>
<h3>Why Webfaction?</h3>
<p><a rel="nofollow" href="http://www.webfaction.com/?affiliate=finalwebsites">Webfaction</a> is a 100% web hosting provider offering shared hosting plans and managed dedicated servers. Very special is that a shared hosting account has advanced features you didn&#8217;t find by other providers. The most important feature is that you get full SSH access to your Linux user directory and processes like PHP and HTTPD are executed as the same user. Having that kind of user accounts gives you much more possibilities like using a reserved amount of memory for a single process or the ability to setup new stuff in the user directory. I think WebFaction doesn&#8217;t provide a shared hosting account for a beginning webmaster, because their control panel is very basic and is mostly used to create new website, applications, email and database accounts. That will say that other functions like protecting directories or creating CRON jobs must be set via SSH and the command line. They create backups for all your website accounts and all databases. That is for the most users enough, but I like to have real time access to a copy to all my files.</p>
<h3>Creating backups for your websites and applications</h3>
<p>While this tutorial is based on my experience with the hosting account by WebFaction, it should work for most other Linux accounts or servers. All you need is full access to the user&#8217;s home directory. In the past I used the backup function provided by the DirectAdmin control panel for all my hosting. Those backup function (similar to other control-panel types) creates one zipped file from all the user data which is stored on a local or remote location. This type of backup will generate a huge server load if sites becoming bigger. In this tutorial we backup our databases to a local location and we are using <strong>rsync</strong> to backup our websites and databases.<span id="more-507"></span></p>
<h3>Creating backups from your database</h3>
<p>First we need one database user which has at least the rights to select an lock tables.</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">GRANT</span> <span style="color: #990099; font-weight: bold;">SELECT</span><span style="color: #000033;">,</span> <span style="color: #990099; font-weight: bold;">LOCK</span> <span style="color: #990099; font-weight: bold;">TABLES</span> <span style="color: #990099; font-weight: bold;">ON</span> <span style="color: #990099; font-weight: bold;">database</span>.<span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">TO</span> <span style="color: #008000;">'yourdumpuser'</span>@<span style="color: #008000;">'localhost'</span></pre></div></div>

<p>If you like to have a single user with full rights for easier database access, you can use this query as an alternative:</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">GRANT</span> <span style="color: #990099; font-weight: bold;">ALL</span> <span style="color: #990099; font-weight: bold;">ON</span> <span style="color: #990099; font-weight: bold;">database</span>.<span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">TO</span> <span style="color: #008000;">'yourdumpuser'</span>@<span style="color: #008000;">'localhost'</span></pre></div></div>

<p>Using the rights for the dump user we&#8217;re able to create database backups from the command line like:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mysqldump <span style="color: #660033;">-uyourdumpuser</span> <span style="color: #660033;">-hlocalhost</span> <span style="color: #660033;">-pyourdumppassword</span> database <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">gzip</span> <span style="color: #660033;">--best</span> <span style="color: #000000; font-weight: bold;">&gt;</span> yourmysqlbackup.gz</pre></div></div>

<p>This command creates an already gzip compressed database backup file. If you have have many databases you need a lot of commands it would be easier to run a bash script like this one:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
<span style="color: #007800;">DBUSER</span>=<span style="color: #ff0000;">&quot;yourdumpuser&quot;</span>
<span style="color: #007800;">DBHOST</span>=<span style="color: #ff0000;">&quot;localhost&quot;</span>
<span style="color: #007800;">DBPASS</span>=<span style="color: #ff0000;">&quot;yourdumppassword&quot;</span>
<span style="color: #007800;">BACKUPDIR</span>=<span style="color: #ff0000;">&quot;dbbackup&quot;</span>
<span style="color: #007800;">DBS</span>=<span style="color: #000000; font-weight: bold;">`</span>mysql -u<span style="color: #007800;">$DBUSER</span> -h<span style="color: #007800;">$DBHOST</span> -p<span style="color: #007800;">$DBPASS</span> <span style="color: #660033;">-e</span><span style="color: #ff0000;">&quot;show databases&quot;</span><span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #000000; font-weight: bold;">for</span> DATABASE <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #007800;">$DBS</span>
<span style="color: #000000; font-weight: bold;">do</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$DATABASE</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #ff0000;">&quot;Database&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
<span style="color: #007800;">FILENAME</span>=<span style="color: #007800;">$DATABASE</span>.gz
mysqldump -u<span style="color: #007800;">$DBUSER</span> -h<span style="color: #007800;">$DBHOST</span> -p<span style="color: #007800;">$DBPASS</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;">--best</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #007800;">$BACKUPDIR</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$FILENAME</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #000000; font-weight: bold;">done</span></pre></div></div>

<p>This small script will read all database names (where the user has access to) and creates files for all of them. Add this code into a file and save it under the name <strong>dbbackup.sh</strong> in your Linux home directory and create also the directory <strong>dbbackup</strong> for the files. The new script need to be executable, use chmod 775 to do that. Test the script with the following command within the directory where the script is located.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>dbbackup.sh</pre></div></div>

<h3>Backup your files using rsync</h3>
<p>Next we need a remote backup location to store our data on a safe place. This can be a second server you own or an external service. Important is that you can access that location via SSH. I&#8217;m using <a rel="nofollow" href="http://www.evbackup.com/">EVBackup</a> from ExaVault. They are not very expensive and they offer a great service. They have also a great <a rel="nofollow" href="http://www.evbackup.com/support-rsync-setup-unix-linux-bsd/">tutorial</a> on how-to configure your account to access their servers using rsync via SSH. </p>
<blockquote><p>If you&#8217;re looking for a backup solution to backup, synchronize and/or share your data, you should try <a rel="nofollow" href="https://spideroak.com/download/referral/46a33949edbafae7ee9610233f3e6464">SpiderOak</a>. They offer great backup solutions for multiple desktop PCs. Just try a free account with 2GB of space.</p></blockquote>
<p>After you setup you SSH connection between you server and your backup server we can start using rsync.</p>
<blockquote><p><strong>Note!</strong> We followed the instructions on from the EVBackup site and created a SSL key to access the remote location without providing a password. It&#8217;s <strong>strongly advised</strong> to do the same, otherwise the next steps will not work for you. You can use the instructions from their website for your own server if you change some parameters and values.</p></blockquote>
<p>The following example will copy all files within the <strong>webapps</strong> directory from your local server to the remote location (directory backup-1):</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">rsync <span style="color: #660033;">-avz</span> <span style="color: #660033;">--delete</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;ssh -i /home/username/backup/ssh_key&quot;</span> <span style="color: #ff0000;">&quot;/home/username/webapps&quot;</span> someuser<span style="color: #000000; font-weight: bold;">@</span>someuser.exavault.com:backup-<span style="color: #000000;">1</span></pre></div></div>

<p><strong>/home/username/backup/ssh_key</strong> is the location of the ssh key you have created before<br />
<strong>/home/username/webapps </strong>is the directory with files you want to backup<br />
<strong>someuser@someuser.exavault.com:backup-1</strong> is the remote location (in this example an account at exavault.com/EVBackup) where all files are stored</p>
<p>Note the parameter <strong>&#8211;delete</strong> will delete files on remote if the didn&#8217;t exist on the local location and <strong>-i</strong> will create a incremental backup. For additional information about the parameters used with <strong>rsync</strong> run this command via the command line: <code>rsync --help</code>.</p>
<h3>Run backups using CRON</h3>
<p>If you have multiple directories to backup you should group them in one bash script:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
rsync <span style="color: #660033;">-avz</span> <span style="color: #660033;">--delete</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;ssh -i /home/username/backup/ssh_key&quot;</span> <span style="color: #ff0000;">&quot;/home/username/webapps&quot;</span> someuser<span style="color: #000000; font-weight: bold;">@</span>someuser.exavault.com:backup-<span style="color: #000000;">1</span>
rsync <span style="color: #660033;">-avz</span> <span style="color: #660033;">--delete</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;ssh -i /home/username/backup/ssh_key&quot;</span> <span style="color: #ff0000;">&quot;/home/username/dbbackup&quot;</span> someuser<span style="color: #000000; font-weight: bold;">@</span>someuser.exavault.com:backup-<span style="color: #000000;">1</span></pre></div></div>

<p>You can add as many <strong>rsync</strong> commands you need in that script, save it in the user&#8217;s home directory with the name <strong>data-backup.ssh</strong>.</p>
<ul>
<li>We need to run the database backup script to store the current data into the <strong>dbbackup</strong> directory</li>
<li>If this backup is finished we start the rsync backup script ( give the first CRON job enough time)</li>
</ul>
<p>An alternative is to merge both bash scripts, but this is not part of this tutorial. Our entries for the crontab file are (use this command: EDITOR=nano crontab -e)</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000;">0</span> <span style="color: #000000;">10</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: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>username<span style="color: #000000; font-weight: bold;">/</span>dbbackup.sh
<span style="color: #000000;">0</span> <span style="color: #000000;">11</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: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>username<span style="color: #000000; font-weight: bold;">/</span>data-backup.sh</pre></div></div>

<p>These CRON jobs will run the first script at 10:00AM and the second at 11:00AM.</p>
<p>That&#8217;s all to backup your websites and databases using rsync and CRON. If you like to try Webfaction use <strong>finalwebsites</strong> as promo code, they offer a 60 days money-back guarantee.<strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://www.web-development-blog.com/archives/create-custom-backups-from-your-website-using-curl/" rel="bookmark" title="July 30, 2008">Create custom backups from your website using cURL</a></li>
<li><a href="http://www.web-development-blog.com/archives/how-to-choose-a-wordpress-hosting-provider/" rel="bookmark" title="January 24, 2010">How-to choose a WordPress Hosting Provider</a></li>
<li><a href="http://www.web-development-blog.com/archives/uploading-files-with-curl/" rel="bookmark" title="September 8, 2006">Uploading files with cURL?</a></li>
</ul>
<p><!-- Similar Posts took 3.393 ms --></p>
<div style="clear:both;">&nbsp;</div>]]></content:encoded>
			<wfw:commentRss>http://www.web-development-blog.com/archives/create-custom-website-backups-using-cron/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->