I need to know the name of the full database backup, so I can restore to another server. I came up with this basic script for backing up (script 1) but I want to do this dynamically, and began to play with cursors, which did not go so well. The second script seems to create an infinite loop or some nasty Cartesian. If anyone can point me in a direction to getting my peanut butter to mix with my chocolate I'd appreciate it.
FWIW, the end goals are to know the db name and do this in a dynamic way so anytime a db is dropped or added we do not have to remember to update backup jobs. Up till now maintenance plans worked, but now we want to know the backup name so we can restore
db's to a preprod environment. Any suggestions/guidance/links would be appreciated.
declare @dbname varchar(100) declare @backupdate varchar(25) SET @backupdate = convert(varchar(25), GETDATE(), 112) declare @backupfilename varchar(150) declare @backupSQL varchar(MAX) Set @dbname = 'master' Set @backupfilename = @dbname + @backupdate + '.bak' SET @backupSQL = 'BACKUP DATABASE '+@dbname+' TO DISK = N''\\delos\SQL_Live_Backup\PreProd_BackupRestore\'+@backupfilename+''' WITH FORMAT, INIT, NAME = N'''+@backupfilename+''', SKIP, NOREWIND, NOUNLOAD, STATS = 10, CHECKSUM, CONTINUE_AFTER_ERROR' EXEC(@backupSQL)
declare @dbname varchar(100) declare @backupdate varchar(25) SET @backupdate = convert(varchar(25), GETDATE(), 112) declare @backupfilename varchar(150) DECLARE db_cursor CURSOR FOR SELECT name FROM sys.databases WHERE NAME NOT LIKE 'zz_%' AND [state]=0 AND is_read_only=0 AND user_access in (0,1) OPEN db_cursor FETCH NEXT FROM db_cursor INTO @dbname WHILE @@FETCH_STATUS = 0 BEGIN PRINT @dbname + @backupdate + '.bak' END CLOSE db_cursor; DEALLOCATE db_cursor;