6
Xp_SQLMaint If you examine any jobs created by the maintenance plan, you will see that the job steps consist of executing the xp_sqlmaint extended stored procedure. For instance, the database backup maintenance plan shown in Setting Up Database Maintenance Plans has the following job step: view source print ? 1.EXECUTE master.dbo.xp_sqlmaint N'-PlanID DE5DE6BF-7AD9-4A10-8086- A9C4BF5674A4 -WriteHistory -VrfyBackup -BkUpOnlyIfClean - CkDB -BkUpMedia DISK -BkUpDB -UseDefDir -BkExt "BAK"' This shows us that the job created by the maintenance plan backs up the database to disk, using the default backup directory appending the "BAK" extension to the backup file after performing integrity checks (-CkDB). You should become familiar with the syntax of xp_sqlmaint as you might have to run it manually at some point. XP_SQLMAINT in turn runs the SQLMaint.exe command line utility, which can be found in the SQL Server installation directory in MSSQL\Binn folder. Note that you must be change the directory to the folder where SQLMAINT.exe is located in order to execute SQLMAINT from the command prompt. Parameter Explanation -? Returns syntax of SQLMAINT -S server_name[\ instance_name] SQL Server name or instance name if using a named instance of SQL Server -U login_ID SQL Server login to be used for connecting to SQL Server with SQLMAINT. If not specified SQLMAINT attempts to connect using Windows authentication. Double quotes must be used if login contains special characters. -P password Password for connecting to SQL Server with the specified login -D database_name Name of the database to be maintained by SQLMAINT

xp_cmdshell

  • Upload
    wasimss

  • View
    111

  • Download
    1

Embed Size (px)

DESCRIPTION

Windows

Citation preview

Page 1: xp_cmdshell

Xp_SQLMaintIf you examine any jobs created by the maintenance plan, you will see that the job steps consist of executing the xp_sqlmaint extended stored procedure. For instance, the database backup maintenance plan shown in Setting Up Database Maintenance Plans has the following job step: view source

print ? 1.EXECUTE master.dbo.xp_sqlmaint   N'-PlanID DE5DE6BF-7AD9-4A10-8086-A9C4BF5674A4   -WriteHistory    -VrfyBackup   -BkUpOnlyIfClean   -CkDB    -BkUpMedia DISK   -BkUpDB    -UseDefDir    -BkExt "BAK"'

This shows us that the job created by the maintenance plan backs up the database to disk, using the default backup directory appending the "BAK" extension to the backup file after performing integrity checks (-CkDB).

You should become familiar with the syntax of xp_sqlmaint as you might have to run it manually at some point. XP_SQLMAINT in turn runs the SQLMaint.exe command line utility, which can be found in the SQL Server installation directory in MSSQL\Binn folder. Note that you must be change the directory to the folder where SQLMAINT.exe is located in order to execute SQLMAINT from the command prompt.

Parameter Explanation

-? Returns syntax of SQLMAINT

-S server_name[\instance_name]

SQL Server name or instance name if using a named instance of SQL Server

-U login_ID

SQL Server login to be used for connecting to SQL Server with SQLMAINT. If not specified SQLMAINT attempts to connect using Windows authentication. Double quotes must be used if login contains special characters.

-P password Password for connecting to SQL Server with the specified login

-D database_name Name of the database to be maintained by SQLMAINT

-PlanName name | -PlanID guid Either maintenance plan name or the GUID of the maintenance plan

-Rpt text_file

Full path and name of the file which should contain the maintenance plan execution report. You can save reports to a remote server by specifying a UNC file name

-To operator_name

Operator that should receive email with maintenance plan execution report attached. Operator must have already been defined. SQLMail is used to notify the operator

Page 2: xp_cmdshell

-HtmlRpt html_file [-DelHtmlRpt <time_period>]

Full path to where maintenance plan report should be saved as HTML file. -DelHtmlRpt <time_period> swith specifies that HTML file must be deleted after specified amount of time

-RmUnusedSpace threshold_percent free_percent

Advises SQL Server to remove unused space from the database. Threshold percent specifies the size database must reach before SQLMaint attempts to shrink it. Free percent specifies the amount of free space to be left in the database files after shrinking. Note that free percent is calculated based on the final size of the database.

