Welcome to WebProNews Breaking eBusiness and Search News
Advertise | Newsletter | Sitemap | News Feeds News Feed 
 WebProNews Search Part of the iEntry network iEntry inc. 

CFDBINFO & CFZIP For Quick Database Backups

Raymond Camden
Expert Author
Published: 2007-12-12

WebProNews RSS Feed


All major database products have tools to let you backup their databases.

MySQL makes it super simple with their command line tools. But what if you want to do it with ColdFusion? Doesn't everyone want to do everything with ColdFusion? I know I do! So let's look at a quick example.

My database backup code will work like so:

1. Get a list of tables from a datasource.
2. Select all rows from the table.
3. Convert the query into WDDX.
4. Zip the giant XML string and store the result.

The code for all this is incredibly simple:

<cfset datasource="blogdev">

<cfdbinfo datasource="#datasource#" name="tables" type="tables">

This code simply uses the new cfdbinfo tag to query the tables from the datasource.

<!--- One struct to rule them all... --->
<cfset data = structNew()>


I'm going to store all my queries in one struct.

<cfloop query="tables">
   <!--- grab all data from table --->
   <cfquery name="getData" datasource="#datasource#">
   select *
   from #table_name#
   </cfquery>

   <cfset data[table_name] = getData>
</cfloop>


Then I loop over each table and select *. Notice I store the query into the struct. By the way - the cfdbinfo tag also lets you get the columns from a database table. But since this is a "quickie" script, I don't mind using the select *.

<!--- Now serialize into one ginormous string --->
<cfwddx action="cfml2wddx" input="#data#" output="packet">


Then we convert the structure into one XML packet.

<!--- file to store zip --->
<cfset zfile = expandPath("./data.zip")>

<!--- Now zip this baby up --->
<cfzip action="zip" file="#zfile#" overwrite="true">
   <cfzipparam content="#packet#" entrypath="data.packet">
</cfzip>


Next I store the string into a zip using the new cfzip and cfzipparam tags. Notice how I feed the string data to the zip using cfzipparam. I don't have to store the text into a temporary file.

<cfoutput>
I retrieved #tables.recordCount# tables from datasource #datasource# and saved it to #zfile#.
</cfoutput>


The last thing I do is output a simple result message so you know how much data was backed up. Here is the complete source in one listing:

<cfset datasource="blogdev">

<cfdbinfo datasource="#datasource#" name="tables" type="tables">

<!--- One struct to rule them all... --->
<cfset data = structNew()>

<cfloop query="tables">
   <!--- grab all data from table --->
   <cfquery name="getData" datasource="#datasource#">
   select *
   from #table_name#
   </cfquery>

   <cfset data[table_name] = getData>
</cfloop>

<!--- Now serialize into one ginormous string --->
<cfwddx action="cfml2wddx" input="#data#" output="packet">

<!--- file to store zip --->
<cfset zfile = expandPath("./data.zip")>

<!--- Now zip this baby up --->
<cfzip action="zip" file="#zfile#" overwrite="true">
<cfzipparam content="#packet#" entrypath="data.packet">
</cfzip>

<cfoutput>
I retrieved #tables.recordCount# tables from datasource #datasource# and saved it to #zfile#.
</cfoutput>


Comments

View All Articles by Raymond Camden



Receive Our Daily Email of Breaking eBusiness News


About the Author:
Raymond Camden, ray@camdenfamily.com
http://ray.camdenfamily.com

Raymond Camden is Vice President of Technology for roundpeg, Inc. A long time ColdFusion user, Raymond has worked on numerous ColdFusion books and is the creator of many of the most popular ColdFusion community web sites. He is an Adobe Community Expert, user group manager, and the proud father of three little bundles of joy.

WebProNews RSS Feed

More Expert Articles Articles

Contact WebProNews
Advertisement





TOP NEWS

WebProBlog
The official blog of WebProNews.

Go to WebProBlog

Targeted Information for Business
WebProNews is part of the iEntry network

Internet Business: Marketing: Small Business:
WebProNews MarketingNewz SmallBusinessNewz
WebProWorld AdvertisingDay PromoteNews
EcommNewz SalesNewz EntrepreneurNewz

Software: Search Engines: Web Design:
WebMasterFree Jayde B2B DesignNewz
NetworkingFiles SearchZA FlashNewz
SecurityConfig SearchNewz WebSiteNotes

Developer: IT Management: Security:
DevWebPro ITManagement SecurityProNews
DevNewz SysAdminNews SecurityConfig
TheDevWeb NetworkingFiles NetworkNewz

The iEntry Network consists of over 100 web publications reaching millions of Internet Professionals. Contact us to advertise.
eBUSINESS RESOURCES






 Advertise | Contact Us | Corporate | Newsletter | Sitemap | Submit an Article | News Feeds
 WebProNews is an iEntry, Inc. ® publication - $line) { echo $line ; } ?> All Rights Reserved
WebProWorld
Ten most recent posts.


SearchBrains.com
NetworkingFiles
Featured Software


About WebProNews
WebProNews is the number one source for eBusiness News. Over 5 million eBusiness professionals read WebProNews and other iEntry business and tech publications.

WebProNews provides real-time coverage of internet business.

Free Email Newsletters:
WebProNews SearchNewz
WebProWorld DevWebPro
Marketing SecurityNews
Plus over 100 other newsletters!

Send me relevant info on products and services.


iEntry.com WebProWorld RSS Feed WebProWorld Contact WebProNews Print Version Email a friend Bookmark us SearchBrains.com SearchBrains RSS Feed