SQL Server automatic backup script

Source: Internet
Author: User

SQL Server automatic backup script

SQL Server backup script:

Third-party backup, and differential backup at other times.

declare  @device  varchar (30); declare  @filename  varchar (50); declare  @datetime  varchar (50); declare  @weekname  varchar (50); declare  @weeknametmp  varchar (4); declare  @weekday  int ; set  @weekname=datename(weekday,getdate()); set  @datetime =  convert ( varchar (20),getdate(),112); set  @device=  'RapooOA'  + @datetime; set  @filename= 'D:\SQLBackup\RapooOA' +@datetime+ '.bak' ; set  @weeknametmp= substring (@weekname,3,1); if (@weeknametmp= '1' )               set  @weekday=1; else  if (@weeknametmp= '2' )               set  @weekday=2; else  if (@weeknametmp= '3' )                 set  @weekday=3; else  if (@weeknametmp= '4' )                 set  @weekday=4; else  if (@weeknametmp= '5' )                 set  @weekday=5; else  if (@weeknametmp= '6' )                 set  @weekday=6; else  if (@weeknametmp= 'Day' )                 set  @weekday=0; set  @datetime =  convert ( varchar (20),getdate()-@weekday,112); set  @device=  'RapooOA'  + @datetime; set  @filename= 'D:\SQLBackup\RapooOA' +@datetime+ '.bak' if (@weekname= 'Weday'                   begin                   execute  sp_addumpdevice  'disk' ,@device,@filename;                   backup  database  RapooOA  to  @device                   end else                   begin                   backup  database  RapooOA  to  @device  with  differential                   end

Save As A. SQL file and use scheduled tasks for scheduled execution.

If the following error occurs: Execute exec sp_dropdevice RapooOA20160117 to delete it and run it again.

Message 15026, level 16, status 1, process sp_addumpdevice, row 74th

The logical device 'rapoooa20160117 'already exists.

This article permanently updates the link address:

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.