Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

SQL Server 2005 backup on multiple Databases in one go

Posted on 2011-03-17
4
342 Views
Last Modified: 2012-05-11
I have 23 databases in one instance of SQL Server 2005 that i want to backup in one go.

Please help
0
Comment
Question by:mamlaide
  • 2
  • 2
4 Comments
 
LVL 6

Expert Comment

by:dan_mason
ID: 35159907
This is the stored procedure I use: does everything in one go.

Note that you need to set a table up before running it - info is in the notes.
CREATE Procedure [dbo].[spBackupAllDatabases]
@Path	varchar(128) ,
@Type	varchar(4)	-- Full / Log
as
/*
Backup file format
<dbname>_Full_yyyymmdd_hhmmss.bak
<dbname>_Log_yyyymmdd_hhmmss.bak

Example:
EXEC Admin.dbo.spBackupAllDatabases 'C\SQLbackups\','Full'
*/
/*
Before you use, set this table up on the DB you're creating the sproc on: 

Create table DatabaseBackup
	(
	Name			varchar(128) primary key nonclustered ,
	BackupFlagFull		varchar(1) not null check (BackupFlagFull in ('Y','N')) ,
	BackupFlagLog		varchar(1) not null check (BackupFlagLog in ('Y','N')) ,
	RetentionPeriodFull	datetime not null ,
	RetentionPeriodLog	datetime not null
	)
*/
set nocount on
declare	@sql varchar(1000)
	
	-- Get all database names
	create table #DBName
		(
		ID		int identity (1,1) ,
		Name		varchar(128) not null ,
		RetentionPeriod	datetime null
		)
	
	insert	#DBName
		(Name)
	select	name
	from	master..sysdatabases
	
	-- Include any new databases in the backup
	insert	DatabaseBackup
		(
		Name ,
		BackupFlagFull ,
		BackupFlagLog ,
		RetentionPeriodFull ,
		RetentionPeriodLog
		)
	select	#DBName.Name ,
		'Y' ,
		'N' ,
		'7 jan 1900' ,		-- default one week
		'1 jan 1900'
	from	#DBName
		left outer join DatabaseBackup
			on DatabaseBackup.Name = #DBName.Name
	where	DatabaseBackup.Name is null
	and	lower(#DBName.Name) <> 'tempdb'
	
	-- Remove any non-existant databases
	delete	DatabaseBackup
	where not exists
		(
		select	*
		from	#DBName
		where	#DBName.Name = DatabaseBackup.Name
		)
	
	delete	#DBName
	
	create table #ExistingBackups
		(
		Name varchar(128) ,
		ID int identity (1,1)
		)
	
	-- loop through databases
declare	@Name 			varchar(128) ,
	@RetentionPeriod	datetime ,
	@LastBackupToKeep	varchar(8) ,
	@ID			int ,
	@MaxID			int	
	
	insert	#DBName
		(Name, RetentionPeriod)
	select	Name, case when @Type = 'Full' then RetentionPeriodFull else RetentionPeriodLog end
	from	DatabaseBackup
	where	(@Type = 'Full' and BackupFlagFull = 'Y')
	or	(@Type = 'Log' and BackupFlagLog = 'Y')
	
	select	@MaxID 	= max(ID) ,
		@ID	= 0
	from	#DBName
	
	while @ID < @MaxID
	begin
		-- get next database to backup
		select	@ID = min(ID) from #DBName where ID > @ID
		
		select 	@Name = Name ,
			@RetentionPeriod = RetentionPeriod
		from 	#DBName
		where	ID = @ID
		
		-- Delete old backups
		delete	#ExistingBackups
		select 	@sql = 'dir /B ' + @Path
		select	@sql = @sql + @Name + '_' + @Type + '*.*'
		
		insert	#ExistingBackups exec master..xp_cmdshell @sql
		
		if exists (select * from #ExistingBackups where Name like '%File Not Found%')
			delete #ExistingBackups
		
		select 	@LastBackupToKeep = convert(varchar(8),getdate() - @RetentionPeriod,112)
		delete	#ExistingBackups where Name > @Name + '_' + @Type + '_' + @LastBackupToKeep

declare	@eID	int ,
	@eMaxID	int ,
	@eName	varchar(128)
		
		-- loop round all the out of date backups
		select	@eID = 0 ,
			@eMaxID = coalesce(max(ID), 0)
		from	#ExistingBackups
		
		while @eID < @eMaxID
		begin
			select 	@eID = min(ID) from #ExistingBackups where ID > @eID
			select	@eName = Name from #ExistingBackups where ID = @eID
			
			select @sql = 'del ' + @Path + @eName
			exec master..xp_cmdshell @sql
		end
		delete	#ExistingBackups
	
		-- now do the backup
		select @sql = @Path + @Name + '_' + @Type + '_' 
					+ convert(varchar(8),getdate(),112) + '_' 
					+ replace(convert(varchar(8),getdate(),108),':','') + '.bak'
		if @Type = 'Full'
			backup database @Name
			to disk = @sql
		else
			backup log @Name
			to disk = @sql
	end

Open in new window

0
 

Author Comment

by:mamlaide
ID: 35160026
Hi Dan Mason,

I am kind of a novice in sql server. Can you give a more simplier solution or explain this solution very well to me (i mean the code) so i could try to implement it.

Thanks
0
 

Author Comment

by:mamlaide
ID: 35160157
Also what about if i have to do differential backups other than full or log
0
 
LVL 6

Accepted Solution

by:
dan_mason earned 500 total points
ID: 35160876
The code is quite difficult to explain in simpler terms: there are explanations dotted through the code, on the lines that are commented out (will appear green in SSMS if you paste it in), but if you were to execute it as it is, and then run the bit in the "CREATE TABLE" subsection, you could then run this:

EXEC Admin.dbo.spBackupAllDatabases 'C\SQLbackups\','Full'

And all your databases would be backed up to C:\SQLbackups

To then do differential backups, you'd want to add a section around line 145:

ELSE IF @Type="Diff"
backup database @Name TO DISK = @sql WITH DIFFERENTIAL

Then you would execute the procedure as before but with Diff instead:
EXEC Admin.dbo.spBackupAllDatabases 'C\SQLbackups\','Diff'


0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question