You are on page 1of 5

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-8086A9C4BF5674A4 -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 -? -S server_name[\instance_name] Explanation Returns syntax of SQLMAINT SQL Server name or instance name if using a named instance of SQL Server 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. Password for connecting to SQL Server with the specified login Name of the database to be maintained by SQLMAINT Either maintenance plan name or the GUID of the maintenance plan 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 Operator that should receive email with maintenance plan execution report attached. Operator must have already been defined. SQLMail is used to notify the operator

-U login_ID

-P password -D database_name -PlanName name | -PlanID guid

-Rpt text_file

-To operator_name

Full path to where maintenance plan report should -HtmlRpt html_file [-DelHtmlRpt be saved as HTML file. -DelHtmlRpt <time_period>] <time_period> swith specifies that HTML file must be deleted after specified amount of time 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. Advises SQL Server to check database integrity with DBCC CHECKDB. -CkDBNoIdx skips indexes during the integrity check. -CkAl advises SQL Server to run DBCC NEWALLOC statement for all tables and indexes. -CkAlNoIdx omits checking of indexes with DBCC NEWALLOC Executes DBCC CHECKCATALOG in the database to check consistency between system tables Runs UPDATE STATISTICS statement against each table in the database with sample of "sample_percent" 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 Writes execution history for each task executed by the maintenance plan into MSDB..sysdbmaintplan_history table 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. 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 Advises SQLMaint to backup database only if integrity checks did not find any errors. If DBCC CHECKDB finds errors and -BkUpOnlyIfClean is

-RmUnusedSpace threshold_percent free_percent

-CkDB | -CkDBNoIdx

-CkAl | -CkAlNoIdx -CkCat -UpdOptiStats sample_percent

-RebldIdx free_space

-WriteHistory

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

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

specified then backups won't be performed -VrfyBackup Executes 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

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

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