博客摘录「 Sql Server 收缩日志文件原理及always on 下的实践」2024年5月22日

四、Always on 环境下实践

先对数据库进行完整备份:

EXEC sp_configure 'show advanced options', 1;

RECONFIGURE;

EXEC sp_configure 'xp_cmdshell', 1;

RECONFIGURE;

DECLARE @DbName NVARCHAR(1000);

DECLARE myCursor CURSOR LOCAL STATIC

FOR

SELECT [name]

FROM sysdatabases

WHERE [name] NOT IN ( 'master', 'model', 'msdb', 'tempdb' )

AND name NOT LIKE '%test%'

AND name NOT LIKE '%bak%'

AND name NOT LIKE '%demo%'

AND version IS NOT NULL

AND version <> 0

ORDER BY [name];

OPEN myCursor;

FETCH NEXT FROM myCursor INTO @DbName;

WHILE ( @@FETCH_STATUS = 0 )

BEGIN

DECLARE @strDate AS NVARCHAR(20),

@strDateBeforeSeven AS NVARCHAR(20),

@strFileName AS NVARCHAR(255),

@strFileNameBeforeSeven AS NVARCHAR(255),

@strCommand AS NVARCHAR(255)

SET @strDate = CONVERT(NVARCHAR(20),GETDATE(),112);

SET @strDateBeforeSeven = CONVERT(NVARCHAR(20),GETDATE()-3,112);

SET @strFileName = 'E:\daybak\['+@DbName+']bakup'+@strDate;

SET @strFileNameBeforeSeven = 'E:\daybak\['+@DbName+']bakup'+@strDateBeforeSeven;

EXEC ('BACKUP DATABASE ['+@DbName+'] TO DISK = ''' + @strFileName + '.bak''')

SET @strCommand = 'DEL ' + @strFileNameBeforeSeven + '.bak'

EXEC master.dbo.xp_cmdshell @strCommand

FETCH NEXT FROM myCursor INTO @DbName;

END;

CLOSE myCursor;

DEALLOCATE myCursor;

然后对数据库进行事务日志备份并收缩:

EXEC sp_configure 'show advanced options', 1;

RECONFIGURE;

EXEC sp_configure 'xp_cmdshell', 1;

RECONFIGURE;

DECLARE @DbName NVARCHAR(1000);

DECLARE myCursor CURSOR LOCAL STATIC

FOR

SELECT [name]

FROM sysdatabases

WHERE [name] NOT IN ( 'master', 'model', 'msdb', 'tempdb' )

AND name NOT LIKE '%test%'

AND name NOT LIKE '%bak%'

AND name NOT LIKE '%demo%'

AND version IS NOT NULL

AND version <> 0

ORDER BY [name];

OPEN myCursor;

FETCH NEXT FROM myCursor INTO @DbName;

WHILE ( @@FETCH_STATUS = 0 )BEGIN

DECLARE @strDate AS NVARCHAR(20),

@strDateBeforeSeven AS NVARCHAR(20),

@strFileName AS NVARCHAR(255),

@strFileNameBeforeSeven AS NVARCHAR(255),

@strCommand AS NVARCHAR(255)

SET @strDate = CONVERT(NVARCHAR(20),GETDATE(),112);

SET @strDateBeforeSeven = CONVERT(NVARCHAR(20),GETDATE()-3,112);

SET @strFileName = 'E:\Log_daybak\['+@DbName+']bakup'+@strDate;

SET @strFileNameBeforeSeven='E:\Log_daybak\'+@DbName+']bakup'+@strDateBeforeSeven;

EXEC ('BACKUP LOG ['+@DbName+'] TO DISK = ''' + @strFileName + '.log'';USE ['+@DbName+'];DBCC SHRINKFILE(2,100);')

SET @strCommand = 'DEL ' + @strFileNameBeforeSeven + '.log'

EXEC master.dbo.xp_cmdshell @strCommand

FETCH NEXT FROM myCursor INTO @DbName;

END;

CLOSE myCursor;

DEALLOCATE myCursor;

相关推荐
美式小田1 小时前
单片机学习笔记 9. 8×8LED点阵屏
笔记·单片机·嵌入式硬件·学习
猫爪笔记1 小时前
前端:HTML (学习笔记)【2】
前端·笔记·学习·html
_不会dp不改名_1 小时前
HCIA笔记3--TCP-UDP-交换机工作原理
笔记·tcp/ip·udp
-一杯为品-2 小时前
【51单片机】程序实验5&6.独立按键-矩阵按键
c语言·笔记·学习·51单片机·硬件工程
熙曦Sakura3 小时前
完全竞争市场
笔记
dr李四维4 小时前
iOS构建版本以及Hbuilder打iOS的ipa包全流程
前端·笔记·ios·产品运营·产品经理·xcode
Komorebi.py8 小时前
【Linux】-学习笔记05
linux·笔记·学习
亦枫Leonlew8 小时前
微积分复习笔记 Calculus Volume 1 - 6.5 Physical Applications
笔记·数学·微积分
冰帝海岸13 小时前
01-spring security认证笔记
java·笔记·spring
小二·14 小时前
java基础面试题笔记(基础篇)
java·笔记·python