How should I do to properly use the sp_msforeachDB to create and delete backups

503 views Asked by At

I'm setting up a new way to create and delete backups in our internal environments. The space on the servers is a big issue so i really have to find a way to do a backup and delete the older one for each database, procedurally.

I came up with the idea of using the sp_MSforeachdb and simply do a backup. On success, it would delete the older backup that was done the day before. (we do 1 backup per night).

DECLARE @DBOPurpose Varchar(15)
DECLARE @Drive      Varchar(1)
Set @DBOPurpose = 'Omnilabs'

DECLARE @command varchar(Max) 

Select @command = '

If Exists (Select * 
            from sys.databases 
            where name like ''%?'' 
            and name like ''%'+@DBOPurpose+'%'')

BEGIN
EXECUTE master.dbo.xp_create_Subdir '+@Drive+':\SQLBACKUP\'+@DBOPurpose+'
GO

BACKUP DATABASE [?] TO DISK = '+@Drive+':\SQLBackup\'+@DBOPurpose+'_Select Cast(Cast(Year(Getdate())As Varchar(150))+''_''+Cast(month(Getdate())As Varchar(150))+''_''+Cast (Day(Getdate()) As Varchar(150))+''_''+ cast(DATEPART(hour, GETDATE()) as varchar) + '''' + cast(DATEPART(minute, GETDATE()) as varchar)+ '''' + cast(DATEPART(Second, GETDATE()) as varchar)+ ''.bak'' As varchar(150))


END

Print N''TEST''
'
--Select @Command
EXEC sp_MSforeachdb @command 

I was expecting that doing Select @Command would actually show me something, but its null. When I run this, it simply writes "Commands completed successfully." within a second, which doesn't make any sense.

1

There are 1 answers

0
InfiniteLoop On

Just saw my error, which was only caused by the hyphens that were not placed correctly. Here's the code:

If exists(Select * from #TempDBName where DBNAME = @DBOPurpose)
BEGIN
    DECLARE @ERROR bit
    DECLARE @command nvarchar(Max)
    SET @Error = 0
    SET @command = '



    DECLARE @DBO VARCHAR(15)
    Set @DBO = ''%'+@DBOPURPOSE+'%''

    If Exists (Select * 
                    from sys.databases 
                    where name like ''%?'' 
                    and name like ''%''+@DBO+''%'')
            BEGIN TRY

                    PRINT ''?''

                    EXECUTE master.dbo.xp_create_Subdir '''+@Drive+':\SQLBACKUP\TEST\?''
                    BACKUP DATABASE [?] TO DISK = '''+@Drive+':\SQLBackup\'+@DBOPurpose+'_' +(Cast(Cast(Year(Getdate())As Varchar(150))+'_'+Cast(month(Getdate())As Varchar(150))+'_'+Cast (Day(Getdate()) As Varchar(150))+'_'+ cast(DATEPART(hour, GETDATE()) as varchar) + cast(DATEPART(minute, GETDATE()) as varchar)+ cast(DATEPART(Second, GETDATE()) as varchar)+ '.bak''' As varchar(150)))+'

            END TRY

            BEGIN CATCH
            Insert into BackupExecLog(ErrorDate, ErrorNumber, ErrorMessage)
                Select Getdate(),
                    ERROR_NUMBER() as ErrorNumber,
                    ERROR_MESSAGE() as ErrorMessage

                    Set @Error = 1
            END CATCH'


        Select @Command
    --EXEC sys.sp_MSforeachdb @command