Solved

Db full backup name changing

Posted on 2006-07-20
8
380 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
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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
GeoClustering  and AOG 25 41
McAfee ePO 5.3.1 failure to install 2 35
sql update 2 35
Converting Stored Procedure to SQL Statement 5 36
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.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

685 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