SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


backup file with datetime stamp


backup file with datetime stamp

Author
Message
Sqlsavy
Sqlsavy
SSC Eights!
SSC Eights! (931 reputation)SSC Eights! (931 reputation)SSC Eights! (931 reputation)SSC Eights! (931 reputation)SSC Eights! (931 reputation)SSC Eights! (931 reputation)SSC Eights! (931 reputation)SSC Eights! (931 reputation)

Group: General Forum Members
Points: 931 Visits: 868
How to create backup files with datatime stamps this is to retain 2 days backups on the disk

BACKUP DATABASE [DBNAME] TO
DISK = N'D:\BACKUPs\DBNAME\DBNAME_1.bak',
DISK = N'D:\BACKUPs\DBNAME\DBNAME_2.bak',
DISK = N'D:\BACKUPs\DBNAME\DBNAME_3.bak',
DISK = N'D:\BACKUPs\DBNAME\DBNAME_4.bak',
DISK = N'D:\BACKUPs\DBNAME\DBNAME_5.bak',
DISK = N'D:\BACKUPs\DBNAME\DBNAME_6.bak',
DISK = N'D:\BACKUPs\DBNAME\DBNAME_7.bak',
DISK = N'D:\BACKUPs\DBNAME\DBNAME_8.bak'
WITH RETAINDAYS = 2,NOFORMAT, NOINIT,
NAME = N'DBNAME-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, COMPRESSION, STATS = 10
GO

Thanks!
RTaylor2208
RTaylor2208
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2783 Visits: 1264
DECLARE @sqlcmd nvarchar(4000)

SELECT @sqlcmd = '

BACKUP DATABASE [DBNAME] TO
DISK = N''D:\BACKUPs\DBNAME\DBNAME_1_' + CAST(CAST(getdate() AS DATE) AS NVARCHAR(100)) + '.bak'',
DISK = N''D:\BACKUPs\DBNAME\DBNAME_2_' + CAST(CAST(getdate() AS DATE) AS NVARCHAR(100)) + '.bak'',
DISK = N''D:\BACKUPs\DBNAME\DBNAME_3_' + CAST(CAST(getdate() AS DATE) AS NVARCHAR(100)) + '.bak'',
DISK = N''D:\BACKUPs\DBNAME\DBNAME_4_' + CAST(CAST(getdate() AS DATE) AS NVARCHAR(100)) + '.bak'',
DISK = N''D:\BACKUPs\DBNAME\DBNAME_5_' + CAST(CAST(getdate() AS DATE) AS NVARCHAR(100)) + '.bak'',
DISK = N''D:\BACKUPs\DBNAME\DBNAME_6_' + CAST(CAST(getdate() AS DATE) AS NVARCHAR(100)) + '.bak'',
DISK = N''D:\BACKUPs\DBNAME\DBNAME_7_' + CAST(CAST(getdate() AS DATE) AS NVARCHAR(100)) + '.bak'',
DISK = N''D:\BACKUPs\DBNAME\DBNAME_8_' + CAST(CAST(getdate() AS DATE) AS NVARCHAR(100)) + '.bak''
WITH RETAINDAYS = 2,NOFORMAT, NOINIT,
NAME = N''DBNAME-Full Database Backup'', SKIP, NOREWIND, NOUNLOAD, COMPRESSION, STATS = 10
GO'

--PRINT @sqlcmd
EXEC (@sqlcmd)



MCITP SQL 2005, MCSA SQL 2012
Sqlsavy
Sqlsavy
SSC Eights!
SSC Eights! (931 reputation)SSC Eights! (931 reputation)SSC Eights! (931 reputation)SSC Eights! (931 reputation)SSC Eights! (931 reputation)SSC Eights! (931 reputation)SSC Eights! (931 reputation)SSC Eights! (931 reputation)

Group: General Forum Members
Points: 931 Visits: 868
Thanks a lot Smile
free_mascot
free_mascot
SSCertifiable
SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)

Group: General Forum Members
Points: 7731 Visits: 2250
In addition, attached script also can be handy for backup and restore. However little modification is required as per the requirement. This is for log backup you can also use it for full backup.

http://www.sqlservercentral.com/scripts/Backup+%2f+Restore/71918/
http://www.sqlservercentral.com/scripts/Backup+%2f+Restore/72321/

HTH

---------------------------------------------------
"Thare are only 10 types of people in the world:
Those who understand binary, and those who don't."
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)

Group: General Forum Members
Points: 223268 Visits: 42003
Sqlsavy (10/28/2013)
How to create backup files with datatime stamps this is to retain 2 days backups on the disk

BACKUP DATABASE [DBNAME] TO
DISK = N'D:\BACKUPs\DBNAME\DBNAME_1.bak',
DISK = N'D:\BACKUPs\DBNAME\DBNAME_2.bak',
DISK = N'D:\BACKUPs\DBNAME\DBNAME_3.bak',
DISK = N'D:\BACKUPs\DBNAME\DBNAME_4.bak',
DISK = N'D:\BACKUPs\DBNAME\DBNAME_5.bak',
DISK = N'D:\BACKUPs\DBNAME\DBNAME_6.bak',
DISK = N'D:\BACKUPs\DBNAME\DBNAME_7.bak',
DISK = N'D:\BACKUPs\DBNAME\DBNAME_8.bak'
WITH RETAINDAYS = 2,NOFORMAT, NOINIT,
NAME = N'DBNAME-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, COMPRESSION, STATS = 10
GO

Thanks!


I can't tell you when it will happen nor what the circumstances will be but there will come a day when you find out that it's a real mistake to automatically delete backup files automatically or even by date. I strongly recommend keeping at least 1 backup file for each of two days even if those files are a week old. For example, we went through a move from one building to another. The servers were down from 5PM on a Friday night 'til 5AM the following Monday morning. If we had a maintance plan or automatic deletes that simply deleted anything older than two days and had a backup failure on a corrupt database, we would have had to spend the day recalling a backup tape or two from offsite remote storage.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Sqlsavy
Sqlsavy
SSC Eights!
SSC Eights! (931 reputation)SSC Eights! (931 reputation)SSC Eights! (931 reputation)SSC Eights! (931 reputation)SSC Eights! (931 reputation)SSC Eights! (931 reputation)SSC Eights! (931 reputation)SSC Eights! (931 reputation)

Group: General Forum Members
Points: 931 Visits: 868
Is it possible to delete old backups using SQL backup script with RETAINDAYS ?
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)

Group: General Forum Members
Points: 223268 Visits: 42003
Sqlsavy (11/4/2013)
Is it possible to delete old backups using SQL backup script with RETAINDAYS ?


No and apologies for the confusion on that.

Normally when I see folks with a RETAINDAYS limit set during the backup creation, they also have something that automatically deletes data sometimes even before a new and viable backup has been made. That's what I was warning against and failed to be clear on that subject.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search