Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Naming a .bak appropriately

Posted on 2010-01-07
4
Medium Priority
?
263 Views
Last Modified: 2012-05-08
I have a destination on the network where I like to keep all of my sql backups. Now for some reason when I go to do a backup to this location the name of the .bak always wants to be the same name. What I would like to do is give the .bak file an appropriate name that corresponds to the database name.

So on the "Back Up Database" Menu, I remove the default back up location, I click the "Add" button and choose my back up device, then create the back up. Problem is I don't know how to make sure that the created BAK is appropriately named. Any direction or insight would be greatly appreciated

Jason
0
Comment
Question by:jazzcatone
  • 2
4 Comments
 
LVL 26

Expert Comment

by:tigin44
ID: 26200623
construct your backup command dynamically and add the date to the end of backup file name.
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 26200684
0
 
LVL 5

Expert Comment

by:dbidba
ID: 26200978

-- ============================================================================
-- Example script to do a differential database backup.
-- Place this in a SQL Agent job and schedule as desired.
-- ============================================================================
-- ----------------------------------------------------------------------------
-- Declare Variables.
-- ----------------------------------------------------------------------------
declare
   @Cmd      nvarchar(4000)   -- Temporary Command holder.
  ,@Msg      varchar(8000)    -- Temporary Messate holder.
  ,@DbNa     sysname          -- Name of Database to backup.
  ,@BkuPthNa varchar(1000)    -- Backup Path Name.
  ,@BkuFilNa varchar(1000)    -- Fully Qualified Backup File Name.

-- ----------------------------------------------------------------------------
-- Assign Constants.
-- ----------------------------------------------------------------------------
select
   @BkuPthNa = 'g:\SqlBackup\LS009\'
  ,@DbNa = 'Dave'

-- ----------------------------------------------------------------------------
-- Build the backup file name.
-- ----------------------------------------------------------------------------
select
  @BkuFilNa = @BkuPthNa
  + @DbNa
  + '\'
  + @DbNa
  + '-Diff-'
  + convert(varchar(20), getdate(),112)
  +'-'
  + replace(convert(varchar(20), getdate(),108),':','')
  + '.bak'

-- ----------------------------------------------------------------------------
-- Create the base backup command.
-- ----------------------------------------------------------------------------
select @Cmd = 'backup database @DbNa to disk = @BkuFilNa with differential'

-- ----------------------------------------------------------------------------
-- Print the command to be executed.
-- ----------------------------------------------------------------------------
select @Msg = replace(@Cmd,'@DbNa',@DbNa)
select @Msg = replace(@Msg,'@BkuFilNa',''''+@BkuFilNa+'''')
select @Msg = 'Command = ' + @Msg
print @Msg

-- ----------------------------------------------------------------------------
-- Execute the command using the runtime values.
-- ----------------------------------------------------------------------------
exec sp_executesql
   @Cmd
  ,N'@DbNa sysname,@BkuFilNa varchar(1000)'
  ,@DbNa
  ,@BkuFilNa

Open in new window

0
 
LVL 5

Accepted Solution

by:
dbidba earned 2000 total points
ID: 26201041
Or for full backups...

-- ============================================================================
-- Example script to do a full database backup.
-- Place this in a SQL Agent job and schedule as desired.
-- ============================================================================
-- ----------------------------------------------------------------------------
-- Declare Variables.
-- ----------------------------------------------------------------------------
declare
   @Cmd      nvarchar(4000)   -- Temporary Command holder.
  ,@Msg      varchar(8000)    -- Temporary Messate holder.
  ,@DbNa     sysname          -- Name of Database to backup.
  ,@BkuPthNa varchar(1000)    -- Backup Path Name.
  ,@BkuFilNa varchar(1000)    -- Fully Qualified Backup File Name.

-- ----------------------------------------------------------------------------
-- Assign Constants.
-- ----------------------------------------------------------------------------
select
   @BkuPthNa = 'g:\SqlBackup\LS009\'
  ,@DbNa = 'Dave'

-- ----------------------------------------------------------------------------
-- Build the backup file name.
-- ----------------------------------------------------------------------------
select
  @BkuFilNa = @BkuPthNa
  + @DbNa
  + '\'
  + @DbNa
  + '-Full-'
  + convert(varchar(20), getdate(),112)
  +'-'
  + replace(convert(varchar(20), getdate(),108),':','')
  + '.bak'

-- ----------------------------------------------------------------------------
-- Create the base backup command.
-- ----------------------------------------------------------------------------
select @Cmd = 'backup database @DbNa to disk = @BkuFilNa'

-- ----------------------------------------------------------------------------
-- Print the command to be executed.
-- ----------------------------------------------------------------------------
select @Msg = replace(@Cmd,'@DbNa',@DbNa)
select @Msg = replace(@Msg,'@BkuFilNa',''''+@BkuFilNa+'''')
select @Msg = 'Command = ' + @Msg
print @Msg

-- ----------------------------------------------------------------------------
-- Execute the command using the runtime values.
-- ----------------------------------------------------------------------------
exec sp_executesql
   @Cmd
  ,N'@DbNa sysname,@BkuFilNa varchar(1000)'
  ,@DbNa
  ,@BkuFilNa

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
Creating a Cordova application which allow user to save to/load from his Dropbox account the application database.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

564 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