-CkDB | -CkDBNoIdxAdvises SQL Server to check database integrity with DBCC CHECKDB. -CkDBNoIdx skips indexes during the integrity check.

-CkAl | -CkAlNoIdx-CkAl advises SQL Server to run DBCC NEWALLOC statement for all tables and indexes. -CkAlNoIdx omits checking of indexes with DBCC NEWALLOC

-CkCat Executes DBCC CHECKCATALOG in the database to check consistency between system tables

-UpdOptiStats sample_percentRuns UPDATE STATISTICS statement against each table in the database with sample of "sample_percent"

-RebldIdx free_space

Specifies amount of free space to be left in indexes that are rebuild as part of this maintenance plan. The fill factor will be set to (100 - free_space)% for all indexes. If free space of 100 is chosen then indexes will be rebuilt with their original FILLFACTOR

-WriteHistoryWrites execution history for each task executed by the maintenance plan into MSDB..sysdbmaintplan_history table

{-BkUpDB [backup_path] | -BkUpLog [backup_path] }

Specifies that full database backup or transaction log backup (or both) must be performed. Backup_path is the full path to the backup destination files.

{-BkUpMedia{DISK [[-DelBkUps <time_period> ::=number[minutes | hours | days | weeks | months|[-CrBkSubDir ] [ -UseDefDir ]]| TAPE}

These flags specify the backup media, which can be either disk or tape. If backing up to disk you have an option to use the default directory (-UseDefDir) - specifying this option overrides the backup_path specified in the previous option. SQLMaint can create a subdirectory for each database that is backed up (-CrBkSubDir). You can further specify time period after which backups must be deleted. For example the following switch will delete files that are 4 weeks old or older: -DelBkUps 4WEEKS

-BkUpOnlyIfClean

Advises SQLMaint to backup database only if integrity checks did not find any errors. If DBCC CHECKDB finds errors and -BkUpOnlyIfClean is specified then backups won't be performed

Page 3: xp_cmdshell

-VrfyBackupExecutes RESTORE VERIFYONLY at completion of each backup to ensure that backups that were just created are readable.

DECLARE @ThreeDaysAgo VARCHAR(50)

SELECT @ThreeDaysAgo = CAST(DATEADD(d, 0, GETDATE()) AS VARCHAR)

print @ThreeDaysAgo

SELECT @ThreeDaysAgo = CAST(DATEADD(d, -3, GETDATE()) AS VARCHAR)

print @ThreeDaysAgo

Page 4: xp_cmdshell

sp_MSFOREACHDB @COMMAND1 = 'USE ?

IF ''?'' NOT IN (''master'', ''model'', ''msdb'', ''tempdb'', ''Northwind'',

''pubs'')

BEGIN

DECLARE @OLDDATE DATETIME

SET @OLDDATE = GETDATE() - 1

EXECUTE master.dbo.xp_delete_file 0, N''E:\SQL_Backups\?'', N''bak'',

@OLDDATE, 1

END

Page 5: xp_cmdshell

DECLARE @ThreeDaysAgo VARCHAR(50)

SELECT @ThreeDaysAgo = CAST(DATEADD(d, -3, GETDATE()) AS VARCHAR)

-- NB this will delete any "bak" file not only the maintenance plan ones.

-- in case the backups are failing check we have at least 2 backups in the folder

-- check > 3 as we always have a final null record for the dir listing - note also that

-- its > 2 backups and not necessarily 2 backups from 3 days ago. Using 2 as its

going

-- to run before the backup job, but the most likely cause of failure is lack of disk

space

-- but we don't want a failure happening and end up with no backups

EXEC dbo.xp_cmdshell 'dir D:\Backups\db\*.bak /b'

IF @@ROWCOUNT > 3

BEGIN

  EXECUTE dbo.xp_delete_file 0,N'D:\Backups\db',N'bak',@ThreeDaysAgo

  -- only do log if deleting the main backup

  EXEC dbo.xp_cmdshell 'dir D:\Backups\db\log\*.trn /b'

  -- for logs we do 3 backups a day so check > 9

  IF @@ROWCOUNT > 9

    EXECUTE dbo.xp_delete_file 0,N'D:\Backups\db\log',N'trn',@ThreeDaysAgo

END