Solved

Db full backup name changing

Posted on 2006-07-20
8
340 Views
Last Modified: 2006-11-18
I have the following procedure running automatically to execute fll backups of a MS-SQL 2005 DB.

DECLARE      @return_value int

EXEC      @return_value = [dbo].[BackupDb] @DB = N'mydbname',
            @ImagePath = N'd:\dbbackups\',
            @ImageFileName = N'mydbnamefull.bak',
            @IsDifferential = 0

SELECT      'Return Value' = @return_value

GO

Is there any way this procedure can change the name of the .bak file to a different name everytime it runs. Let's say adding the date to the name?
Currently, everytime the procedure runs, it replaces the file with the new one. I'd like to keep the old files all the time.

Another option could be adding "something" to the procedure to create a new folder (with new name) everytime and store the .bak file there.

Any way to run this procedure without replacing the previous .bk file?


0
Comment
Question by:sweetbuttercup
  • 3
  • 3
  • 2
8 Comments
 
LVL 25

Expert Comment

by:Lee Savidge
Comment Utility
Hi,

When you assign the name, replace

@ImageFileName = N'mydbnamefull.bak',

with

@ImageFileName = N'mydbnamefull-' + convert(varchar(255), getdate(), 112) + right('00' + cast(datepart(hh, getdate()) as varchar(2)), 2) + right('00' + cast(datepart(mm, getdate()) as varchar(2)), 2) + right('00' + cast(datepart(ss, getdate()) as varchar(2)), 2)+ '.bak'


This will give you a file name that is unique, in the form

mydbnamefull-yyyymmddhhmmss.bak

which should be pretty unique

Hope this helps.

Regards,

Lee
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
Comment Utility
sweetbuttercup,
> @ImageFileName = N'mydbnamefull.bak'
@ImageFileName = N'mydbnamefull'+REPLACE(REPLACE(CONVERT(varchar,getdate(),113),':',''),' ','')+'.bak'
0
 
LVL 25

Expert Comment

by:Lee Savidge
Comment Utility
Hi,

On reflection there is always

@ImageFileName = N'mydbnamefull' + replace(replace(replace(replace(convert(varchar,getdate(),121), '-', ''), ' ', ''), ':', ''), '.', '') + '.bak'

Does the same as what I did earlier, but the date format is always numerical which makes it easier searching on a directory listing for the latest as it will always be listed at the bottom if you list things by name. This is because the resultant number is an ever incrementing number.

Regards,

Lee
0
 
LVL 1

Author Comment

by:sweetbuttercup
Comment Utility
I'm getting an error (Incorrect syntax near '+'.) when I run  ANY of the solutions stated above.
Is it because I have the following defined in the store procedure?  I have very basic knowledge of this.

--Stored Proc Parameters
@ImageFileName NVARCHAR(100),
0
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 total points
Comment Utility
All codes are working fine ..

declare @ImageFileName nvarchar(100)
SET @ImageFileName = N'mydbnamefull'+REPLACE(REPLACE(CONVERT(varchar,getdate(),113),':',''),' ','')+'.bak'
SELECT @ImageFileName
SELECT @ImageFileName = N'mydbnamefull' + replace(replace(replace(replace(convert(varchar,getdate(),121), '-', ''), ' ', ''), ':', ''), '.', '') + '.bak'
SELECT @ImageFileName


can you post the entire procedure
0
 
LVL 1

Author Comment

by:sweetbuttercup
Comment Utility
STORE PROCEDURE:
Create PROCEDURE [dbo].[BackupDb]
      --Stored Proc Parameters
      
  @DB nvarchar(30), -- Database Name
  @ImagePath NVARCHAR(1024), -- Path to where the backup image will be stored
  @ImageFileName NVARCHAR(100), -- Name of the actual backup file
  @IsDifferential bit -- Value defining if it is a Differential or full backup 1 = incremental, 0 = full
 
AS
      
      DECLARE @FullFilePath NVARCHAR(1075)
      DECLARE @with nvarchar(300)
      
      if  substring(@ImagePath, len(@ImagePath), 1) <> '\'
            set @ImagePath = @ImagePath + '\'

      SET @FullFilePath = @ImagePath + @ImageFileName
      if @IsDifferential = 1
            backup database @db to disk = @FullFilePath with init, differential
      else
            backup database @db to disk = @FullFilePath with init

GO
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
Comment Utility
Is it still giving any error ?
0
 
LVL 1

Author Comment

by:sweetbuttercup
Comment Utility
It works perfectly!!!!!! Thank you so much!

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now