I was faced with a peculiar request tonight. One of my hosting clients wanted me to write a script that would automatically email him his Access database each night. Even though our servers are backed up on a daily basis, he wanted a copy sent through to him. After a bit of searching I came across a fantastic CFC created by Nate Nielson,
http://www.webclarity.com, which creates ZIP files on the fly. As this guy's database is over 1MB in size I thought, perfect!
There are a couple of things that you'll need before implementing this tutorial though:
NB: by enabling createObject on a server a possible security implication raises its head in that users could access the Service Factory setting in the CF Adminstrator and view datasources, change passwords etc. For this reason some hosts may NOT allow you to use this function so check with them first!
OK. The CFC component itself is beautiful! Short, simple and quick. Everything a function should be so at this point it's only fair to offer congratulations to Nate for his creation. How you include the component in your page is totally up to you but I simply used
<CFINCLUDE> to include it. I won't go into detail as to how the CFC component actually works as Nate covers this in detail on both his site and in the download itself.
Create a new file called email_zip.cfm or whatever and add the code below:
<!---include the zip cfc--->
<cfinclude template="zip.cfc"
/>
<!---set the path to the file that you want to zip up, obviously change the file path to suit--->
<cfparam name="dbFile"
default="c:\domains\mydomain.com\db\myDatabase.mdb"
/>
<!---I wanted to give the zip file a unique name so all I'm doing here is creating a date stamp for the zip file. This is only one of many ways to do this --->
<cfset zipTS="#DateFormat(Now(),
'ddmmyy')#">
<!---set the path to the zip file you are going to create. Again the actual path will be up to you. The zip file is named and stamped with the date--->
<cfparam name="dbZip"
default="c:\domains\mydomain.com\wwwroot\dbBackup-#zipTS#.zip"
/>
<!---the following script does four simple actions to actually create the zip
file --->
<cfscript>
// create an instance of the zip component
zipper = createObject("component", "zip");
// start a new zip file. This just uses the path we set above
zipper.newZip("#dbZip#");
// add files to zip. Again this uses our file path above.
zipper.addFile("#dbFile#");
// create the zip file
zipper.createZip();
</cfscript>
<!---OK we now have our zip file sitting on the server so all we do is attach it to an email and off it
goes! --->
<cfmail to="foo@bar.com"
from="bar@foo.com"
subject="Database Backup for #DateFormat(Now(), ' d mmmm
yyyy')#">
Attached to this email is the latest backup for your database
<cfmailparam
file="#dbZip#" />
</cfmail>
And that's it! All there is to it. The email gets sent out to the recipient with a compressed zip file attached. To add a bit of automation I simple created a scheduled task to run each night to send the email automatically.
Again thanks to Nate for a great CFC and I hope this helps others as much as it helped me. It certainly got a big thumbs up from my client!
Submitted by: Phil Williams
Email: support<at>openmindhosting.com
Web: http://www.openmindhosting.com