Friday, March 9, 2012

Dynamically changing a backup file name

Hi all
I have read in many posts that DTS is not usually used for doing backups (a
scheduled job is preferred). However if I want to dynamically change the
name of the backup file depending on the date e.g. mydata_september.bak
during the month of september and then mydata_november.bak in november, how
would I go about doing that?
Dale1.Simple way is use the SQL Server Maintenance plan. It
automatically appends the Year,month,Day and time with the
database name.
Example. The backup file name for pubs database will be
pubs_db_200310132200.BAK.
2. If you want to use the datetime with your format, you
have to write a program.
Sample Script:
--
Declare
@.CurrentDateTime varchar(20),
@.dbname varchar(20),
@.dbbackupname varchar(40)
Begin
Select @.dbname=db_name()
Print @.dbname
Select @.CurrentDateTime = substring(DATENAME(month, getdate
()),1,3) +
cast(DATEPART(day, GETDATE()) as
varchar(2))+
cast(DATEPART(hh, GETDATE()) as
varchar(2)) +
cast(DATEPART(mi, GETDATE()) as
varchar(2))
Select @.dbbackupname = @.dbname+@.CurrentDateTime
select @.dbbackupname ='E:\' + @.dbbackupname + '.bak'
backup database @.dbname to disk=@.dbbackupname WITH
NOUNLOAD , RETAINDAYS = 2, DIFFERENTIAL, SKIP , STATS =10, FORMAT
Print @.CurrentDateTime
End
Warning: Test the script before you use. Use at your own
risk.
- SQLVarad(MDCBA-1999,MCSE-1999)
>--Original Message--
>Hi all
>I have read in many posts that DTS is not usually used
for doing backups (a
>scheduled job is preferred). However if I want to
dynamically change the
>name of the backup file depending on the date e.g.
mydata_september.bak
>during the month of september and then
mydata_november.bak in november, how
>would I go about doing that?
>Dale
>
>.
>|||Thanks, will give it a try
Dale
"SQLVarad" <SQLVarad@.hotmail.com> wrote in message
news:01f101c398d9$706888d0$a601280a@.phx.gbl...
> 1.Simple way is use the SQL Server Maintenance plan. It
> automatically appends the Year,month,Day and time with the
> database name.
> Example. The backup file name for pubs database will be
> pubs_db_200310132200.BAK.
> 2. If you want to use the datetime with your format, you
> have to write a program.
> Sample Script:
> --
> Declare
> @.CurrentDateTime varchar(20),
> @.dbname varchar(20),
> @.dbbackupname varchar(40)
> Begin
> Select @.dbname=db_name()
> Print @.dbname
> Select @.CurrentDateTime = substring(DATENAME(month, getdate
> ()),1,3) +
> cast(DATEPART(day, GETDATE()) as
> varchar(2))+
> cast(DATEPART(hh, GETDATE()) as
> varchar(2)) +
> cast(DATEPART(mi, GETDATE()) as
> varchar(2))
> Select @.dbbackupname = @.dbname+@.CurrentDateTime
> select @.dbbackupname ='E:\' + @.dbbackupname + '.bak'
> backup database @.dbname to disk=@.dbbackupname WITH
> NOUNLOAD , RETAINDAYS = 2, DIFFERENTIAL, SKIP , STATS => 10, FORMAT
> Print @.CurrentDateTime
> End
> Warning: Test the script before you use. Use at your own
> risk.
> - SQLVarad(MDCBA-1999,MCSE-1999)
> >--Original Message--
> >Hi all
> >
> >I have read in many posts that DTS is not usually used
> for doing backups (a
> >scheduled job is preferred). However if I want to
> dynamically change the
> >name of the backup file depending on the date e.g.
> mydata_september.bak
> >during the month of september and then
> mydata_november.bak in november, how
> >would I go about doing that?
> >
> >Dale
> >
> >
> >.
> >

No comments:

Post a Comment