Showing posts with label managed. Show all posts
Showing posts with label managed. Show all posts

Thursday, March 22, 2012

Backup script

Hi,

I'm currently working on a backup script for my MSSQL databases. I have managed to make a working backup command but I have one problem.
I need every new backup file to have a uniqe filename, for example backup_UNIQE.bak. How can I make a variable that can do this?

Alexander

Here's the basis of one that can be used which uses the date as a unique identifier:

Code Snippet

declare @.filename nvarchar(2000)

set @.filename = 'c:\yourbackupdirectory\dbname_bk_' + convert(varchar,getdate(),112) + '.bak'

backup database northwind
to disk = @.filename

HTH!

|||

Hi,

Can you stick a date on the end of it? See below and then pass the filename into you script somehow.

declare @.datet varchar(10), @.filename varchar(20)

set @.datet = convert(varchar(10),getdate(),112)

set @.filename = 'backup'+@.datet+'.bak'

print @.filename

will give you

backup20070824.bak

You could always add the time to it :

set @.datet = replace(convert(varchar(8),getdate(),114),':', '')

Any help to you?

Cheers

Matt

|||Thank you both! This worked like a charm! Smile

Matt Tolhurst:

Can you please explain this one: set @.datet = replace(convert(varchar(8),getdate(),114),':', '') ?

I'm not TSQL coder yet, and I would appricitate it very much! Smile

- Alexander
|||

Alexander Davidsen wrote:


Can you please explain this one: set @.datet = replace(convert(varchar(8),getdate(),114),':', '') ?

Hi,

That bit is simply taking the time part of the getdate function and removing the colons, as you don't really want to put colons in the file name.

declare @.datet2 datetime, @.datet varchar(10), @.filename varchar(20)

set @.datet2 = getdate()

print @.datet2 --

Aug 24 2007 11:07AM

set @.datet = convert(varchar(10),@.datet2,112)

print @.datet

20070824

here is the time part:

set @.datet = convert(varchar(8),@.datet2,114)

print @.datet

11:07:44

set @.datet = replace(convert(varchar(8),@.datet2,114),':', '')

print @.datet

110744

Any clearer?

Matt

|||Thanks Matt Smile

This made it alot clearer!Smile

Have a nice weekend!

- Alexander

Backup Schedule Troubles

Some how, I've managed to create a backup (disk backup) with three different
schedules. At 6pm each day, the database backups three times (three
different names).
What I'd like to have is... each Monday the previous Monday's backup would
be overwritten, each Tuesday the previous Tuesday's backup would be
overwritten, etc...
Now, I can't figure out how to remove the schedules, as it appears that I
only see one schedule and destination when I go in to the backup.
How do I get to a "clean slate" and accomplish my ideal backup scenario.
Thanks.Check under management, SQL Server Agent, Jobs.
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"09evaca" <09evaca@.discussions.microsoft.com> wrote in message
news:D8D3EAF7-90A5-495C-B9D5-37E933FDF24A@.microsoft.com...
> Some how, I've managed to create a backup (disk backup) with three different
> schedules. At 6pm each day, the database backups three times (three
> different names).
> What I'd like to have is... each Monday the previous Monday's backup would
> be overwritten, each Tuesday the previous Tuesday's backup would be
> overwritten, etc...
> Now, I can't figure out how to remove the schedules, as it appears that I
> only see one schedule and destination when I go in to the backup.
> How do I get to a "clean slate" and accomplish my ideal backup scenario.
> Thanks.

Backup Schedule Troubles

Some how, I've managed to create a backup (disk backup) with three different
schedules. At 6pm each day, the database backups three times (three
different names).
What I'd like to have is... each Monday the previous Monday's backup would
be overwritten, each Tuesday the previous Tuesday's backup would be
overwritten, etc...
Now, I can't figure out how to remove the schedules, as it appears that I
only see one schedule and destination when I go in to the backup.
How do I get to a "clean slate" and accomplish my ideal backup scenario.
Thanks.
Check under management, SQL Server Agent, Jobs.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"09evaca" <09evaca@.discussions.microsoft.com> wrote in message
news:D8D3EAF7-90A5-495C-B9D5-37E933FDF24A@.microsoft.com...
> Some how, I've managed to create a backup (disk backup) with three different
> schedules. At 6pm each day, the database backups three times (three
> different names).
> What I'd like to have is... each Monday the previous Monday's backup would
> be overwritten, each Tuesday the previous Tuesday's backup would be
> overwritten, etc...
> Now, I can't figure out how to remove the schedules, as it appears that I
> only see one schedule and destination when I go in to the backup.
> How do I get to a "clean slate" and accomplish my ideal backup scenario.
> Thanks.
sql

Backup Schedule Troubles

Some how, I've managed to create a backup (disk backup) with three different
schedules. At 6pm each day, the database backups three times (three
different names).
What I'd like to have is... each Monday the previous Monday's backup would
be overwritten, each Tuesday the previous Tuesday's backup would be
overwritten, etc...
Now, I can't figure out how to remove the schedules, as it appears that I
only see one schedule and destination when I go in to the backup.
How do I get to a "clean slate" and accomplish my ideal backup scenario.
Thanks.Check under management, SQL Server Agent, Jobs.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"09evaca" <09evaca@.discussions.microsoft.com> wrote in message
news:D8D3EAF7-90A5-495C-B9D5-37E933FDF24A@.microsoft.com...
> Some how, I've managed to create a backup (disk backup) with three differe
nt
> schedules. At 6pm each day, the database backups three times (three
> different names).
> What I'd like to have is... each Monday the previous Monday's backup would
> be overwritten, each Tuesday the previous Tuesday's backup would be
> overwritten, etc...
> Now, I can't figure out how to remove the schedules, as it appears that I
> only see one schedule and destination when I go in to the backup.
> How do I get to a "clean slate" and accomplish my ideal backup scenario.
> Thanks.