<?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>gHacks technology news &#187; cat</title>
	<atom:link href="http://www.ghacks.net/tag/cat/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ghacks.net</link>
	<description>A technology blog covering software, mobile phones, gadgets, security, the Internet and other relevant areas.</description>
	<lastBuildDate>Wed, 25 Nov 2009 11:56:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Back up your Apache web directory and database with this simple script</title>
		<link>http://www.ghacks.net/2009/01/22/back-up-your-apache-web-directory-and-database-with-this-simple-script/</link>
		<comments>http://www.ghacks.net/2009/01/22/back-up-your-apache-web-directory-and-database-with-this-simple-script/#comments</comments>
		<pubDate>Thu, 22 Jan 2009 18:58:34 +0000</pubDate>
		<dc:creator>Jack Wallen</dc:creator>
				<category><![CDATA[Advice]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Tutorials Advanced]]></category>
		<category><![CDATA[Tutorials Basic]]></category>
		<category><![CDATA[automated backups]]></category>
		<category><![CDATA[backup-solution]]></category>
		<category><![CDATA[backups]]></category>
		<category><![CDATA[cat]]></category>
		<category><![CDATA[cron]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[LAMP]]></category>
		<category><![CDATA[tar]]></category>

		<guid isPermaLink="false">http://www.ghacks.net/?p=10060</guid>
		<description><![CDATA[I administer a lot of web sites. And all of these web sites need backup solutions. Since most of those web sites use LAMP servers it only made sense to set up a backup system using the available, included open source tools. It didn&#8217;t take long to create a solid backup system and, with the [...]]]></description>
			<content:encoded><![CDATA[<p>I administer a lot of web sites. And all of these web sites need backup solutions. Since most of those web sites use LAMP servers it only made sense to set up a backup system using the available, included open source tools. It didn&#8217;t take long to create a solid backup system and, with the help of cron, automate that system so that Apache&#8217;s document root and the website databases were backed up regularly and without user intervention.</p>
<p>The script made use of the following tools: date, cat, tar, mv, and rm. That&#8217;s it. The script will create backups with the date in the file name and then move the backups to a central location. Without further adieu, let&#8217;s get to the script.</p>
<p><span id="more-10060"></span><em>#! /bin/sh</em></p>
<p><em>TMP=&#8221;/tmp/&#8221;</em></p>
<p><em>#Format the date in YEAR-MO-DY format<br />
TODAY=`date +%F`</em></p>
<p><em># Check to see if there is a lastbackup file in /tmp, if not create it,<br />
# if so then set LAST equal to $TODAY<br />
if [ -f /tmp/lastbackup ]; then<br />
LAST=`cat /tmp/lastbackup`<br />
else<br />
LAST=$TODAY<br />
fi</em></p>
<p><em># Set the web directory backup name to the following<br />
WEB_FILENAME=&#8221;inc-&#8221;$TODAY&#8221;-web.tar.gz&#8221;</em></p>
<p><em># Set database backup name to the following<br />
DB_FILENAME=&#8221;inc-&#8221;$TODAY&#8221;-db.tar.gz&#8221;</em></p>
<p><em># this tars up my web directory into web.tar.gz tarball.<br />
/bin/tar -czf $TMP$WEB_FILENAME &#8211;after-date=$LAST /var/www/html</em></p>
<p><em># Move the web back to the backup directory<br />
/bin/mv $TMP$WEB_FILENAME /data</em></p>
<p><em># Remove web backup file from temp director<br />
rm $TMP$WEB_FILENAME</em></p>
<p><em># this tars up my database directory into $TODAY-db.tar.gz tarball.<br />
/bin/tar -czf $TMP$DB_FILENAME &#8211;after-date=$LAST /var/lib/mysql</em></p>
<p><em># Move the backup database to the backup directory<br />
/bin/mv $TMP$DB_FILENAME /data</em></p>
<p><em># Remove web backup file from temp directory<br />
rm $TMP$DB_FILENAME<br />
</em></p>
<p>What I wanted this to do is create daily backups and move the backups to the <strong>/data</strong> directory on the drive housing the server. These backups will be saved for one month. After the month is completed i have a second script that deletes the months backups prior to running the next backup (so there is always a backup to fall to). How I made use of this script is simple. I save the script (called <strong>backup.sh</strong>) in the root user directory and create a second script called <strong>rm_backups.sh</strong> that looks like this:</p>
<p><em>#! /bin/sh</em></p>
<p><em>rm /data/*gz</em></p>
<p>With these two files in place I create two cron entries. The first cron entry is for running the <strong>backup.sh</strong> script and looks like:</p>
<p>0 23 * * *     ~/backup.sh</p>
<p>The second cron entry is for running the <strong>rm_backups.sh</strong> script and looks like:</p>
<p>0 20 1 * *     ~/rm_backups.sh</p>
<p>Both of the above cron jobs are created as the root user.</p>
<p><strong>Final Thoughts</strong></p>
<p>Naturally this solution could be easily modified (using such tools rsync) to set up an offsite backup solution. What should be obvious is that creating a simple, flexible server backup system on Linux is easy. With the help of a little ingenuity, you can create your own automated backup service.</p>

	Tags: <a href="http://www.ghacks.net/tag/automated-backups/" title="automated backups" rel="tag">automated backups</a>, <a href="http://www.ghacks.net/tag/backup-solution/" title="backup-solution" rel="tag">backup-solution</a>, <a href="http://www.ghacks.net/tag/backups/" title="backups" rel="tag">backups</a>, <a href="http://www.ghacks.net/tag/cat/" title="cat" rel="tag">cat</a>, <a href="http://www.ghacks.net/tag/cron/" title="cron" rel="tag">cron</a>, <a href="http://www.ghacks.net/tag/date/" title="date" rel="tag">date</a>, <a href="http://www.ghacks.net/tag/lamp/" title="LAMP" rel="tag">LAMP</a>, <a href="http://www.ghacks.net/tag/linux/" title="Linux" rel="tag">Linux</a>, <a href="http://www.ghacks.net/tag/tar/" title="tar" rel="tag">tar</a><br />

	<h4>Related posts</h4>
	<ul class="st-related-posts">
	<li><a href="http://www.ghacks.net/2009/01/11/using-cron-to-automate-linux-tasks/" title="Using Cron to Automate Linux Tasks (January 11, 2009)">Using Cron to Automate Linux Tasks</a> (7)</li>
	<li><a href="http://www.ghacks.net/2009/02/15/quick-archiving-in-gnome/" title="Quick Archiving in GNOME (February 15, 2009)">Quick Archiving in GNOME</a> (1)</li>
	<li><a href="http://www.ghacks.net/2009/10/10/linux-back-in-time-backup-made-easy/" title="Linux Back In Time: Backup made easy (October 10, 2009)">Linux Back In Time: Backup made easy</a> (1)</li>
	<li><a href="http://www.ghacks.net/2009/02/19/installing-firefox-and-flash-from-source/" title="Installing Firefox and Flash From Source (February 19, 2009)">Installing Firefox and Flash From Source</a> (8)</li>
	<li><a href="http://www.ghacks.net/2008/12/20/install-phpmyadmin-for-easy-mysql-administration/" title="Install phpmyadmin for easy MySQL administration (December 20, 2008)">Install phpmyadmin for easy MySQL administration</a> (6)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://www.ghacks.net/2009/01/22/back-up-your-apache-web-directory-and-database-with-this-simple-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
