Posted in ColdFusion | Posted on 11-28-2007 | 5,487 views
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:
- Get a list of tables from a datasource.
- Select all rows from the table.
- Convert the query into WDDX.
- Zip the giant XML string and store the result.
The code for all this is incredibly simple:
2
3<cfdbinfo datasource="#datasource#" name="tables" type="tables">
This code simply uses the new cfdbinfo tag to query the tables from the datasource.
2<cfset data = structNew()>
I'm going to store all my queries in one struct.
2 <!--- grab all data from table --->
3 <cfquery name="getData" datasource="#datasource#">
4 select *
5 from #table_name#
6 </cfquery>
7
8 <cfset data[table_name] = getData>
9</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 *.
2<cfwddx action="cfml2wddx" input="#data#" output="packet">
Then we convert the structure into one XML packet.
2<cfset zfile = expandPath("./data.zip")>
3
4<!--- Now zip this baby up --->
5<cfzip action="zip" file="#zfile#" overwrite="true">
6 <cfzipparam content="#packet#" entrypath="data.packet">
7</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.
2I retrieved #tables.recordCount# tables from datasource #datasource# and saved it to #zfile#.
3</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:
2
3<cfdbinfo datasource="#datasource#" name="tables" type="tables">
4
5<!--- One struct to rule them all... --->
6<cfset data = structNew()>
7
8<cfloop query="tables">
9 <!--- grab all data from table --->
10 <cfquery name="getData" datasource="#datasource#">
11 select *
12 from #table_name#
13 </cfquery>
14
15 <cfset data[table_name] = getData>
16</cfloop>
17
18<!--- Now serialize into one ginormous string --->
19<cfwddx action="cfml2wddx" input="#data#" output="packet">
20
21<!--- file to store zip --->
22<cfset zfile = expandPath("./data.zip")>
23
24<!--- Now zip this baby up --->
25<cfzip action="zip" file="#zfile#" overwrite="true">
26 <cfzipparam content="#packet#" entrypath="data.packet">
27</cfzip>
28
29<cfoutput>
30I retrieved #tables.recordCount# tables from datasource #datasource# and saved it to #zfile#.
31</cfoutput>


I was thinking about writing something similar to this, only generating the create and insert statements required to extract a DB in a form that could easily be moved to a different server.
Am I the only one? (I know u are on a mac too Raymond)
Um, I'd thin it would probably time out. You could add sanity checks in there - checking record count, etc.
<cfset badlist = "foo,moo">
Then in your cfloop, you just do:
<cfif not listfind(badlist, table_name)>
Ie, if you don't find the table in the bad list, carry on.
IMO, a "skip-list" seems like a great way to compromise between incremental back-ups, and full back-ups for tables that never change.
Thanks Ray!
Now that said (and it works great), is it possible to to save that backup data into a file other than a wddx packet?
say like a text file perhaps that is zipped up instead?
Just Curious...
i m gonna read a bit more about json and wddx packets :)
good one
As an aside for bigger DB's what would you or anyone else recommend as a way of maybe splitting the file into "emailable" chunks?
a) First, you want to convert the WDDX packet back into native data. That is as simple as running cfwddx again, but with action=wddx2cfml.
b) This gives us a structure with table names as keys, and queries as data. For each key you would:
c) Loop over the query and do an insert of the data back into the table. Remember that CF gives you functions to inspect queries (columnlist) and get the individual cells, so this is something you can handle easily enough. :)
Thanks,
Aina.
how do we analyze mssql and backup mssql tables using coldfusion
Am going to write some code to rebuild the DB from the WDDX now - will post when I've got it - thanking you kindly sir!
[Add Comment] [Subscribe to Comments]