1.bat文件:

sqlcmd -S 127.0.0.1 -i f:\backup\backup.sql

2.backup.sql内容:

--其中sp_BackupDatabase为存储过程的名称,addy为要备份的数据库,F为要备份的类型

exec [sp_BackupDatabase] 'addy','F'
go

3.sp_BackupDatabase存储过程:

 

-- Author: Addy.Zhao

-- Create date: 2012-11-10
-- Description: 备份数据库
-- Parameter1: 数据库名
-- Parameter2: 备份类型 F=全部, D=差异, L=日志

--把这个存储过程在master数据库下执行

ALTER PROCEDURE [dbo].[sp_BackupDatabase]
       @databaseName sysname, @backupType CHAR(1)
AS
BEGIN
       SET NOCOUNT ON;

       DECLARE @sqlCommand NVARCHAR(1000)
       DECLARE @dateTime NVARCHAR(20)

       SELECT @dateTime = REPLACE(CONVERT(VARCHAR, GETDATE(),111),'/','') +
       REPLACE(CONVERT(VARCHAR, GETDATE(),108),':','')

       IF @backupType = 'F'
               SET @sqlCommand = 'BACKUP DATABASE ' + @databaseName +
               ' TO DISK = ''C:\Backup\' + @databaseName + '_Full_' + @dateTime + '.BAK'''
      
       IF @backupType = 'D'
               SET @sqlCommand = 'BACKUP DATABASE ' + @databaseName +
               ' TO DISK = ''C:\Backup\' + @databaseName + '_Diff_' + @dateTime + '.BAK'' WITH DIFFERENTIAL'
      
       IF @backupType = 'L'
               SET @sqlCommand = 'BACKUP LOG ' + @databaseName +
               ' TO DISK = ''C:\Backup\' + @databaseName + '_Log_' + @dateTime + '.TRN'''
      
       EXECUTE sp_executesql @sqlCommand
END

 

4.写完上面3个文件,在window计划任务下添加任务即可。

 

posted on 2012-11-12 09:03  小傻瓜  阅读(6663)  评论(0编辑  收藏  举报