SQL - 收缩数据库日志文件

时间:2010-02-03 22:57:05

标签: sql-server-2008 backup transaction-log shrink

我正在尝试缩小数据库日志文件。我试图跑:

USE databasename 
BACKUP log databasename
WITH truncate_only 
DBCC shrinkfile (databasename_log, 1)

我收到错误消息:

  

Msg 155,Level 15,State 1,Line 3   'truncate_only'不是公认的   备份选项。

我错过了什么吗?

2 个答案:

答案 0 :(得分:5)

SQL Server 2008不再允许使用NO_LOG / TRUNCATE_ONLY选项。

要截断事务日志,您必须备份(实际)或将数据库的恢复模型切换为简单。后者可能就是你真正想要的。除非您使常规事务日志备份能够在中午恢复到某个时间点,否则您不需要完全恢复。

答案 1 :(得分:0)

我认为最好的方法是使用这样的脚本:

USE AdventureWorks
GO

-- Use some dynamic SQL just only not to re-write several times the name 
-- of your db, or to insert this snippet into a loop for all your databases...
DECLARE @dbname varchar(50) = 'AdventureWorks';
DECLARE @logFileName varchar(50) = @dbname  + '_log';
DECLARE @SQL nvarchar(max);
SET @SQL = REPLACE('ALTER DATABASE {dbname} SET RECOVERY FULL;', '{dbname}', @dbname);
EXECUTE(@SQL);

DECLARE @path nvarchar(255) = N'F:\BCK_DB\logBCK' + CONVERT(CHAR(8), GETDATE(), 112) + '_'
  + REPLACE(CONVERT(CHAR(8), GETDATE(), 108),':','') + '.trn';

BACKUP LOG @dbname TO DISK = @path WITH INIT, COMPRESSION;

DBCC SHRINKFILE(@logFileName);

-- determine here the new file size and growth rate:
SET @SQL = REPLACE('ALTER DATABASE {dbname} MODIFY FILE (NAME = ' + @logFileName + ', SIZE = 32000MB, FILEGROWTH = 10%);', 
    '{dbname}', @dbname);
EXECUTE(@SQL);
GO

http://www.snip2code.com/Snippet/12913/How-to-correctly-Shrink-Log-File-for-SQL