Thursday, 17 October 2013

Automatic FTP backups of SQL Server Express

Had a bit of a head-scratcher here, OK I know that in full versions of SQL Server backups can be managed (from what I understand) but how do you automate backups of SQL Server Express Versions?

The thing that I wanted (I don't ask for much!) was for cumulative, sequential backup volumes to be created on a daily basis and then pushed out to a remote FTP server which I know is backed up offsite as well (a double backup if you like)

So, what's the principle? Simple really, I run a scheduled task on the Window VPS each night when the database is quiet (normally after 9pm GMT) which is just a .bat file which does the following:

  1. Exports a backup of the named database (via a .sql command file)
  2. Moves it into a tmp directory
  3. Creates a directory for today's date
  4. Moves the temp file into that
  5. Dynamically writes an FTP script
  6. Executes the FTP command
  7. Tidies up the tmp files

Sounds easy enough, but the internal functions had me thinking a little bit. Let's walk through the process of the files needed:

SQL file export of whole database

Create a text file called export.sql and added this command into it

BACKUP DATABASE [nameofdatabase] TO  DISK = N'(pathtothedatabase\nameofdatabase.bak' WITH NOFORMAT, NOINIT,  NAME = N' EasyToRememberBackupName', SKIP, NOREWIND, NOUNLOAD,  STATS = 10
GO

nameofdatabase = yeah, the name of your database
pathtothedatabase = full directory structure where you want the backup to be saved to (e.g. D:\SQLBackups\Name\TMP
The TMP bit you need to create to save the initial .bak file into from the SQL dump
EasyToRememberBackupName = This is nothing more than a convenient name to call the backup and won't be used anywhere else anyway, but will be referenced if you restore a database from this in the future. We'll call it SQLbackup.bak for this exercise

Save that file into a dedicated directory (which will be referred to as VolumeRunningTheScriptFrom in the next stage) and create a new empty text file called backup.bat

Now the really fun bit, we've nicked a bit of internal DOS commands coupled it with SQL commands and chucked in a bit of FTP command line code as well to create this beast:

sqlcmd -S .\SQLSERVERLOCALNAME -i "VolumeRunningTheScriptFrom\export.sql"
set string=%date:~-7,2%%date:~-10,2%%date:~-4,4%
echo %string%
md VolumeRunningTheScriptFrom\%string%
VolumeRunningTheScriptFrom\%string%
cd..
move VolumeRunningTheScriptFrom\tmp\SQLbackup.bak VolumeRunningTheScriptFrom\%string%\SQLbackup.bak
cd VolumeRunningTheScriptFrom\%string%\
move VolumeRunningTheScriptFrom\%string%\SQLbackup.bak VolumeRunningTheScriptFrom\%string%\BACKUP_%string%.bak
cd "VolumeRunningTheScriptFrom"
@echo off
del script.txt
echo open FTPserveraddress>>script.txt
echo user FTPuseraccount>>script.txt
echo FTPpassword>>script.txt
echo cd RemoteDirectory>>script.txt
echo bin>>script.txt
echo put VolumeRunningTheScriptFrom\%string%\BACKUP_%string%.bak>>script.txt
echo bye>>script.txt
echo exit>>script.txt
echo cls>>script.txt

ftp -n -s:"VolumeRunningTheScriptFrom\script.txt"

Now that's a mixed bag of code to walk through, let's do it a line at a time:

sqlcmd -S .\SQLSERVERLOCALNAME -i "VolumeRunningTheScriptFrom\export.sql"
From the command line tell the local SQL engine to open the Local SQL Server (in this case SQLSERVERLOCALNAME they typically run as SQLEXPRESS or SQL2008EXPRESS whatever name you gave it when it was set up) and then the FULL path to the .sql file created earlier to execute.

set string=%date:~-7,2%%date:~-10,2%%date:~-4,4%
echo %string%


OK so using a lovely old DOS date function, we are setting a string to be today's date, so if it was 23/11/2013 then the date string would be

23112013

which will make the sequential nature of the backup a whole lot easier to name for uniqueness


md VolumeRunningTheScriptFrom\%string%
VolumeRunningTheScriptFrom\%string%
cd..


Let's make a directory within your backup folder to the value of the date string (23112013) and check its ok, then skip back up to the root of where we are running the script from

move VolumeRunningTheScriptFrom\TMP\SQLbackup.bak VolumeRunningTheScriptFrom\%string%\SQLbackup.bak

OK, so now we want to move the temporary SQL dump from the TMP folder into the newly created datestamp folder


cd VolumeRunningTheScriptFrom\%string%\
move VolumeRunningTheScriptFrom\%string%\SQLbackup.bak VolumeRunningTheScriptFrom\%string%\BACKUP_%string%.bak


So now we want to rename the backup to be specifically renamed to the datestamp. In my case we were running a number of backups into the offsite directory so we wanted to prefix the file with the database name, in this example BACKUP_

So we've basically moved back into the new backup folder for 23112013 and renamed ("move" in DOS terms) the SQLbackup.bak file to a new filename of BACKUP_23112013.bak

cd "VolumeRunningTheScriptFrom"
@echo off

Move back up the folder to the root

del script.txt

Now the funky FTP script to handle the date-specific file. The issue I had with this was that we wanted to keep FTP transferring files from the system from a scheduled task running each evening, but in the past we used a static FTP text file to call from, this was no use when using date-sequential strings, so we are basically creating the script.txt file on the fly, a cunning way of making sure we reference the correct backup for FTP offsite.

First thing though, delete yesterday's script!!

echo open FTPserveraddress>>script.txt
echo user FTPuseraccount>>script.txt
echo FTPpassword>>script.txt
echo cd RemoteDirectory>>script.txt
echo bin>>script.txt
echo put VolumeRunningTheScriptFrom\%string%\BACKUP_%string%.bak>>script.txt
echo bye>>script.txt
echo exit>>script.txt
echo cls>>script.txt


Now we have written all the values into the script.txt file, all that's left is for us to call the script via the system FTP command, silently...

ftp -n -s:"VolumeRunningTheScriptFrom\script.txt"

So, that's the basics in place for the backup, all that's needed now is to schedule this batch file through the Task Scheduler and we're good to run.

I always just run the batch file at the beginning to check it and there's a nice satisfying DOS window which runs through the 0-100% back up process and the FTP transfer which informs you that the files are being pushed offsite.

Please drop me a note if you want the source files, mark{at}1stclassmedia.co.uk

Friday, 28 June 2013

Finding a kitchen designer in Edinburgh

Edinburgh is the centre for many things, festivals for one, museums? Art Galleries aplenty. In fact when it comes to high class design houses, Edinburgh has a huge array to choose from. The same (bear with me here) can be said for Kitchen Designs. OK, you can get an economical kitchen from a large DIY outlet, it'll have factory quality finishes and will require replacing in 12 years or so, but will it be truly original? Enter the real experts, bespoke kitchen designers. Choosing from the number of Kitchen Designers in Edinburgh may seem like a daunting task initially. Opening up a phone book or doing a web search may lead you to the designer, however great designers come by recommendation and referral routes - there simply is nothing quite like a word of mouth recommendation for the best kitchen designer. Once you get an idea who to engage it becomes a whole lot easier to make an educated choice about who to use, particuarly one who will take into account full planning and layout and will stick to your budget.

So how do you find such a good Kitchen Designer, particularly a bespoke kitchen designer in Edinburgh? One of the best ways to find the right person who will provide first class guidance and design services is, ask. Ask everyone, your friends or family who have recently had their kitchens replaced and ask about their experiences. More importantly if you have found a decent looking kitchen designer, go them directly and get some insight into the planning process and get testimonials. Also sit down and have a coffee and a chat - if you believe you can work with the designer, all the better!

Choosing from the sheer volume of kitchen designers you find may actually be quite easy, prepare your list of services you are looking for in your kitchen designer, the type of kitchen design you want, and a budget outline for the work you can afford and present it to them, any designer worth their salt will try their best to make a decision whether you can afford them or if you are looking in the right place. As cost is the primary limitation for a lot of people, decent early on which area of the kitchen you want to focus on, if it's worktops or finishes or just the best quality appliances then be true to yourself, you're investing a lot of money in this, so demand the best.

Preparing your list and then taking the time to ask friends, family and the designer themselves about their experience can make finding a Kitchen Designer in Edinburgh so much easier than you probably expected!

Thursday, 20 June 2013

jQuery Window Handler (target="_blank")

I love jQuery. I make no bones about it - it's been a revelation for many developers who want to utilise code blocks as reusable elements and by golly it does speed up development both client-side and server-side!

However there are occasions when the traditional view on using "legacy" HTML is sometimes the only way we can perceive a solution. Take this example - we had a colleague who was at his wits end as he'd spent ages trying to use the good ole image map for a map of Scotland to make a clickable map for guest houses. All very well and easy to do. Problem was that the code used another jQuery handler to overlay a replacement map of the area clicked to make it look pretty which in turn played havoc with the internal target="_blank" directive.

After much swearing he dropped me a note to see if we could help. We took a look at the code and it all seemed fairly ok and we encountered the same problem, wtf was going on here, we couldn't use the traditional means above nor could we see how the href wasn't being looked at by the jQuery function.

After much looking and wondering (god bless Stack Overflow, by far one of the best troubleshooting websites ever) we pulled together a code block which may suit one or two others who have need of it.

Image Map Code:

<map name="mymap" id="map">
<area coords="6, 234, 17, 235, 35, 205, 37, 22, 44, 163, 56, 152, 76, 122, 82, 115, 80, 91, 66, 83, 28, 183, 25, 137, 22, 151, 7, 160, 7, 179, 16, 181" class="newWindow" href="#" rel="http://www.bbc.co.uk" id="12" shape="poly" alt="altname" title="title" />

</map>

We added the class to the area to make it a little easier to pass into the jQuery function

jQuery:

$('.newWindow').click(function(e){
e.preventDefault();
var url = $(this).attr('rel');
window.open(url, "_blank");
return false;
});

So, basically, breaking down the code the following happens:

$('.newWindow').click(function(e){ //render a new function each time the class newWindow is clicked

e.preventDefault(); //stop any links from working (note this could probably be left out, we kept it in for convenience)

var url = $(this).attr('rel'); //create a variable "url" from the rel value on the area

window.open(url, "_blank"); //good ole JS to handle the window event

return false; //stop other shit from happening

So, in summary, the little function allows image maps to open URLs in a new window by making the existing link a # (null) link and placing the URL into the rel tag. Grabbing the rel tag into the url variable we push that into the window.open event and voila the target="_blank" emulator kicks in.

There are probably numerous ways of doing this but I thought this may help some poor troubled soul out!

Scripts pooled from various links including

http://stackoverflow.com/questions/4813879/window-open-target-self-v-window-location-href

http://stackoverflow.com/questions/970388/jquery-disable-a-link