Sunday, January 31, 2010

How To: Write A .BAT file To BackUp your SQL Express and/or SQL Standard Databases


For my first how to article I thought it appropriate to come out with a blog I have been searching for, for a very long time. So here it is: First off I want to come out and state one thing, I am not responsible for anything that might occur on your system and or your data being completely secure. Please use an enterprise solution for guaranteeing that your information is secure and backed up. This article is being written for any tech that needs a bit of help for DBA.

First off I will post code bits and then explain them after I post.

First code bit:

@ECHO OFF

@REM Created By: MyTekki
@REM Last Updated: 01-10-10
@REM v1.0.5.0 Final

for /f "tokens=1-3 delims=./-" %%f in ("%date%") do (
set _today=%%f-%%g-%%h
)

Now what this does is basically 2 things. it goes through the default "date" variable object and gathers pieces of information out of them such as the day, month, year. Then assigns (sets) a new variable known as the "_today" variable. So now that we have this bit of information we can start setting some of our other variables.

SET CMD_PATH=%SYSTEMROOT%\system32\cmd.exe
SET sqlServer=%1
SET sqlInstance=%2
SET saUsr=%3
SET saPass=%4
SET dbName=%5
SET tempBackLoc=%6
SET copyBackLoc=%7
SET logLoc=%8
SET deleteLocal=%9

Here we set a couple variables also we set to the cmd.exe (command prompt) file normally located on the system under C:\Windows\System32\cmd.exe we use %sytemroot% instead of %windir% both are built in variables but one is supported in older versions as well as newer. So now that is done we can look at the other variables that are set such as "sqlServer" as you can see they all have a %1, %2, %3, etc.. This is due to the fact that we are going to dynamically take these variables in making this object more useful for further use (multiple databses for example). So in this case if we were doing a scheduled task it would look something like the following:

{PathOfBATFile}\{BATFileName}.bat {sqlServer} {sqlInstance} {sqlUser} {sqlPass} {dbName} {tempBackUpLoc} {copyBackUpLoc} {logLoc} {deleteLocal--BooleanValue Y or N}


MySample:

C:\Temp\SQL_autoBackUp.bat MyTekkiSrv SQLExpress2005 sa test123 Data2010 C:\Temp\ R:\SQLBackUps\ C:\Temp\Log\ Y


!!!!! BIG NOTES !!!!!
Many of you are probably wondering why I don't just copy to the final locaiton. The reason why I do not do this is because of 2 reasons. If for some reason you can not connect to the final location? Secondly if security changes in the final location? Also SQL Server can not back up to a remote location it has to be a local physical drive not a network location.

Another BIG NOTE note:
I do not Check for spaces any where in this script maybe a later version I will add it in. But for now you can not use spaces in anything folder names instance names passwords

Now we do a couple checks:

@REM ---- VARIABLE CHECKS

IF NOT DEFINED dbName GOTO defineDatabase
IF NOT DEFINED tempBackLoc GOTO defineTempBackLoc
IF NOT DEFINED copyBackLoc GOTO defineCopyBackLoc
IF NOT DEFINED logLoc GOTO defineLogLoc


Here we check to see if that we have defined a database name, temp backup location, final location, and a log location

@REM --- END OF VAR CHECKS

@REM Checks to see if the Directory for the temp BackUp Exists
IF EXIST %tempBackLoc% GOTO CHKCOPYLOC

@REM Creates the directory for the temp BackUp
MKDIR %tempBackLoc%

:CHKCOPYLOC

@REM Checks to see if the Directory for the real BackUp Exists
IF EXIST %copyBackLoc% GOTO COPYFILE

@REM Creates the directory for the copied file.
MKDIR %copyBackLoc%
Now we are going to see if some of the variables exist. For example the back up location exists if not we will make the location for them.

:COPYFILE
@REM -- Makes the back up using the sqlcmd exe Found in the C:\Program Files\Microsoft SQL Server\90\Tools\Binn
%CMD_PATH% /c sqlcmd -S %sqlServer%\%sqlInstance% -U %saUsr% -P %saPass% -Q "BACKUP DATABASE [%dbName%] TO DISK = N'%tempBackLoc%%dbName%%_today%.bak' WITH NOFORMAT, NOINIT, NAME = N'%dbName%-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10"
COPY "%tempBackLoc%%dbName%%_today%.bak" "%copyBackLoc%%dbName%_%_today%.bak"

Now because the SQL Directory is within the the enviroment variables of the system (by default on install of sql) For those of you who do not know where this is I have posted how to check

--Windows XP and Server 2003. --

Right click My Computer > Properties > Advanced Tab > Evironment Variables > System Variables > Path (the one that includes C:\windows\system32) In here if you hit edit you will see at the end there is a location posted. c:\Program Files\Microsoft SQL Server\90\Tools\binn\

--Windows Vista and Server 2008 --

Right click Computer > Properties > Advanced System Settings (left Side) > Evironment Variables > System Variables > Path (the one that includes C:\windows\system32) In here if you hit edit you will see at the end there is a location posted. c:\Program Files\Microsoft SQL Server\90\Tools\binn\

Note: If you are using SQL Expres 2008 or SQL 2008 you will probable see the following:

C:\Program Files\Microsoft SQL Server\100\Tools\Binn

Now because this path is in the System Eviroment Variables I can just call sqlcmd and it will automatically know what file I am calling. So this we pass all the variables as paramaters that the sqlcmd requires to run the query of BACKUP. We name the file with the tempBackLoc then the dbName then the _today.

@REM -- checks to see if the file successfully got copied. If not it goes to the copy bad
IF NOT EXIST "%copyBackLoc%%dbName%_%_today%.bak" GOTO COPYBAD
We now check to see if the database actually backed up. If it did not copy correctly we exit and log error to the log location.

@REM if the copied Existed. Then we write a log letting the person know (this will also append to a previous log)
ECHO #################### %_today% -- executed SQL_autoBackUp.bat #################### >> %logLoc%%dbName%_log.txt
ECHO -- >> %logLoc%%dbName%_log.txt
ECHO Copied file %copyBackLoc%%dbName%_%_today%.bak successfully >> %logLoc%%dbName%_log.txt
ECHO -- >> %logLoc%%dbName%_log.txt
ECHO ####################################################################################### >> %logLoc%%dbName%_log.txt
ECHO ----- >> %logLoc%%dbName%_log.txt
GOTO DELETEORIG
Here we write that we executed the batch file and that it created the following files successfully

:COPYBAD

ECHO #################### %_today% -- executed SQL_autoBackUp.bat #################### >> %logLoc%%dbName%_log.txt
ECHO -- >> %logLoc%%dbName%_log.txt
ECHO Copy failed >> %logLoc%%dbName%_log.txt
ECHO -- >> %logLoc%%dbName%_log.txt
ECHO ####################################################################################### >> %logLoc%%dbName%_log.txt
ECHO ----- >> %logLoc%%dbName%_log.txt
EXIT
Else we write to the log file telling them that it did not back up successfully

:DELETEORIG
IF %deleteLocal% == Y DEL "%tempBackLoc%%dbName%%_today%.bak"
EXIT
Finally we check to see if they want us to delete the local copy of the file. (tempBackLoc) If so then we delete it

@REM --- Messages to users who do not define params

:defineDatabase
ECHO Please Define a Database Name within your params
PAUSE
EXIT

:defineTempBackLoc
ECHO Please define a temporary Back Up Location within your params
PAUSE
EXIT

:defineCopyBackLoc
ECHO Please define a final (copy) Back Up Location within your params
PAUSE
EXIT

:defineLogLoc
ECHO Please define a Log Location within your params
PAUSE
EXIT
and as the comment says all these are, are messages that if the user does not define certain things I can tell them.

Please note that the tempBackLoc (temp back up location) must have SQL Security rights. This is a local security group that gets created on the server. Normally looks like the following:

SQLServer2005MSSQLUser$COMPNAME$INSTANCENAME

In my case I have:

SQLServer2005MSSQLUser$MyTekkiSrv$SQLEXPRESS2005

If you wish for the entire source of this file. Please use the following link

http://mytekki.com/public/scripts/bat/SQLExpressBackUp.bat

Saturday, January 30, 2010

Google stops Supporting IE6

As it may come to no surprise to tech people google has announced it will stop supporting IE6. As of March 1st, 2010 google will no longer support IE6. Now you may think "well no big deal, people should be upgraded already." If this is your thoughts unfortunately although we would think most IT personnel would take care of this and have their client machines updated past IE6 you would be surprised at the number of users still using the browser. In just doing a quick google search on the current IE browser and versions, I found this graph which displays a shocking fact.

2009IE8IE7IE6FirefoxChromeSafariOpera
December13.5%12.8%10.9%46.4%9.8%3.6%2.3%
November13.3%13.3%11.1%47.0%8.5%3.8%2.3%
October12.8%14.1%10.6%47.5%8.0%3.8%2.3%
September12.2%15.3%12.1%46.6%7.1%3.6%2.2%
August10.6%15.1%13.6%47.4%7.0%3.3%2.1%
July9.1%15.9%14.4%47.9%6.5%3.3%2.1%
June7.1%18.7%14.9%47.3%6.0%3.1%2.1%
May5.2%21.3%14.5%47.7%5.5%3.0%2.2%
April3.5%23.2%15.4%47.1%4.9%3.0%2.2%
March1.4%24.9%17.0%46.5%4.2%3.1%2.3%
February0.8%25.4%17.4%46.4%4.0%3.0%2.2%
January0.6%25.7%18.5%45.5%3.9%3.0%2.3%

Although over the years/months IE6 has grown less and less popular. You can see last month still 10.9% of people still use the browser.