SQL Server backup script

Source: Internet
Author: User

-- Full backup
-- Note: Everything starts from full backup.
Declare
@ Path varchar (1000 ),
@ Filename varchar (1000 ),
@ Weekday varchar (20)

Set @ weekday = case datepart (weekday, getdate ())
When 1 then 'sunday'
When 2 then 'monday'
When 3 then 'tues'
When 4 then 'weday'
When 5 then 'thursday'
When 6 then 'Friday'
When 7 then 'satur' endset @ Path = 'd: \ backdata \'
Set @ filename = @ path + @ weekday + 'full backup. Bak'

Backup database [testdb]
To disk = @ filename with init
, Nounload
, Name = n' testdb full backup'
, Noskip
, Stats = 10
, Noformat -- Differential backup
-- Note: Before performing differential backup, you must perform a successful full backup of the database.
Declare
@ Path varchar (1000 ),
@ Weekday varchar (20 ),
@ Filename varchar (1000 ),
@ Hour varchar (4)

Set @ weekday = case datepart (weekday, getdate ())
When 1 then 'sunday'
When 2 then 'monday'
When 3 then 'tues'
When 4 then 'weday'
When 5 then 'thursday'
When 6 then 'Friday'
When 7 then 'satur' end

Set @ Path = 'd: \ backdata \'
Set @ hour = convert (varchar (2), datepart (hour, getdate () + 'point'
Set @ filename = @ path + @ weekday + @ hour + 'differential backup. Bak'

Backup database [testdb]
To disk = @ filename with init
, Nounload
, Differential
, Name = n' testdb differential backup'
, Noskip
, Stats = 10
, Noformat -- log backup
-- Note: a successful full database backup must be performed before log backup.
Declare
@ Path varchar (1000 ),
@ Weekday varchar (20 ),
@ Filename varchar (1000 ),
@ Hour varchar (4)

Set @ weekday = case datepart (weekday, getdate ())
When 1 then 'sunday'
When 2 then 'monday'
When 3 then 'tues'
When 4 then 'weday'
When 5 then 'thursday'
When 6 then 'Friday'
When 7 then 'satur' end

Set @ Path = 'd: \ backdata \'
Set @ hour = convert (varchar (2), datepart (hour, getdate () + 'point'
Set @ filename = @ path + @ weekday + @ hour + 'Log backup. Bak'

Backup log [testdb]
To disk = @ filename with init
, Nounload
, Name = n' testdb log backup'
, Noskip
, Stats = 10
, Noformat
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.