备份数据库用到的相关代码(在数据库事务中添加)
时间:2011-01-29 来源:前尘光明
declare @disk nvarchar(100)
set @disk=N'E:\cshisdatabackup\thotel'+convert(nvarchar(12),getdate(),112)
print @disk
BACKUP DATABASE [Thotel] TO DISK = @disk WITH INIT , NOUNLOAD , NAME = N'Thotel 备份', NOSKIP , STATS = 10, DESCRIPTION = N'cshis备份', NOFORMAT
这个bat命令用来备份数据库文件thotel20110129到thotel20110129.7Z
rem 前尘光明 20110129 保存文件的格式为:文件名+20110129的形式
rem 这是使用7z,将当天的文件打包成7z(注:在这个软件里,7z格式和zip相比的压缩率更高)rem -t7z是设置格式为7Z,-m用来设置压缩率,-mx=9表示最大压缩率rem "C:\Program Files\7-Zip\7z.exe" 7Z命令行版本的路径rem %date:~0,4%%date:~5,2%%date:~8,2%,这是截取bat中的日期格式20110129的形式
"C:\Program Files\7-Zip\7z.exe" a -t7z -mx=9 E:\cshisdatabackup\thotel%date:~0,4%%date:~5,2%%date:~8,2%.7z E:\cshisdatabackup\thotel%date:~0,4%%date:~5,2%%date:~8,2%
用这个vbs来清理多余的备份文件:
'用来删除对应目录下的数据库备份文件(带日期格式的如:thotel20110129)
'前尘光明 20110129
dim fso,fpath,fp,fils,notremovefiles,fileExArr,fiex
fpath="E:\cshisdatabackup"
fileExArr=Array("thotel") '支持的备份名前缀,如需要多个前缀,这里添加
keepbackDays=9 '保存九天
numf=Array("01","02","03","04","05","06","07","08","09")
Function fNum(num)
If num<10 then
fNum= numf(num-1)
else fNum= CStr(num)
End If
End Function
'用来获取时间字符串
Function DateStr(d)
DateStr = CStr(Year(d))+fNum(Month(d))+fNum(Day(d))
End Function
'用来生成最近的文件名列表
Function FilesList(fileExArray,oneday)
FilesList="|"
for each fiex in fileExArray
for i=0 to keepbackDays
FilesList=FilesList+ UCase(fiex) +DateStr(oneday-i) + "|"
next
next
End Function
set fso=CreateObject("Scripting.FileSystemObject")
if not fso.FolderExists(fpath) then wscript.exit
notremovefiles = UCase(FilesList(fileExArr,date))
'msgbox notremovefiles
Set fp = fso.GetFolder(fpath)
set fils=fp.Files
for each f in fils
fname=fso.GetBaseName(f.Path)
if InStr(notremovefiles,UCase(fname))=0 then
'f.Delete true '这个的注释取消后,可以删除文件,小心哦,不会在回收站中出现哦
msgbox "这个文件会被删除:"+ vbcrlf +f.Name+ vbcrlf + fname,0,"提示"
end if
next set fso=nothing
set fpath=nothing
set fp=nothing
set fils=nothing
set notremovefiles=nothing
set fileExArr=nothing
set fiex=nothing
相关阅读 更多 +