Automatically back up SQL Server database using bat batch file timing

Source: Internet
Author: User

One. Create a SQL file and write the following code in it:

Use Master
GO
DECLARE @str varchar (100)
Set @str = ' D:\sqlserver14backup\CDB\ ' +replace (replace (replace (CONVERT (Varchar,getdate (), 20), '-', '), ', '), ': ', ' ) + '. Bak '
BACKUP DATABASE [CDB] to [email protected]
With Retaindays=15,noformat,noinit,
Name=n ' full backup ', Skip,norewind,
nounload,stats=10
GO

Two. Write the bat file to invoke the SQL file, create a bat file, and add the following code to it:

@echo off
@sqlcmd-I. D:\backupsql\CDBbackup.sql

REM: Delete backup files from seven days ago
forfiles/p "D:\sqlserver14backup\CDB"/s/m * */d-7/C "cmd/c del @path"
@pause

Three. Set the schedule to perform tasks automatically under Windows7

Start-"program-" Attachment-"Task Scheduler-" action-"Create basic tasks-" Add name (Oraclebackup)-"Weekly-" Set Program run time-"Click Start Program

Automatically back up SQL Server database using bat batch file timing

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.