需求分析:因为公司遵从正版软件的许可,本着节约成本的优良传统,故没有使用什么盗版软件,用的是微软的SQL Server 2005 Express。因为对数据十分的重视,故需要每天对数据库进行备份,而又要删除两个星期之前的备份数据,经查阅资料,得出如下一套方案,还望各路大侠指点一二:
现在的环境是一台安装SQL Server 2005 Express和Microsoft SQLServer 2005 Management Studio Express 图形化管理工具的服务器。
现在在C盘下创建如下两个文件夹:C:\Script和C:\DB_Backup,Script下用于存放脚本文件,而DB_Backup下用于存放备份数据。
1、C:\Script文件夹下有下面三个文件:Schedule.cmd,DeleteDB.vbs 和BackupDB.sql
2、BackupDB.sql 有如下内容:
DECLARE @dateString CHAR(12), @dayStr CHAR(2), @monthStr CHAR(2), @hourStr CHAR(2), @minStr CHAR(2)
--定义月变量
IF (SELECT LEN(CAST(MONTH(GETDATE()) AS CHAR(2))))=2
SET @monthSTR=CAST(MONTH(GETDATE()) AS CHAR(2))
ELSE
SET @monthSTR= '0' + CAST(MONTH(GETDATE()) AS CHAR(2))
--定义天变量
IF (SELECT LEN(CAST(DAY(GETDATE()) AS CHAR(2))))=2
SET @daySTR=CAST(DAY(GETDATE()) AS CHAR(2))
ELSE
SET @daySTR='0' + CAST(DAY(GETDATE()) AS CHAR(2))
--定义小时变量
IF (SELECT LEN(DATEPART(hh, GETDATE())))=2
SET @hourStr=CAST(DATEPART(hh, GETDATE()) AS CHAR(2))
ELSE
SET @hourStr= '0' + CAST(DATEPART(hh, GETDATE()) AS CHAR(2))
--定义分变量
IF (SELECT LEN(DATEPART(mi, GETDATE())))=2
SET @minStr=CAST(DATEPART(mi, GETDATE()) AS CHAR(2))
ELSE
SET @minStr= '0' + CAST(DATEPART(mi, GETDATE()) AS CHAR(2))
--定义基于当前时间戳变量
SET @dateString=CAST(YEAR(GETDATE()) AS CHAR(4)) + @monthStr + @dayStr + @hourStr + @minStr
--=================================================================
DECLARE @IDENT INT, @sql VARCHAR(1000), @DBNAME VARCHAR(200)
SELECT @IDENT=MIN(database_id) FROM SYS.DATABASES WHERE [database_id] > 0 AND NAME = 'master'
BEGIN
SELECT @DBNAME = NAME FROM SYS.DATABASES WHERE database_id = @IDENT
SELECT @SQL = 'BACKUP DATABASE '+@DBNAME+' TO DISK = ''D:\SQLTEST\'+@DBNAME+'_db_' + @dateString +'.BAK'' WITH INIT'
EXEC (@SQL)
SELECT @IDENT=MIN(database_id) FROM SYS.DATABASES WHERE [database_id] > 0 AND database_id>@IDENT AND NAME NOT IN ('TEMPDB')
END
3、DeleteDB.vbs下有如下内容:
On Error Resume Next
Dim fso, folder, files, sFolder, sFolderTarget
Set fso = CreateObject("Scripting.FileSystemObject")
'保存数据库备份文件路径
sFolder = "D:\SQLTEST\"
Set folder = fso.GetFolder(sFolder)
Set files = folder.Files
'用于写入文本文件,并生成删除数据库备份报告
Const ForAppending = 8
'在scripts下创建一个空txt文件:Log.txt
Set objFile = fso.OpenTextFile(sFolder & "\Log.txt", ForAppending)
objFile.Write "================================================================" &
VBCRLF & VBCRLF
objFile.Write " 数据库备文件报告 " & VBCRLF
objFile.Write " 日期: " & FormatDateTime(Now(),1) & "" &
VBCRLF
查看本栏目更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/database/SQLServer/
objFile.Write " 时间: " & FormatDateTime(Now(),3) & "" &
VBCRLF & VBCRLF
objFile.Write "================================================================" &
VBCRLF
'枚举备份文件目录文件
For Each itemFiles In files
'获取要删除文件的文件名
a=sFolder & itemFiles.Name
'获取文件扩展名
b = fso.GetExtensionName(a)
'检查扩展名是否为BAK
If uCase(b)="BAK" Then
'检查数据库备份是否为14天以前
If DateDiff("d",itemFiles.DateCreated,Now()) >= 14 Then
'删除旧备份
fso.DeleteFile a
objFile.WriteLine "备份文件已删除: " & a
End If
End If
Next
objFile.WriteLine "================================================================" &
VBCRLF & VBCRLF
objFile.Close
Set objFile = Nothing
Set fso = Nothing
Set folder = Nothing
Set files = Nothing
4、在D:\SQLTEST\下创建一个文本文件:Log.txt,用于保存删除日志
5、Schedule.cmd有如下内容:
cd C:\Program Files\Microsoft SQL Server\90\Tools\Binn\
sqlcmd -S .\SQLEXPRESS -E -i"d:\SQLTEST\BackupDB.sql"
C:\Script\ DeleteDB.vbs
6、下面只要在计划任务里设置定时运行Schedule.cmd命令即可。
参考文章:http://www.mssqltips.com/sqlservertip/1486/automate-sql-server-express-backups-and-deletion-of-older-backup-files/