Solved

Db full backup name changing

Posted on 2006-07-20
8
389 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 500 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
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.

734 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