Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 SQL Server Administration (2005)
 Transaction Log backup filename help

Author  Topic 

dbaman
Starting Member

46 Posts

Posted - 2010-01-28 : 08:49:45
I am running this T-SQL to take Transaction log backup...

BACKUP LOG [PUBN] TO DISK = N'M:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\PUBN_DAILY_TRAN.bak' WITH NOFORMAT, INIT, NAME = N'PUBN-Transaction Log Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10
GO
declare @backupSetId as int
select @backupSetId = position from msdb..backupset where database_name=N'PUBN' and backup_set_id=(select max(backup_set_id) from msdb..backupset where database_name=N'PUBN' )
if @backupSetId is null begin raiserror(N'Verify failed. Backup information for database ''PUBN'' not found.', 16, 1) end
RESTORE VERIFYONLY FROM DISK = N'M:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\PUBN_DAILY_TRAN.bak' WITH FILE = @backupSetId, NOUNLOAD, NOREWIND
GO

My question is instead of "PUBN_DAILY_TRAN.bak" as filename for the backup how can I make it Todaydate_PUBN_DAILY_TRAN.bak so everyday I get PUBN_DAILY_TRAN.bak appended with todaysdate?

For example when I run today I should get file name: 01282010_PUBN_DAILY_TRAN.bak

Thanks

R

Kristen
Test

22859 Posts

Posted - 2010-01-28 : 09:06:17
This is what I use

DECLARE @strFileName varchar(4000)
SELECT @strFileName = 'M:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\'
+ db_name()
+ '_'
+ CONVERT(varchar(8), GetDate(), 112) -- yyyymmdd
+ '_'
+ REPLACE(LEFT(CONVERT(varchar(8), GetDate(), 108), 5), ':', '') -- hh:mm:ss
+ '_'
+ 'Tran' -- Type - TLog, Full, Diff etc.
+ '.BAK'

BACKUP LOG [PUBN]
TO DISK = @strFileName
WITH
NAME = N'PUBN-Transaction Log Backup',
STATS = 10

Don't think you need any other options. You could change NAME to use an @Parameter too, and include the Date/Time alongside the description.

"... so everyday I get ..."

You ought to be taking a TLog backup much more frequently than once a day - every 15 minutes would be a good interval ...
Go to Top of Page

dbaman
Starting Member

46 Posts

Posted - 2010-01-28 : 15:21:30
Thanks Kristen. That's what I am looking for.

R
Go to Top of Page
   

- Advertisement -