Code:
DECLARE @path VARCHAR(256) SET @path = 'E:\SQLBACKUPS\' EXECUTE master.dbo.xp_create_subdir @path DECLARE @query nvarchar(max); DECLARE @name VARCHAR(50) -- database name DECLARE @fileName VARCHAR(256) -- filename for backup DECLARE db_cursor CURSOR FOR SELECT name FROM master.dbo.sysdatabases WHERE name NOT IN ('master','model','msdb','tempdb') -- exclude these databases OPEN db_cursor FETCH NEXT FROM db_cursor INTO @name WHILE @@FETCH_STATUS = 0 BEGIN SET @fileName = @path + @name + '.BAK' BACKUP DATABASE @name TO DISK = @fileName FETCH NEXT FROM db_cursor INTO @name END CLOSE db_cursor DEALLOCATE db_cursor IF EXISTS ( SELECT name FROM master.dbo.sysdatabases WHERE name = N'SSISDB' ) BEGIN USE SSISDB DECLARE @command NVARCHAR(MAX) SET @command = 'BACKUP MASTER KEY TO FILE = ''' + @path + N'SSISDB_MASTERKEY'' ' + 'ENCRYPTION BY PASSWORD = ''Password1$'';' exec sp_executesql @command END ELSE BEGIN print 'SSISDB DOES NOT EXIST - COULD NOT BACKUP MASTER KEY' END