Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

backup all databases in SQL 2008 Expand / Collapse
Author
Message
Posted Friday, January 20, 2012 7:56 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, September 18, 2013 8:21 AM
Points: 230, Visits: 411
Hi,
Need a script to compress and backup all the databases in sql 2008, except system databases.
Please let me know if someone has it.

Thanks.
Post #1239476
Posted Friday, January 20, 2012 8:27 AM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, February 22, 2013 7:38 AM
Points: 283, Visits: 226
DECLARE @name VARCHAR(50) -- database name
DECLARE @path VARCHAR(256) -- path for backup files
DECLARE @fileName VARCHAR(256) -- filename for backup
DECLARE @fileDate VARCHAR(20) -- used for file name

SET @path = 'C:\Backup\'

SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112)

DECLARE db_cursor CURSOR FOR
SELECT name
FROM master.dbo.sysdatabases
WHERE name NOT IN ('master','model','msdb','tempdb')

OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @name

WHILE @@FETCH_STATUS = 0
BEGIN
SET @fileName = @path + @name + '_' + @fileDate + '.BAK'
BACKUP DATABASE @name TO DISK = @fileName WITH COMPRESSION

FETCH NEXT FROM db_cursor INTO @name
END

CLOSE db_cursor
DEALLOCATE db_cursor



Robert Murphy, Microsoft MCITP (Database Administrator 2008) and MCAD Certified
Post #1239514
Posted Friday, January 20, 2012 8:56 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, February 1, 2012 3:57 AM
Points: 91, Visits: 125


exec sp_msforeachdb 'if db_id(''?'')>4 backup database [?] to disk = ''C:\?.bak'' with init, compression;'

Post #1239539
Posted Friday, January 20, 2012 9:06 AM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, February 22, 2013 7:38 AM
Points: 283, Visits: 226
padhis (1/20/2012)


exec sp_msforeachdb 'if db_id(''?'')>4 backup database [?] to disk = ''C:\?.bak'' with init, compression;'




Does the same thing but isn't support by Microsoft



Robert Murphy, Microsoft MCITP (Database Administrator 2008) and MCAD Certified
Post #1239551
Posted Friday, January 20, 2012 9:15 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, September 18, 2013 8:21 AM
Points: 230, Visits: 411
Thanks a lot all. It works great.
Post #1239568
Posted Sunday, January 22, 2012 2:48 PM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 11:01 AM
Points: 4,656, Visits: 11,131
Ola Hallengren has a great script for that:

http://ola.hallengren.com/sql-server-backup.html

No need to re-invent the wheel.


--
Gianluca Sartori

How to post T-SQL questions
spaghettidba.com
@spaghettidba
Post #1239962
Posted Monday, January 28, 2013 10:19 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, February 22, 2013 6:41 AM
Points: 4, Visits: 8
Try SQL Backup Master, which can back up all non-system databases at once. Basic edition is free.
Post #1412541
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse