Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Db full backup name changing

Posted on 2006-07-20
8
Medium Priority
?
402 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 17147219
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
ID: 17147264
sweetbuttercup,
> @ImageFileName = N'mydbnamefull.bak'
@ImageFileName = N'mydbnamefull'+REPLACE(REPLACE(CONVERT(varchar,getdate(),113),':',''),' ','')+'.bak'
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 17147500
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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 1

Author Comment

by:sweetbuttercup
ID: 17148343
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
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 2000 total points
ID: 17148413
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
ID: 17148468
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
ID: 17152853
Is it still giving any error ?
0
 
LVL 1

Author Comment

by:sweetbuttercup
ID: 17153987
It works perfectly!!!!!! Thank you so much!

0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

730 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