Skip to main content
Answer

Automate default backup at every deployment, anyone already made this and cares to share?

  • August 3, 2023
  • 2 replies
  • 86 views

Freddy
Forum|alt.badge.img+16
  • Thinkwise Local Partner Brasil

Just a question, out of perhaps laziness :)

Personally I always tend to forget to make a backup before a deployment..  anyone by chance already made a solution for this in the SF and cares to share it?  ..   

Best answer by John Sangers

Hi Freddy,

We added a template with a piece of code to the ug_before_upgrade_always program object:

SET @name = (SELECT DB_NAME()) 

BACKUP DATABASE @name TO DISK = @filename

where the the @filename is composed of the settings we use on our server, the path can be determined using:
   EXEC master.dbo.xp_instance_regread 
            N'HKEY_LOCAL_MACHINE', 
            N'Software\Microsoft\MSSQLServer\MSSQLServer',N'BackupDirectory', 
            @path OUTPUT,  
            'no_output'

This topic has been closed for replies.

2 replies

John Sangers
Apprentice
Forum|alt.badge.img+1
  • Apprentice
  • Answer
  • August 4, 2023

Hi Freddy,

We added a template with a piece of code to the ug_before_upgrade_always program object:

SET @name = (SELECT DB_NAME()) 

BACKUP DATABASE @name TO DISK = @filename

where the the @filename is composed of the settings we use on our server, the path can be determined using:
   EXEC master.dbo.xp_instance_regread 
            N'HKEY_LOCAL_MACHINE', 
            N'Software\Microsoft\MSSQLServer\MSSQLServer',N'BackupDirectory', 
            @path OUTPUT,  
            'no_output'


Freddy
Forum|alt.badge.img+16
  • Author
  • Thinkwise Local Partner Brasil
  • August 4, 2023

@John Sangers it's that easy :) 

Thanks!

As we use containers, I did it as followed:

declare @database       varchar(30)		= (select DB_NAME())
declare @path varchar(150) = '/var/opt/mssql/backup/'
declare @target varchar(260) = @path + @database + '.bak'

BACKUP DATABASE @database
TO DISK = @target
WITH FORMAT;