Freigeben über


Lots of databases, full recovery model, low disk space...what to do?

I do not much care for having my development databases set to Recovery modell = full. There is really no need for that. The day I need to restore a logfile the environmnet better be a production environment.

Unfortunatly SQL 2005 let me set the Recovery model on a global level (Please tell me how if it really does - I'm no core SQL guy.). I know it let's you set default file locations etc. and that is good.

The problems starts when SharePoint creates the db's for you, because you never remember to go in to the SQL server and change the recovery model to simple. Then shrink the log file to save the space... It is simply to unconvenient.

Running low on disk space, this problem had to dealt with. But to change some hundred databases manually is of course out of the question. So I wrote a small script to take care of it.

The Script first alters the recovery model, then shrinks the file. Remember to set the starting point for your databases. In my example I have 7 system databases and the rest is SharePoint db's. Be carefull not to make changes to any database not used for development only.

Do a check on

select name, database_id from sys.databases order by database_id

USE [master]

GO

DECLARE @dbname nvarchar(128)

DECLARE @filename nvarchar(128)

DECLARE @cmd nvarchar(500)

DECLARE @SQLString NVARCHAR(500)

DECLARE @ParmDefinition NVARCHAR(500)

DECLARE @getdbnames CURSOR

          SET @getdbnames = CURSOR FOR

          select name from sys.databases where database_id > 7 order by database_id

OPEN @getdbnames

FETCH NEXT

FROM @getdbnames INTO @dbname

WHILE @@FETCH_STATUS = 0

BEGIN

SET @cmd = N'ALTER DATABASE "' + @dbname + '"

SET RECOVERY SIMPLE WITH NO_WAIT' + CHAR(13)

                 + N'ALTER DATABASE "' + @dbname

                 + '" SET RECOVERY SIMPLE '

EXECUTE sp_executesql @cmd

SET @SQLString =N'select @filenameOUT = name FROM "' + @dbName + '".sys.database_files where type_desc = ''Log'''

SET @ParmDefinition = N'@filenameOUT

varchar(300) OUTPUT'

EXECUTE sp_executesql @SQLString, @ParmDefinition, @filenameOUT=@filename OUTPUT

SELECT @filename

SET @cmd = N'USE "' + @dbname + '" DBCC SHRINKFILE (N''' + @filename +''' , 0, TRUNCATEONLY)'

EXECUTE sp_executesql @cmd

FETCH NEXT FROM @getdbnames INTO @dbname

END

CLOSE @getdbnames

DEALLOCATE @getdbnames

Please note that this approach is not recommended to test and production environments. Leave SharePoint databases to their default settings there.

The change run supprisingly fast (there is a lot of disk I/O here) and I got a lot of free space on the server again! :)

 

Edit:

Just got a reply from Michael in Austria. He says:

Everytime you create a new database SQL Server "clones" its "Model" database. So by setting your appreciate recovery model at this database, it will be default for every new one.

I have tested it for SQL Server 2000 but it should be the same for all other editions.

You can get more information about the model database here: https://msdn.microsoft.com/en-us/library/ms186388.aspx

 

Thanks, Michael! :)

Comments