Derek Dysart

Derek Dysart


Periodic notes from an independent Sitecore Developer/Architect

April 2017
M T W T F S S
« Nov    
 12
3456789
10111213141516
17181920212223
24252627282930

Categories


Backup All SQL Databases

Derek DysartDerek Dysart

Sometimes this blog serves as more of a notebook for me to save things I end up looking up a bunch of times. In this case, I find myself needing to backup all of the Sitecore databases for a particular instance, so I’ve adapted the SQL Script from this MSSQL Tips post.

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
DECLARE @scPrefix VARCHAR (20) -- Sitecore database prefix

-- specify database backup directory
SET @path = 'C:\Backup\'  

-- specify filename format
SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112) 

-- specify the Sitecore db prefix
SET @scPrefix = 'MySCDatabase'

DECLARE db_cursor CURSOR FOR  
SELECT name 
FROM master.dbo.sysdatabases 
WHERE CharIndex(@scPrefix, name) = 1

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  

       FETCH NEXT FROM db_cursor INTO @name   
END   

CLOSE db_cursor   
DEALLOCATE db_cursor

or use this handy Gist

Independent Sitecore developer. I also blog about other stuff from time to time.