Solved

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

  • 3 August 2023
  • 2 replies
  • 55 views

Userlevel 5
Badge +16
  • Thinkwise Local Partner Brasil
  • 389 replies

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?  ..   

icon

Best answer by John Sangers 4 August 2023, 09:27

View original

This topic has been closed for comments

2 replies

Userlevel 1
Badge +1

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'

Userlevel 5
Badge +16

@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;