<?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>Aditya Kothadiya&#039;s Blog &#187; script</title>
	<atom:link href="http://adityakothadiya.com/tag/script/feed/" rel="self" type="application/rss+xml" />
	<link>http://adityakothadiya.com</link>
	<description>Entrepreneurship, programming, design, productivity, books, philosophy and more.</description>
	<lastBuildDate>Sun, 16 May 2010 16:20:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Automating Your MySQL Database Backup On Media Temple’s Grid-Server</title>
		<link>http://adityakothadiya.com/2009/05/automating-your-mysql-database-backup-on-media-temple%e2%80%99s-grid-server/</link>
		<comments>http://adityakothadiya.com/2009/05/automating-your-mysql-database-backup-on-media-temple%e2%80%99s-grid-server/#comments</comments>
		<pubDate>Mon, 25 May 2009 18:26:00 +0000</pubDate>
		<dc:creator>Aditya</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://adityakothadiya.com/?p=447</guid>
		<description><![CDATA[Let me admit this &#8211; I did not backup the Tweeght&#8216;s database until today. Somehow I kept procrastinating it for no reason. Since Tweeght is growing consistently, it was crucial for me to backup its database on daily basis and prepare myself from loosing all the crucial data in some sort of catastrophic event. I [...]]]></description>
			<content:encoded><![CDATA[<p>Let me admit this &#8211; I did not backup the <a href="http://tweeght.com" target="_blank">Tweeght</a>&#8216;s database until today. Somehow I kept procrastinating it for no reason. Since Tweeght is growing consistently, it was crucial for me to backup its database on daily basis and prepare myself from loosing all the crucial data in some sort of catastrophic event.</p>
<p>I use Media Temple&#8217;s Grid-Server for Tweeght. MT has good knowledge base article on &#8211; <a href="http://kb.mediatemple.net/questions/129/How+can+I+Backup+and+Restore+a+MySQL+database%3F" target="_blank">How can I Backup and Restore a MySQL database?</a>.</p>
<p>But this article just mentions about how to backup your database for once. I wanted to automate this backup process on daily basis. So I Googled a little-bit, and came across <a href="http://www.websitepublisher.net/article/mysql-backup/" target="_blank">this wonderful script</a>. So I took that script, updated it for MT&#8217;s Grid-Server infrastructure, and now I&#8217;m all set within 20 minutes to take daily backups of my crucial databases.</p>
<p><strong>Here are the steps I followed:</strong></p>
<p>1. Go to /data directory in your account. I preferred this over /domains directory because this is accessible only by you and not by public. You don&#8217;t want the public to accidentally access dump of your database.</p>
<p>2. Create /db_backup directory. Go to /db_backup directory and create three new directories &#8211; /scripts , /daily and /recent.</p>
<p>3. Go to /scripts directory and open up your favorite editor. Copy and paste following code -</p>
<pre class="brush: plain;">
#!/bin/bash
# Set a value that we can use for a datestamp
DATE=`date +%Y-%m-%d`
# Our Base backup directory
BASEBACKUP=&quot;/home/XXXXX/data/db_backup/daily&quot;

for DATABASE in `cat /home/XXXXX/data/db_backup/scripts/db_list.txt`
do
# This is where we throw our backups.
FILEDIR=&quot;$BASEBACKUP/$DATABASE&quot;

# Test to see if our backup directory exists.
# If not, create it.
if [ ! -d $FILEDIR ]
then
mkdir -p $FILEDIR
fi

echo -n &quot;Exporting database:  $DATABASE&quot;
mysqldump --add-drop-table -h internal-db.sXXXXX.gridserver.com -udbXXXXX -pPASSWORD $DATABASE | gzip -c -9 &gt; $FILEDIR/$DATABASE-$DATE.sql.gz
echo &quot;      ......[ Done Exporting to local backup, now exporting for remote backup] &quot;
cp $FILEDIR/$DATABASE-$DATE.sql.gz /home/XXXXX/data/db_backup/recent/$DATABASE.sql.gz
echo &quot;      .......[Done]&quot;
done

# AutoPrune our backups.  This will find all files
# that are &quot;MaxFileAge&quot; days old and delete them.
MaxFileAge=4
find $BASEBACKUP -name '*.gz' -type f -mtime +$MaxFileAge -exec rm -f {} \;
</pre>
<p>4. Change few things in above code -</p>
<ul>
<li>Replace all the instances of &#8216;XXXXX&#8217; with your gridserver account number.</li>
<li>Replace &#8216;PASSWORD&#8217; with your SSH password</li>
</ul>
<p>5. Now save this file as something like &#8216;db_backup.sh&#8217;</p>
<p>6. Open up your editor one more time, and write the names of databases you want to back up on each single line -</p>
<pre class="brush: plain;">
dbXXXXX_proj1
dbXXXXX_proj2
</pre>
<p>7. Now save this file as something like &#8216;db_list.txt&#8217;</p>
<p>8. Basically, the script pulls a list of databases to be backed up from a file called db_list.txt, this file takes 1 database name per line. It then exports the database and compresses, then saves it with a date-stamped filename to a directory called ../daily/&lt;db_name&gt;/.</p>
<p>Then, the script copies a non-date-stamped filename to a directory called ../recent/, thus overwriting the previous file stored there.</p>
<p>Finally it goes through the daily directory structure and deletes any files older than 4 days.</p>
<p>9. Now you can test your backup script by running following command -</p>
<pre class="brush: plain;">
./db_backup.sh
</pre>
<p>10. The script will echo out some progress messages. If no error message, then hopefully your databases should be backed up.</p>
<p>To see if it was successful execution, go to ../daily directory. You will see directories created with &lt;db_name&gt; names. Under these directories, you&#8217;ll see compressed version of sql dumps. Similarly, go to ../recent directory, and you&#8217;ll see the similar compressed version of sql dump.<br />
To automate this backup process, go to your MediaTemple&#8217;s admin section of your primary domain and click the &#8220;Cron Jobs&#8221; link.</p>
<p>11. Click on the &#8216;Add a new cron job&#8217; button.</p>
<p>12. Add your email address in the &#8220;Notification Email&#8221; field. This will send you emails whenever the cron job runs and will let you know if it was successful or not.</p>
<p>13. Also add path of your script in the &#8220;Command or script to execute&#8221; field. Enter: /home/XXXXX/data/db_backup/scripts/db_backup.sh. Again, replace &#8216;XXXXX&#8217; with your gridserver account number.</p>
<p>14. On the same page, you can specify how often you&#8217;d like your script to run. This entire section is up to you requirements. I&#8217;ve set mine to run daily at 3:00 AM when traffic on my site is slower.</p>
<p>15. Click Save and you&#8217;re all set to backup your databases on daily basis!</p>
<p>I hope this will be useful to you. If you see any issues, please let me know in the comment section.</p>
]]></content:encoded>
			<wfw:commentRss>http://adityakothadiya.com/2009/05/automating-your-mysql-database-backup-on-media-temple%e2%80%99s-grid-server/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>
