<?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>Gavin Pearce &#187; Code Library</title>
	<atom:link href="http://www.gavinpearce.com/blog/category/code-library/feed" rel="self" type="application/rss+xml" />
	<link>http://www.gavinpearce.com</link>
	<description>A Web Developer and Technical Architect</description>
	<lastBuildDate>Wed, 30 Jun 2010 16:53:21 +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>Check which ports are open on a Linux server</title>
		<link>http://www.gavinpearce.com/blog/check-which-ports-are-open-on-a-linux-server-179</link>
		<comments>http://www.gavinpearce.com/blog/check-which-ports-are-open-on-a-linux-server-179#comments</comments>
		<pubDate>Tue, 20 Apr 2010 12:00:38 +0000</pubDate>
		<dc:creator>Gavin Pearce</dc:creator>
				<category><![CDATA[Code Library]]></category>
		<category><![CDATA[Networking and Servers]]></category>

		<guid isPermaLink="false">http://www.gavinpearce.com/?p=179</guid>
		<description><![CDATA[It&#8217;s very easy to check which ports are listening on your server before IPTABLES and your Firewall takes affect.
SSH command:

netstat -lptu

However, getting a list of open ports after IPTABLES and your Firewall have done their job requires a Port Scan. To get the fairest results this needs to be done from a different machine (depending [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s very easy to check which ports are listening on your server <em>before</em> IPTABLES and your Firewall takes affect.</p>
<p>SSH command:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">netstat</span> <span style="color: #660033;">-lptu</span></pre></div></div>

<p>However, getting a list of open ports <em>after</em> IPTABLES and your Firewall have done their job requires a Port Scan. To get the fairest results this needs to be done from a different machine (depending on your IPTABLES/Firewall config, and what it is you&#8217;re trying to test).</p>
<p>SSH command:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">nmap</span> 192.0.32.10</pre></div></div>

<p>You might also find this useful.</p>
<p>SSH command:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">nmap</span> <span style="color: #660033;">-sS</span> <span style="color: #660033;">-p0-65535</span> <span style="color: #660033;">-P0</span> <span style="color: #660033;">-T4</span> --osscan_limit --osscan_guess --host_timeout 15m <span style="color: #660033;">--max-retries</span> <span style="color: #000000;">0</span> --min_parallelism <span style="color: #000000;">100</span> --max_parallelism <span style="color: #000000;">500</span> <span style="color: #660033;">-O</span> <span style="color: #660033;">-oX</span> <span style="color: #660033;">-V</span> 192.168.1.1</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.gavinpearce.com/blog/check-which-ports-are-open-on-a-linux-server-179/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Find closest location using longitude and latitude coordinates in PHP or MySQL</title>
		<link>http://www.gavinpearce.com/blog/find-closest-location-using-longitude-and-latitude-coordinates-in-php-or-mysql-149</link>
		<comments>http://www.gavinpearce.com/blog/find-closest-location-using-longitude-and-latitude-coordinates-in-php-or-mysql-149#comments</comments>
		<pubDate>Tue, 20 Apr 2010 09:00:08 +0000</pubDate>
		<dc:creator>Gavin Pearce</dc:creator>
				<category><![CDATA[Code Library]]></category>

		<guid isPermaLink="false">http://www.gavinpearce.com/?p=149</guid>
		<description><![CDATA[Although at first thought it might not seem it, it&#8217;s actually very simple to find the closest location in PHP or MySQL when given a range of of longitude and latitude coordinates. 
First, lets look at the maths. For those in the know, we&#8217;re essentially calculating the difference in the hypotenuse. Whichever result returns the [...]]]></description>
			<content:encoded><![CDATA[<p>Although at first thought it might not seem it, it&#8217;s actually very simple to find the closest location in PHP or MySQL when given a range of of longitude and latitude coordinates. </p>
<p>First, lets look at the maths. For those in the know, we&#8217;re essentially calculating the difference in the hypotenuse. Whichever result returns the lowest number from our data set will be the closest.</p>
<p>The number we need is the Square Root (&radic;) of:<br />
(firstLongitude-secondLongitude)*(firstLongitude-secondLongitude)<br />
+<br />
(firstlLatitude-secondLatitude)*(firstLatitude-secondLatitude)</p>
<p>Obviously, this number is useless if we have only two locations (and then we wouldn&#8217;t be interested in which was the closest anyway!), so we would loop this equation, changing the second lat/long for each location, to figure which is nearest.</p>
<p>In PHP we can do that like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sqrt</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$firstLongitude</span><span style="color: #339933;">-</span><span style="color: #000088;">$secondLongitude</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$firstLongitude</span><span style="color: #339933;">-</span><span style="color: #000088;">$secondLongitude</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$firstlLatitude</span><span style="color: #339933;">-</span><span style="color: #000088;">$secondLatitude</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$firstLatitude</span><span style="color: #339933;">-</span><span style="color: #000088;">$secondLatitude</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Remember, we&#8217;d need this to loop to be useful.</p>
<p>And in MySQL we&#8217;d select the data like this:</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">ORDER BY</span> <span style="color: #000099;">SQRT</span>
<span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#40;</span>fLongitude<span style="color: #CC0099;">-</span>sLongitude<span style="color: #FF00FF;">&#41;</span><span style="color: #CC0099;">*</span><span style="color: #FF00FF;">&#40;</span>fLongitude<span style="color: #CC0099;">-</span>sLongitude<span style="color: #FF00FF;">&#41;</span><span style="color: #FF00FF;">&#41;</span>
<span style="color: #CC0099;">+</span><span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#40;</span>fLatitude<span style="color: #CC0099;">-</span>sLatitude<span style="color: #FF00FF;">&#41;</span><span style="color: #CC0099;">*</span><span style="color: #FF00FF;">&#40;</span>fLatitude<span style="color: #CC0099;">-</span>sLatitude<span style="color: #FF00FF;">&#41;</span><span style="color: #FF00FF;">&#41;</span><span style="color: #FF00FF;">&#41;</span> 
<span style="color: #990099; font-weight: bold;">ASC</span> <span style="color: #990099; font-weight: bold;">LIMIT</span> <span style="color: #008080;">1</span><span style="color: #000033;">;</span></pre></div></div>

<p>Hopefully this will help you get the ground work in place to make a decent start.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gavinpearce.com/blog/find-closest-location-using-longitude-and-latitude-coordinates-in-php-or-mysql-149/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Display users IP quickly using a batch file</title>
		<link>http://www.gavinpearce.com/blog/display-users-ip-quickly-using-a-batch-file-258</link>
		<comments>http://www.gavinpearce.com/blog/display-users-ip-quickly-using-a-batch-file-258#comments</comments>
		<pubDate>Mon, 19 Apr 2010 12:00:18 +0000</pubDate>
		<dc:creator>Gavin Pearce</dc:creator>
				<category><![CDATA[Code Library]]></category>
		<category><![CDATA[Networking and Servers]]></category>

		<guid isPermaLink="false">http://www.gavinpearce.com/?p=258</guid>
		<description><![CDATA[It&#8217;s a very simple script, but maybe it might help you out. If you want to help a user find out their IP address quickly, create the following .bat file for them and get them to run it.

@echo on
ipconfig
pause

]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s a very simple script, but maybe it might help you out. If you want to help a user find out their IP address quickly, create the following .bat file for them and get them to run it.</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;">@echo <span style="color: #000080;">on</span>
ipconfig
pause</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.gavinpearce.com/blog/display-users-ip-quickly-using-a-batch-file-258/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Switch your computer network configuration between DHCP and Static quickly using a batch script</title>
		<link>http://www.gavinpearce.com/blog/switch-your-computer-network-configuration-between-dhcp-and-static-quickly-using-a-batch-script-252</link>
		<comments>http://www.gavinpearce.com/blog/switch-your-computer-network-configuration-between-dhcp-and-static-quickly-using-a-batch-script-252#comments</comments>
		<pubDate>Mon, 19 Apr 2010 09:00:26 +0000</pubDate>
		<dc:creator>Gavin Pearce</dc:creator>
				<category><![CDATA[Code Library]]></category>
		<category><![CDATA[Networking and Servers]]></category>

		<guid isPermaLink="false">http://www.gavinpearce.com/?p=252</guid>
		<description><![CDATA[I run my laptop on several different networks when testing/trialling things. Some of them have different subnets, some of them have DHCP, some of them don&#8217;t.
Constantly jumping into the Control Panel to change my network settings when I change subnet was taking awhile, and another NIC isn&#8217;t an option here, so I made these quick [...]]]></description>
			<content:encoded><![CDATA[<p>I run my laptop on several different networks when testing/trialling things. Some of them have different subnets, some of them have DHCP, some of them don&#8217;t.</p>
<p>Constantly jumping into the Control Panel to change my network settings when I change subnet was taking awhile, and another NIC isn&#8217;t an option here, so I made these quick .bat files that I can run when I need to update things</p>
<p>Set-IP-DHCP.bat:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;">netsh interface ip <span style="color: #000080;">set</span> address name=<span style="color: #800000;">&quot;Local Area Connection 2&quot;</span> dhcp
netsh interface ip <span style="color: #000080;">set</span> dns name=<span style="color: #800000;">&quot;Local Area Connection 2&quot;</span> dhcp</pre></div></div>

<p>Set-IP-Static.bat:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;">netsh interface ip <span style="color: #000080;">set</span> address name=<span style="color: #800000;">&quot;Local Area Connection 2&quot;</span> source=<span style="color: #000080;">static</span> addr=10.99.88.3 mask=255.255.255.240
netsh interface ip <span style="color: #000080;">set</span> dns name=<span style="color: #800000;">&quot;Local Area Connection 2&quot;</span> source=<span style="color: #000080;">static</span> addr=10.99.88.5  
netsh interface ip add dns name = <span style="color: #800000;">&quot;Local Area Connection 2&quot;</span> addr = 10.99.88.5</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.gavinpearce.com/blog/switch-your-computer-network-configuration-between-dhcp-and-static-quickly-using-a-batch-script-252/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Check for valid server name using URL Rewrite &#8211; else redirect.</title>
		<link>http://www.gavinpearce.com/blog/check-for-valid-server-name-using-url-rewrite-else-redirect-182</link>
		<comments>http://www.gavinpearce.com/blog/check-for-valid-server-name-using-url-rewrite-else-redirect-182#comments</comments>
		<pubDate>Mon, 19 Apr 2010 09:00:25 +0000</pubDate>
		<dc:creator>Gavin Pearce</dc:creator>
				<category><![CDATA[Code Library]]></category>

		<guid isPermaLink="false">http://www.gavinpearce.com/?p=182</guid>
		<description><![CDATA[A client of mine has an area of their website that can be accessed via several different domain names, all mapped to the same folder/file structure. (ignore the SEO problems that causes for now  ). 
They wanted part of this website to only be accessed by one domain, and create a 404 error otherwise. [...]]]></description>
			<content:encoded><![CDATA[<p>A client of mine has an area of their website that can be accessed via several different domain names, all mapped to the same folder/file structure. (ignore the SEO problems that causes for now <img src='http://www.gavinpearce.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> ). </p>
<p>They wanted part of this website to only be accessed by one domain, and create a 404 error otherwise. I achieved this with a quick URL Rewrite:</p>
<p>Apache Conf:</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;"><span style="color: #00007f;">RewriteCond</span> %{SERVER_NAME} !^websubdomain\.somedomain\.co\.uk
<span style="color: #00007f;">RewriteRule</span> (.*) - [R=<span style="color: #ff0000;">404</span>,L]</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.gavinpearce.com/blog/check-for-valid-server-name-using-url-rewrite-else-redirect-182/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Run a PHP script once a day using a Cron Job</title>
		<link>http://www.gavinpearce.com/blog/run-a-php-script-once-a-day-using-a-cron-job-191</link>
		<comments>http://www.gavinpearce.com/blog/run-a-php-script-once-a-day-using-a-cron-job-191#comments</comments>
		<pubDate>Sun, 18 Apr 2010 12:00:50 +0000</pubDate>
		<dc:creator>Gavin Pearce</dc:creator>
				<category><![CDATA[Code Library]]></category>
		<category><![CDATA[Networking and Servers]]></category>

		<guid isPermaLink="false">http://www.gavinpearce.com/?p=191</guid>
		<description><![CDATA[Just a quick reference guide, I use WGET added to cron to run a PHP script once a day at 7am.
Cron command:

0 7 * * *   wget -O - -q -t 1 http://www.example.net/path-to-file/file.php

or we can use Lynx if need-be.
Cron command:

0 7 * * * /usr/bin/lynx --source http://www.example.net/path-to-file/file.php

If the file isn&#8217;t on the net, [...]]]></description>
			<content:encoded><![CDATA[<p>Just a quick reference guide, I use WGET added to cron to run a PHP script once a day at 7am.</p>
<p>Cron command:</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;">7</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;">wget</span> <span style="color: #660033;">-O</span> - <span style="color: #660033;">-q</span> <span style="color: #660033;">-t</span> <span style="color: #000000;">1</span> http:<span style="color: #000000; font-weight: bold;">//</span>www.example.net<span style="color: #000000; font-weight: bold;">/</span>path-to-file<span style="color: #000000; font-weight: bold;">/</span>file.php</pre></div></div>

<p>or we can use Lynx if need-be.</p>
<p>Cron command:</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;">7</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>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">lynx</span> <span style="color: #660033;">--source</span> http:<span style="color: #000000; font-weight: bold;">//</span>www.example.net<span style="color: #000000; font-weight: bold;">/</span>path-to-file<span style="color: #000000; font-weight: bold;">/</span>file.php</pre></div></div>

<p>If the file isn&#8217;t on the net, you can also run it by calling PHP direct&#8230;</p>
<p>Cron command:</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;">7</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>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>php <span style="color: #660033;">-q</span> <span style="color: #000000; font-weight: bold;">/</span>htdocs<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>x.php</pre></div></div>

<p>Remember to check your path to PHP. Hopefully this will help someone looking for the same thing.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gavinpearce.com/blog/run-a-php-script-once-a-day-using-a-cron-job-191/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Block access to Subversion&#8217;s .SVN folders using URL Rewrite</title>
		<link>http://www.gavinpearce.com/blog/block-access-to-subversions-svn-folders-using-url-rewrite-193</link>
		<comments>http://www.gavinpearce.com/blog/block-access-to-subversions-svn-folders-using-url-rewrite-193#comments</comments>
		<pubDate>Sun, 18 Apr 2010 09:00:29 +0000</pubDate>
		<dc:creator>Gavin Pearce</dc:creator>
				<category><![CDATA[Code Library]]></category>
		<category><![CDATA[Networking and Servers]]></category>

		<guid isPermaLink="false">http://www.gavinpearce.com/?p=193</guid>
		<description><![CDATA[We have a couple of websites that are run under version control. However as an added bit of security, we don&#8217;t want anyone to be able to navigate to any of the .SVN folders that Subversion creates. We use the following rule in the main Apache Config/Rules file (HTTPD.CONF &#8211; in RedHat/CentOS at: /etc/httpd/) to [...]]]></description>
			<content:encoded><![CDATA[<p>We have a couple of websites that are run under version control. However as an added bit of security, we don&#8217;t want anyone to be able to navigate to any of the .SVN folders that Subversion creates. We use the following rule in the main Apache Config/Rules file (HTTPD.CONF &#8211; in RedHat/CentOS at: /etc/httpd/) to prevent access to .SVN folders across the whole server.</p>
<p>Apache Conf:</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;"><span style="color: #00007f;">RewriteEngine</span> <span style="color: #0000ff;">On</span>
<span style="color: #00007f;">RewriteRule</span> ^(.*/)?\.svn/ - [R=<span style="color: #ff0000;">404</span>,L]</pre></div></div>

<p>This will give you a 404 error, and show you the Server&#8217;s 404 page. If you set this rule locally in a Virtual Host, you can show the error doc for that custom virtual host.</p>
<p>You could of course modify this to return 403, or any error number you like.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gavinpearce.com/blog/block-access-to-subversions-svn-folders-using-url-rewrite-193/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Automatically backup full MySQL database to another server</title>
		<link>http://www.gavinpearce.com/blog/automatically-backup-full-mysql-database-to-another-server-213</link>
		<comments>http://www.gavinpearce.com/blog/automatically-backup-full-mysql-database-to-another-server-213#comments</comments>
		<pubDate>Fri, 16 Apr 2010 09:00:09 +0000</pubDate>
		<dc:creator>Gavin Pearce</dc:creator>
				<category><![CDATA[Code Library]]></category>
		<category><![CDATA[Networking and Servers]]></category>

		<guid isPermaLink="false">http://www.gavinpearce.com/?p=213</guid>
		<description><![CDATA[One of our servers automatically backs up the database to another server. I use the following bit of Bash scripting to achieve this:
backup-db:

#!/bin/bash
&#160;
#Author : Gavin Pearce @ GavinPearce.com
#Date : 2010-04-14
#Purpose : Backup all databases on one server to another server, and record to log file
&#160;
FILENAME=DB`date +%F`.sql
mysqldump -u root -pyourpassword --all-databases &#38;gt; /var/backups/$FILENAME
echo &#34;---------------------------------------------------------&#34;&#38;gt;&#38;gt; /var/backups/log
echo &#34;BACKUP [...]]]></description>
			<content:encoded><![CDATA[<p>One of our servers automatically backs up the database to another server. I use the following bit of Bash scripting to achieve this:</p>
<p>backup-db:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#Author : Gavin Pearce @ GavinPearce.com</span>
<span style="color: #666666; font-style: italic;">#Date : 2010-04-14</span>
<span style="color: #666666; font-style: italic;">#Purpose : Backup all databases on one server to another server, and record to log file</span>
&nbsp;
<span style="color: #007800;">FILENAME</span>=DB<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>F<span style="color: #000000; font-weight: bold;">`</span>.sql
mysqldump <span style="color: #660033;">-u</span> root <span style="color: #660033;">-pyourpassword</span> <span style="color: #660033;">--all-databases</span> <span style="color: #000000; font-weight: bold;">&amp;</span>gt; <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>backups<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$FILENAME</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;---------------------------------------------------------&quot;</span><span style="color: #000000; font-weight: bold;">&amp;</span>gt;<span style="color: #000000; font-weight: bold;">&amp;</span>gt; <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>backups<span style="color: #000000; font-weight: bold;">/</span>log
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;BACKUP JOB: &quot;</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>F-<span style="color: #000000; font-weight: bold;">%</span>H:<span style="color: #000000; font-weight: bold;">%</span>M:<span style="color: #000000; font-weight: bold;">%</span>S <span style="color: #000000; font-weight: bold;">`</span> <span style="color: #000000; font-weight: bold;">&amp;</span>gt;<span style="color: #000000; font-weight: bold;">&amp;</span>gt; <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>backups<span style="color: #000000; font-weight: bold;">/</span>log
rsync <span style="color: #660033;">-ave</span> <span style="color: #c20cb9; font-weight: bold;">ssh</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>backups<span style="color: #000000; font-weight: bold;">/</span> otherserveruser<span style="color: #000000; font-weight: bold;">@</span>192.168.1.1:<span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>backups2<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #000000; font-weight: bold;">&amp;</span>gt;<span style="color: #000000; font-weight: bold;">&amp;</span>gt; <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>backups<span style="color: #000000; font-weight: bold;">/</span>log
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;---------------------------------------------------------&quot;</span><span style="color: #000000; font-weight: bold;">&amp;</span>gt;<span style="color: #000000; font-weight: bold;">&amp;</span>gt; <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>backups<span style="color: #000000; font-weight: bold;">/</span>log</pre></div></div>

<p>To have RSYNC work, you will need to setup Key Based logins between the two servers. Or you could use SCP on this line &#8230;</p>
<p>You will also need to check:</p>
<ol>
<li>The username/password for MySQL;</li>
<li>The path you export the initial Database Export/Dump to;</li>
<li>The username of the user on the other server you are RSYNCing with;</li>
<li>The IP of the  other server.</li>
</ol>
<p>Then add this script to cron to run when you need it. Don&#8217;t forget to give it execute permissions.</p>
<p>Cron:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000;">1</span> <span style="color: #000000;">23</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> root <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>backup-db</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.gavinpearce.com/blog/automatically-backup-full-mysql-database-to-another-server-213/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Display a quote of the day when logging into Shell or SSH</title>
		<link>http://www.gavinpearce.com/blog/display-a-quote-of-the-day-moment-when-logging-into-shell-ssh-240</link>
		<comments>http://www.gavinpearce.com/blog/display-a-quote-of-the-day-moment-when-logging-into-shell-ssh-240#comments</comments>
		<pubDate>Thu, 15 Apr 2010 09:00:25 +0000</pubDate>
		<dc:creator>Gavin Pearce</dc:creator>
				<category><![CDATA[Code Library]]></category>
		<category><![CDATA[Networking and Servers]]></category>

		<guid isPermaLink="false">http://www.gavinpearce.com/?p=240</guid>
		<description><![CDATA[When logging into one of our servers, I have setup a little &#8220;quote of the day&#8221; (well more, quote of the moment), that randomly appears from a list of quotes we&#8217;ve given it. Everytime we login, we get a different quote. Pointless in all aspects, except it brightens up our day a little bit &#8211; [...]]]></description>
			<content:encoded><![CDATA[<p>When logging into one of our servers, I have setup a little &#8220;quote of the day&#8221; (well more, quote of the moment), that randomly appears from a list of quotes we&#8217;ve given it. Everytime we login, we get a different quote. Pointless in all aspects, except it brightens up our day a little bit &#8211; which is always a good thing!</p>
<p>Add the following to the bash profile file for each user you want to see the message. You can find the file in their home directory.</p>
<p>.bash_profile:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">fn</span>=<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>quotes.txt
<span style="color: #007800;">cnt</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">wc</span> <span style="color: #660033;">-l</span> <span style="color: #007800;">$fn</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #007800;">lns</span>=<span style="color: #800000;">${cnt:0:2}</span>
<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;">$lns</span>&quot;</span> = <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #007800;">rnd</span>=<span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">else</span>
  <span style="color: #7a0874; font-weight: bold;">let</span> <span style="color: #ff0000;">&quot;lns += 1&quot;</span>
  <span style="color: #007800;">rnd</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">expr</span> <span style="color: #007800;">$RANDOM</span> <span style="color: #000000; font-weight: bold;">%</span> <span style="color: #007800;">$lns</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
  <span style="color: #7a0874; font-weight: bold;">let</span> <span style="color: #ff0000;">&quot;rnd += 1&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">echo</span>   
<span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-n</span> <span style="color: #800000;">${rnd}</span>p <span style="color: #007800;">$fn</span>
<span style="color: #7a0874; font-weight: bold;">echo</span></pre></div></div>

<p>Create a file called quotes.txt, put each quote on it&#8217;s own line, and upload to a location to which all users have access. I use /home/quotes.txt.</p>
<p>Behold some great quotes as you login!  <img src='http://www.gavinpearce.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>I&#8217;ll leave you with a few of my favourite to get you start:</p>
<p><em>&#8220;The only people who have anything to fear from free software are those whose products are worth even less.&#8221;</em> &#8211; David Emery</p>
<p><em>&#8220;640K ought to be enough for anybody.&#8221;</em> &#8211; Bill Gates 1981</p>
<p><em>&#8220;Computers are getting smarter all the time.  Scientists tell us that soon they will be able to talk to us.  (And by &#8216;they&#8217;, I mean &#8216;computers&#8217;.  I doubt scientists will ever be able to talk to us.)&#8221;</em> &#8211; Dave Barry</p>
<p><em>&#8220;Computers are useless.  They can only give you answers.&#8221;</em> &#8211; Pablo Picasso</p>
<p><em>&#8220;Computer science education cannot make anybody an expert programmer any more than studying brushes and pigment can make somebody an expert painter.&#8221;</em> &#8211; Eric Raymond</p>
<p><em>Courage is what it takes to stand up and speak; courage is also what it takes to sit down and listen. </em> &#8211; Winston Churchill</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gavinpearce.com/blog/display-a-quote-of-the-day-moment-when-logging-into-shell-ssh-240/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Display a nice welcome message once logged into Shell/SSH</title>
		<link>http://www.gavinpearce.com/blog/display-a-nice-welcome-message-once-logged-into-shell-ssh-222</link>
		<comments>http://www.gavinpearce.com/blog/display-a-nice-welcome-message-once-logged-into-shell-ssh-222#comments</comments>
		<pubDate>Thu, 15 Apr 2010 09:00:13 +0000</pubDate>
		<dc:creator>Gavin Pearce</dc:creator>
				<category><![CDATA[Code Library]]></category>
		<category><![CDATA[Networking and Servers]]></category>

		<guid isPermaLink="false">http://www.gavinpearce.com/?p=222</guid>
		<description><![CDATA[When logging into one of our SSH servers, it greets you with Good Morning, Good Afternoon or Good Evening respectively. Largely pointless, but just one of those nice touches that makes our live slightly more interesting!
Here&#8217;s the script we use to do this:
welcome-msg:

#!/bin/bash
tod='morning'
hour=$&#40;date &#124; cut -c 12-13&#41;
if &#91; $hour -gt 11 &#93;; then
	tod='afternoon'; 
fi
if &#91; [...]]]></description>
			<content:encoded><![CDATA[<p>When logging into one of our SSH servers, it greets you with Good Morning, Good Afternoon or Good Evening respectively. Largely pointless, but just one of those nice touches that makes our live slightly more interesting!</p>
<p>Here&#8217;s the script we use to do this:</p>
<p>welcome-msg:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #007800;">tod</span>=<span style="color: #ff0000;">'morning'</span>
<span style="color: #007800;">hour</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">date</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> <span style="color: #660033;">-c</span> <span style="color: #000000;">12</span>-<span style="color: #000000;">13</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$hour</span> <span style="color: #660033;">-gt</span> <span style="color: #000000;">11</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
	<span style="color: #007800;">tod</span>=<span style="color: #ff0000;">'afternoon'</span>; 
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$hour</span> <span style="color: #660033;">-gt</span> <span style="color: #000000;">17</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
	<span style="color: #007800;">tod</span>=<span style="color: #ff0000;">'evening'</span>; 
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>Good&quot;</span> <span style="color: #007800;">$tod</span> <span style="color: #ff0000;">&quot;and welcome to SERVERNAME.<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;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>motd</pre></div></div>

<p>Upload and give it execute permissions.</p>
<p>Add to Cron:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><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: #000000; font-weight: bold;">*</span> root <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>welcome-msg</pre></div></div>

<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gavinpearce.com/blog/display-a-nice-welcome-message-once-logged-into-shell-ssh-222/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

