CFLib.org – Common Function Library Project

backupDatabase(dbsource)

Last updated February 21, 2011

Version: 1 | Requires: ColdFusion 8 | Library: DataManipulationLib

 
Rated 22 time(s). Average Rating: 5.0

Description:
Backs up a database to a zip file.

Return Values:
Returns a string.

Example:

view plain print about
<cfoutput>
<cfsetting showdebugoutput="no">
#BackupDatabase("darwanlns.com")#
</cfoutput>

Parameters:

Name Description Required
dbsource DNS. Yes

Full UDF Source:

view plain print about
<!---
 Backs up a database to a zip file.
 
 @param dbsource      DNS. (Required)
 @return Returns a string. 
 @author Darwan Leonardo Sitepu (dlns2001@yahoo.com) 
 @version 1, February 21, 2011 
--->

<cffunction name="backupDatabase" returnType="string" output="false">
    <cfargument name="dbsource" type="string" required="true">
    <cftry>
        <cfdbinfo datasource="#arguments.dbsource#" name="tables" type="tables" />
        <cfsilent>
            <cfset data = StructNew() />
            <cfloop query="tables">  <!--- grab all data from table --->  
                <cftry>
                    <cfset data[Table_Name] = StructNew() />  
                    <cfdbinfo datasource="#datasource#" table="#Table_Name#" name="qryTableFields" type="columns" />  
                    <cfset data[Table_Name].schema = qryTableFields />  
                    <cfquery name="getData" datasource="#datasource#" debug="no" cachedwithin=#CreateTimeSpan(0,0,0,10)# >SELECT * FROM [#Table_Name#]</cfquery>    
                    <cfset data[Table_Name].data = getData />            
                <cfcatch></cfcatch>
                </cftry>
            </cfloop><!--- Now serialize into one ginormous string --->
        </cfsilent>
        <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.xml" /></cfzip>
        <cfreturn "I retrieved #tables.recordCount# tables from datasource #datasource# and saved it to #zfile#.">
    <cfcatch>
        <cfreturn "Backup Database Is Failed..">
    </cfcatch>
    </cftry>
</cffunction>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Dave Anderson Dave Anderson added
iniToStruct
20 day(s) ago

Dave Anderson Dave Anderson added
deDupeArray
20 day(s) ago

Richard Richard added
dice
22 day(s) ago

Isaac Dealey Isaac Dealey added
getRelative
a while ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 22 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Raymond Camden highlightAndCrop
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson