DOS Commands for the SQL Server DBA

Embed Size (px)

DESCRIPTION

DOS commands for SQL DBA

Citation preview

DOS Commands for the SQL Server DBABy:Jeremy Kadlec|Read Comments| Related Tips:More>DBA Best Practices

ProblemI have seen your recent tips (Introduction to Windows PowerShell for the SQL Server DBA Part 1,Introduction to Windows PowerShell for the SQL Server DBA Part 2andPowerShell Support in SQL Server 2008 - Invoke-Sqlcmd Cmdlet) on PowerShell and see value in them. Unfortunately, we do not have PowerShell installed on our SQL Servers and I am not sure when that is going to happen. Until that day arrives could you give me some insight into valuable DOS commands that I can leverage in administrative and research situations on my SQL Servers?SolutionPowerShell offers a great deal of value, but if it is not installed on your SQL Servers, then working through that process in some large organizations could take time. I would recommend moving down that path, but until that day comes, there are some DOS commands that can be valuable as well.In an earlier tip (Accessing the Windows File System from SQL Server) we outlined some options to access files and directories, so be sure to check that out. To build on those commands let's see how to do the following: File management Copy, cut, paste, move, delete, etc. files Create and remove directories Troubleshooting and Research Gather system information - Winmsd Validating a machine is online after a reboot - Ping Active Ports - netstat Network cards - ipconfig Free disk space - master..xp_fixeddrives Directory structure - master..xp_subdirsBefore we go too far down this path, using these commands in SQL Server is based on having the extended stored procedure master..xp_cmdshell enabled. In SQL Server 2000, in general this was accomplished by having SQL Server System Administrator rights. In SQL Server 2005, the master..xp_cmdshell extended stored procedure is enabled by using theSurface Area Configuration manager. In SQL Server 2008, enabling the master..xp_cmdshell extended stored procedure is accomplished by properlyconfiguring the correct facet.

File ManagementOne set of logic I have used time and time again in T-SQL administrative scripts is related to file management. Whether it is from an automated maintenance task or backup\recovery\log shipping, managing files has been a necessity.DOS File Management Commands

Copy and Paste Files- The xcopy command is handy when you need to copy and paste files from one directory to another. In the example below we are copying the Test.txt file in the C:\temp directory to the root of the C:\ drive. The /v switch verifies the file as it is written to the destination directory, the /q suppresses the display of messages, /y switch indicates that suppresses a prompt to overwrite an existing file and the /z switch copies the file in a mode where the file could be restarted. This command has additional options available than the copy command related to including subdirectories, archived files, verifying files, etc.

Cut and Paste Files- When it comes to cutting and pasting files, I prefer to use the move command. It is a simple command with a single switch to suppress any prompting followed by the source file and destination directory. Another alternative is to use the xcopy command listed above and then one of the delete commands listed below for more advanced deleting techniques.

Delete Files- Deleting files is imperative to ensure disk drives to not fill up. Although disk is cheap at some point it gets expensive to manage (people) and power the SAN\NAS\DASD devices.

Here are a few different tips that have already been written on the topic: Maintenance task to delete old SQL Server backup filesMaintenance task to delete old SQL Server backup filesBy:Greg Robidoux|Read Comments (3)| Related Tips:More>Maintenance

ProblemIn two previous tips we discussed how to automate full backups and transaction log backups by creating scripts that iterate through each of your databases and then execute the backup commands. A reader requested information about how to automate the process of deleting older backup files, so this tip explains one approach for getting rid of older backup files that are generated.SolutionIn previous tip we took a look at using Windows Scripting (Simple way to find errors in SQL Server error log) to read through the error log files and generate a slimmer error file with just the error messages and the related messages that were created at the same time. In this tip, we will also be using Windows Scripting to go through each of the subfolders to find files older than a certain timeframe and then delete these files.Here is the VBScript code. This was pulled together from a few different code snippets found on the internet.There are two parameters that need to be adjusted: iDaysOld - specify how many days old the files need to be for the script to delete them strPath - this is the folder where the backups are created.iDaysOld=7strPath="C:\BACKUP"

SetobjFSO=CreateObject("Scripting.FileSystemObject")SetobjFolder=objFSO.GetFolder(strPath)SetcolSubfolders=objFolder.SubfoldersSetcolFiles=objFolder.Files

ForEachobjFileincolFilesIfobjFile.DateLastModifiedExpress Edition

ProblemAs a lot of line-of-business applications are being built with SQL Server 2005 Express Edition as their backend database, we need to make sure that we backup the system and the user databases running on these instances. Unfortunately, SQL Server 2005 Express Edition does not come with SQL Agent which we would normally use to create a database maintenance plan to backup all the databases. How do we perform a backup of our system and user databases in SQL Server 2005 Express Edition similar to how we do it in other editions?SolutionWe can use a combination of VBScript and TSQL with Task Manager in Windows to automate the creation of user and system database backups in SQL Server 2005 Express Edition.Note: All files should be saved in folderE:\SQL_Backup\scripts. This can be changed, but this example is setup for this folder. If you save to a different folder you will need to update the scripts accordingly.Step 1 - Create the TSQL scriptThe TSQL script below generates a database backup similar to the formatting generated by the database maintenance plan, taking into account the date and time the backup files were generated. We save the script as a .sql file, E:\SQL_Backup\scripts\backupDB.sql, which we will call from a batch file using sqlcmd.DECLARE@dateStringCHAR(12),@dayStrCHAR(2),@monthStrCHAR(2),@hourStrCHAR(2),@minStrCHAR(2)--monthvariableIF(SELECTLEN(CAST(MONTH(GETDATE())ASCHAR(2))))=2SET@monthSTR=CAST(MONTH(GETDATE())ASCHAR(2))ELSESET@monthSTR='0'+CAST(MONTH(GETDATE())ASCHAR(2))--dayvariableIF(SELECTLEN(CAST(DAY(GETDATE())ASCHAR(2))))=2SET@daySTR=CAST(DAY(GETDATE())ASCHAR(2))ELSESET@daySTR='0'+CAST(DAY(GETDATE())ASCHAR(2))--hourvariableIF(SELECTLEN(DATEPART(hh,GETDATE())))=2SET@hourStr=CAST(DATEPART(hh,GETDATE())ASCHAR(2))ELSESET@hourStr='0'+CAST(DATEPART(hh,GETDATE())ASCHAR(2))--minutevariableIF(SELECTLEN(DATEPART(mi,GETDATE())))=2SET@minStr=CAST(DATEPART(mi,GETDATE())ASCHAR(2))ELSESET@minStr='0'+CAST(DATEPART(mi,GETDATE())ASCHAR(2))--namevariablebasedontimestampSET@dateString=CAST(YEAR(GETDATE())ASCHAR(4))+@monthStr+@dayStr+@hourStr+@minStr--=================================================================DECLARE@IDENTINT,@sqlVARCHAR(1000),@DBNAMEVARCHAR(200)SELECT@IDENT=MIN(database_id)FROMSYS.DATABASESWHERE[database_id]>0ANDNAMENOTIN('TEMPDB')WHILE@IDENTISNOTNULLBEGINSELECT@DBNAME=NAMEFROMSYS.DATABASESWHEREdatabase_id=@IDENT/*Changedisklocationhereasrequired*/SELECT@SQL='BACKUPDATABASE'+@DBNAME+'TODISK=''E:\SQL_Backup\'+@DBNAME+'_db_'+@dateString+'.BAK''WITHINIT'EXEC(@SQL)SELECT@IDENT=MIN(database_id)FROMSYS.DATABASESWHERE[database_id]>0ANDdatabase_id>@IDENTANDNAMENOTIN('TEMPDB')ENDStep 2 - Create the VBScript fileNext, we will need to create a VBScript file which will be responsible for cleaning up old copies of the database backups. The script also writes to a log file which records the database backup files. You do need to create an empty file named E:\SQL_Backup\scripts\LOG.txtto save a log of the deleted files. Also copy the below script and save as E:\SQL_Backup\scripts\deleteBAK.vbsOnErrorResumeNextDimfso,folder,files,sFolder,sFolderTargetSetfso=CreateObject("Scripting.FileSystemObject")

'locationofthedatabasebackupfilessFolder="E:\SQL_Backup\"

Setfolder=fso.GetFolder(sFolder)Setfiles=folder.Files

'usedforwritingtotextfile-generatereportondatabasebackupsdeletedConstForAppending=8

'youneedtocreateafoldernamedscriptsforeaseoffilemanagement&'afileinsideitnamedLOG.txtfordeleteactivityloggingSetobjFile=fso.OpenTextFile(sFolder&"\scripts\LOG.txt",ForAppending)

objFile.Write"================================================================"&VBCRLF&VBCRLFobjFile.Write"DATABASEBACKUPFILEREPORT"&VBCRLFobjFile.Write"DATE:"&FormatDateTime(Now(),1)&""&VBCRLFobjFile.Write"TIME:"&FormatDateTime(Now(),3)&""&VBCRLF&VBCRLFobjFile.Write"================================================================"&VBCRLF

'iteratethrueachofthefilesinthedatabasebackupfolderForEachitemFilesInfiles'retrievecompletepathoffilefortheDeleteFilemethodandtoextract'fileextensionusingtheGetExtensionNamemethoda=sFolder&itemFiles.Name

'retrievefileextensionb=fso.GetExtensionName(a)'checkifthefileextensionisBAKIfuCase(b)="BAK"Then

'checkifthedatabasebackupsareolderthan3daysIfDateDiff("d",itemFiles.DateCreated,Now())>=3Then

'DeleteanyoldBACKUPfilestocleanupfolderfso.DeleteFileaobjFile.WriteLine"BACKUPFILEDELETED:"&aEndIfEndIfNext

objFile.WriteLine"================================================================"&VBCRLF&VBCRLF

objFile.Close

SetobjFile=NothingSetfso=NothingSetfolder=NothingSetfiles=NothingStep 3 - Create the batch file that will call the TSQL script and the VBScript fileWe need to create the batch file which will call both the TSQL script and the VBScript file. The contents of the batch file will be a simple call to the sqlcmd.exe and a call to the VBScript file using either wscript.exe or simply calling the file. Save the file as E:\SQL_Backup\scripts\databaseBackup.cmdand save it in the scripts subfolderREM Run TSQL Script to backup databasessqlcmd -S-E -i"E:\SQL_Backup\scripts\backupDB.sql"REM Run database backup cleanup scriptE:\SQL_Backup\scripts\deleteBAK.vbsStep 4 - Create a task in Windows Task SchedulerCreate a daily task in Windows Task Scheduler that will call the batch file created in the previous step. This can be found in the Control Panel -> Scheduled Tasks or under Start -> All Programs -> Accessories -> System Tools -> Scheduled Tasks.Since we are using Windows authentication to run the TSQL script, use a Windows account that is a member of the db_backupoperator role of all the databases Launch "Scheduled Tasks" Click on Add Scheduled Task Browse to the "E:\SQL_Backup\scripts" folder and select databaseBackup.cmd Pick the frequency and time for the backups to run Lastly, enter a Windows account that has at least db_backupoperator role privileges for all of the databases See screenshots below

Next Steps Implement this solution on your SQL Server 2

Rename Files- Since we are talking about files, in many of the scripts I have written I have renamed files so it is easy to determine that they have been processed. At the most simple level, the rename command can be called with the current directory and file name followed by the new file name.

Create Directories- In the example code below, we are creating a new directory based on the current date with the mkdir DOS command.

-- 1 - Declare variablesDECLARE@CMD1varchar(8000)DECLARE@RestoreRootDirectoryvarchar(255)DECLARE@CurrentDatedatetimeDECLARE@CurrentNamevarchar(8)-- 2 - Initialize variablesSET@CMD1=''SET@RestoreRootDirectory='C:\Temp\'SET@CurrentDate=GETDATE()SELECT@CurrentName=CONVERT(varchar(8),@CurrentDate,112)-- 3a - Create the current directorySELECT@CMD1='EXEC master.dbo.xp_cmdshell '+char(39)+'mkdir '+@RestoreRootDirectory+@CurrentName+'\'+char(39)-- SELECT @CMD1EXEC(@CMD1)-- 3b - Test the error valueIF@@ERROR0BEGINRAISERROR('3a - Restore directory not created',16,1)RETURNEND

Remove Directories- In the example code below, we are removing a directory based on the the current date minus one.

-- 1 - Declare variablesDECLARE@CMD1varchar(8000)DECLARE@RestoreRootDirectoryvarchar(255)DECLARE@CurrentDatedatetimeDECLARE@PreviousNamevarchar(8)-- 2 - Initialize variablesSET@CMD1=''SET@RestoreRootDirectory='C:\Temp\'SET@CurrentDate=GETDATE()SELECT@PreviousName=CONVERT(varchar(8),@CurrentDate-1,112)-- 3a - Drop the previous directorySELECT@CMD1='EXEC master.dbo.xp_cmdshell '+char(39)+'rmdir '+@RestoreRootDirectory+@PreviousName+'\ /q'+char(39)-- SELECT @CMD1EXEC(@CMD1)-- 3b - Test the error valueIF@@ERROR0BEGINRAISERROR('3a - Restore directory not deleted',16,1)RETURNEND

Troubleshooting and ResearchDOS Troubleshooting and Research Commands

Gather system information- Winmsd can be invoked directly from the Start | Run command by typing in WINMSD. With this command you are able to get a basic set of information about the machine.

Validating a machine is online after a reboot- Although the ping command will not tell you when your application is operational after a reboot, it will let you know when Windows should be operational so you can begin to validate SQL Server has recovered and the application is operational.

Active Ports- The netstat -a command is valuable to provide the active TCP connections with the TCP and UDP ports the SQL Server is listening on.

Network Cards- Another command for general troubleshooting is the ipconfig command. In this example we are listing all of the information across each of the NICs, but this command does offer more options in terms of flushing DNS, releasing DNS, renewing DNS, etc.

Free disk space- The master..xp_fixeddrives extended stored procedure lists the free space in MB per disk drive. One situation I have run into with processes requiring a significant amount of disk space on a monthly basis is to check the free disk space prior to running the remainder of the code. It is frustrating to have a process run for an extended period of time only to find out sufficient disk space was not available in the first place. Check out theDetermine Free Disk Space in SQL Server with T-SQL Codetip for additional information.

EXECmaster.sys.xp_fixeddrivesGO

Directory structure- The master..xp_subdirs extended stored procedure provides the ability to capture the sub directories.

EXECmaster.sys.xp_subdirs'c:\'GO

Next Steps As you are faced with more situations where you need to access the Windows file system, be sure to see what options are available with extended stored procedures, DOS and PowerShell. As your organization has time, be sure to check out PowerShell to see how this new product can improve your overall infrastructure management. For more information about master..xp_cmdshell visit: Enabling xp_cmdshell in SQL Server 2005 Where is the Surface Area Configuration tool in SQL Server 2008 For more information about PowerSheel visit: Introduction to Windows PowerShell for the SQL Server DBA Part 1 Introduction to Windows PowerShell for the SQL Server DBA Part 2 PowerShell Support in SQL Server 2008 - Invoke-Sqlcmd Cmdlet