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