Showing posts with label Log. Show all posts
Showing posts with label Log. Show all posts

Tuesday, September 24, 2019

Shrink LOG files automatically, SQL Server

Use below script to shrink T-LOG files. This script will shrink the log file which is larger than the value of the variable "@LOG_SIZE_LIMIT". You can change it to meet your requirement.

To run this script automatically, create a SQL JOB and place this script in the STEP. That's all.

See Also: SHRINK DATABASE, Check Job Status, Grant Access to SQL Agent, View Job history.


USE master;
GO
DECLARE @LOG_SIZE_LIMIT int = 10240 -- 10GB

declare  DBName cursor for
select a.DATABASE_NAME,mf. name AS LogicalFileName ,LOG_SIZE_MB
from(
SELECT DATABASE_NAME = DB_NAME(s.database_id)
    , LOG_SIZE_MB = CAST(SUM(CASE WHEN type_desc = 'LOG' THEN size END) * 8. / 1024 AS DECIMAL(10,2))
    , ROW_SIZE_MB = CAST(SUM(CASE WHEN type_desc = 'ROWS' THEN size END) * 8. / 1024 AS DECIMAL(10,2))
    , TOTAL_SIZE_MB = CAST(SUM(size) * 8. / 1024 AS DECIMAL(10,2))
FROM sys.databases s
JOIN sys.master_files m
ON s.database_id = m.database_id
where s.database_id > 4 and s.state_desc= 'ONLINE' and s.recovery_model_desc = 'SIMPLE'
GROUP BY s.database_id,s.name
) a
inner join sys.master_files AS mf on DB_NAME(mf.database_id) = a.DATABASE_NAME and mf.type_desc = 'LOG'
where LOG_SIZE_MB > @LOG_SIZE_LIMIT
ORDER BY LOG_SIZE_MB desc

declare @DBNAme varchar(1000), @LogicalFileName varchar(1000), @LOG_SIZE_MB int, @SQLExec varchar(4000)

open DBName
fetch next from DBName into @DBNAme,@LogicalFileName,@LOG_SIZE_MB

WHILE @@FETCH_STATUS=0
BEGIN

SET @SQLExec= 'use '+@DBNAme+'; DBCC shrinkfile ('+@LogicalFileName+')'
print @SQLExec
execute (@SQLExec)

fetch next from DBName into @DBNAme,@LogicalFileName,@LOG_SIZE_MB
END

close DBName
deallocate DBName

Thanks.

Monday, August 19, 2019

Check Database backup History, SQL Server

Always I like to use below SQL to find the backup history either DB backup or LOG backup. this is very handy and useful.

By this SQL you can find the backup DB name,  date-time when the backup was taken, media to use, backup-size, logical path to take backup and many more...

By this SQL you also can find the location where to take backup last time ...

Below Query will show the result for lastest 10 days history. If you need to number of the day of history to check, please change in WHERE condition...


SELECT CONVERT(CHAR(100), Serverproperty('Servername')) AS Server,
       bkset.database_name,
       bkset.backup_start_date,
       bkset.backup_finish_date,
       bkset.expiration_date,
       CASE bkset.type
         WHEN 'D' THEN 'Database'
         WHEN 'L' THEN 'Log'
       END                    AS backup_type,
       bkset.backup_size,
       bkf.logical_device_name,
       bkf.physical_device_name,
       bkset.NAME             AS backupset_name,
       bkset.description,
       *
FROM   msdb.dbo.backupmediafamily bkf
       INNER JOIN msdb.dbo.backupset bkset
               ON bkf.media_set_id = bkset.media_set_id
WHERE  ( CONVERT(DATETIME, bkset.backup_start_date, 102) 

                 >= Getdate() - 10 )
ORDER  BY bkset.database_name,
          bkset.backup_finish_date 


This SQL will give output as below .. due to some restriction, I am blackout some data ...


Sample Output of the above SQL