博客摘录「 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;

相关推荐
云边有个稻草人2 小时前
【优选算法】—复写零(双指针算法)
笔记·算法·双指针算法
冷眼看人间恩怨10 小时前
【Qt笔记】QDockWidget控件详解
c++·笔记·qt·qdockwidget
Hejjon15 小时前
SpringBoot 整合 SQLite 数据库
笔记
西洼工作室18 小时前
【java 正则表达式 笔记】
java·笔记·正则表达式
初学者7.18 小时前
Webpack学习笔记(2)
笔记·学习·webpack
新手上路狂踩坑19 小时前
Android Studio的笔记--BusyBox相关
android·linux·笔记·android studio·busybox
stm 学习ing21 小时前
HDLBits训练3
c语言·经验分享·笔记·算法·fpga·eda·verilog hdl
尘觉21 小时前
算法的学习笔记—扑克牌顺子(牛客JZ61)
数据结构·笔记·学习·算法
bohu831 天前
sentinel学习笔记1-为什么需要服务降级
笔记·学习·sentinel·滑动窗口
初学者7.1 天前
Webpack学习笔记(3)
笔记·学习·webpack