<?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>Ruby on Rails Outsourcing &#187; cron</title>
	<atom:link href="http://www.ruby-on-rails-outsourcing.com/tag/cron/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ruby-on-rails-outsourcing.com</link>
	<description>Kirana Tama - Rails outsourcing company that helps small businesses minimize their costs</description>
	<lastBuildDate>Wed, 28 Jul 2010 05:58:20 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Database Backup Using Cron</title>
		<link>http://www.ruby-on-rails-outsourcing.com/2009/10/16/backup-databases-using-cron/</link>
		<comments>http://www.ruby-on-rails-outsourcing.com/2009/10/16/backup-databases-using-cron/#comments</comments>
		<pubDate>Fri, 16 Oct 2009 08:30:03 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Knowledge]]></category>
		<category><![CDATA[cron]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://www.ruby-on-rails-outsourcing.com/read/backup-databases-using-cron/</guid>
		<description><![CDATA[Sometime we want to backup our databases for a reasons, like prevent the database deleted, move database to another server, etc. its very bored if we do it every day manually and sometime we forget to backup the database. so what we want is backup the database with something automatically with the time we have [...]]]></description>
			<content:encoded><![CDATA[<p>Sometime we want to backup our databases for a reasons, like prevent the database deleted, move database to another server, etc. its very bored if we do it every day manually and sometime we forget to backup the database. so what we want is backup the database with something automatically with the time we have set, but how ? there is a lot of trick to do this, one of the is use shell script and cron.</p>
<p>ok, lets do it</p>
<p><strong>1. Create Shell Script</strong></p>
<pre class="brush: bash">$ vim backup.sh</pre>
<p>in vim editor put this script</p>
<pre class="brush: bash">
#!/bin/bash
#directory backup
DIR_NAME="/directory/backup/"
#file name we will be used
FILE_NAME="mysqlbackup_`date +%Y%m%d%H%M%S`.sql"
#Variable NUM is used for counted total of existing backup file
NUM=`ls $DIR_NAME -R -1 | grep mysqlbackup | wc -l`
#maximal file backup
MAX=2
#email notification
MAIL="mail@domain.com"
#if reach maximum backup
if [ $NUM -ge $MAX ]; then
#find out the first file in backup directory
for i in $( ls $DIR_NAME -R -1 | grep mysqlbackup  ); do
#deleting first file that we found
rm -rf $DIR_NAME$i
#get out from the loop
break
done
fi
#backup all database
mysqldump -uusername -ppassword --all-databases &gt; $DIR_NAME$FILE_NAME
#compressed dump file
gzip -rf $DIR_NAME$FILE_NAME
#create notification file
echo "New Backup file created : $DIR_NAME$FILE_NAME.gz" &gt; /tmp/mydbbackup.log
#send notification to email
mail -s "New Backup file created" $MAIL &lt; /tmp/mydbbackup.log
</pre>
<p>save file &#8220;esc&#8221; &#8220;:wq&#8221;</p>
<p>change the permission of file to execute file</p>
<pre class="brush: bash">$ chmod a+x backup.sh</pre>
<p>and test</p>
<pre class="brush: bash">$  ./backup.sh</pre>
<p>if this work you willl get a notification email, or for see the log see here :</p>
<pre class="brush: bash">$ cat /tmp/mydbbackup.log</pre>
<p>next step is set the cron</p>
<pre class="brush: bash">$ sudo vim /etc/crontab</pre>
<p>for example</p>
<pre class="brush: bash"># m h dom mon dow user  command
00 03 * * * username sh /directory/script/backup.sh #yourscript here</pre>
<p>descriptions :</p>
<p>m       = Minute<br />
h        = Hour<br />
dom   = Day of Month<br />
mon   = Month<br />
dow    = Day of Week<br />
user    = User<br />
command = command that will execute</p>
<p>in conditions above 00 03 * * * &#8230;. this script will be running every day at 3 AM.</p>
<div class="sexy-bookmarks sexy-bookmarks-expand sexy-bookmarks-bg-sexy"><ul class="socials"><li class="sexy-delicious"><a href="http://del.icio.us/post?url=http://www.ruby-on-rails-outsourcing.com/2009/10/16/backup-databases-using-cron/&amp;title=Database+Backup+Using+Cron" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a></li><li class="sexy-digg"><a href="http://digg.com/submit?phase=2&amp;url=http://www.ruby-on-rails-outsourcing.com/2009/10/16/backup-databases-using-cron/&amp;title=Database+Backup+Using+Cron" rel="nofollow" class="external" title="Digg this!">Digg this!</a></li><li class="sexy-stumbleupon"><a href="http://www.stumbleupon.com/submit?url=http://www.ruby-on-rails-outsourcing.com/2009/10/16/backup-databases-using-cron/&amp;title=Database+Backup+Using+Cron" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a></li><li class="sexy-technorati"><a href="http://technorati.com/faves?add=http://www.ruby-on-rails-outsourcing.com/2009/10/16/backup-databases-using-cron/" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a></li><li class="sexy-facebook"><a href="http://www.facebook.com/share.php?u=http://www.ruby-on-rails-outsourcing.com/2009/10/16/backup-databases-using-cron/&amp;t=Database+Backup+Using+Cron" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a></li><li class="sexy-twitter"><a href="http://twitter.com/home?status=Database+Backup+Using+Cron+-+http://b2l.me/9cqs+(via+@ptwgs)" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a></li><li class="sexy-mail"><a href="mailto:?subject=%22Database%20Backup%20Using%20Cron%22&amp;body=I%20thought%20this%20article%20might%20interest%20you.%0A%0A%22Sometime%20we%20want%20to%20backup%20our%20databases%20for%20a%20reasons%2C%20like%20prevent%20the%20database%20deleted%2C%20move%20database%20to%20another%20server%2C%20etc.%20its%20very%20bored%20if%20we%20do%20it%20every%20day%20manually%20and%20sometime%20we%20forget%20to%20backup%20the%20database.%20so%20what%20we%20want%20is%20backup%20the%20database%20with%20something%20automatically%20with%20the%20t%22%0A%0AYou%20can%20read%20the%20full%20article%20here%3A%20http://www.ruby-on-rails-outsourcing.com/2009/10/16/backup-databases-using-cron/" rel="nofollow" class="external" title="Email this to a friend?">Email this to a friend?</a></li><li class="sexy-comfeed"><a href="http://www.ruby-on-rails-outsourcing.com/2009/10/16/backup-databases-using-cron/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a></li><li class="sexy-linkedin"><a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://www.ruby-on-rails-outsourcing.com/2009/10/16/backup-databases-using-cron/&amp;title=Database+Backup+Using+Cron&amp;summary=Sometime%20we%20want%20to%20backup%20our%20databases%20for%20a%20reasons%2C%20like%20prevent%20the%20database%20deleted%2C%20move%20database%20to%20another%20server%2C%20etc.%20its%20very%20bored%20if%20we%20do%20it%20every%20day%20manually%20and%20sometime%20we%20forget%20to%20backup%20the%20database.%20so%20what%20we%20want%20is%20backup%20the%20database%20with%20something%20automatically%20with%20the%20t&amp;source=Ruby on Rails Outsourcing" rel="nofollow" class="external" title="Share this on Linkedin">Share this on Linkedin</a></li></ul><div style="clear:both;"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.ruby-on-rails-outsourcing.com/2009/10/16/backup-databases-using-cron/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
