定时自动备份IIS的WWW日志的vbs脚本

时间:2022-08-31 07:32:04
  1. dim IISCount,IISObject,logfiledir,fso,LogFilePeriods,inputtime,site,sites,i,j,sitename(999),WshShell  
  2. 'on error resume next  
  3. set WshShell = WScript.CreateObject("WScript.Shell")  
  4. Set fso = CreateObject("scripting.FileSystemObject")  
  5. set IISCount=GetObject("IIS://localhost/w3svc")  
  6. sites=0  
  7. for each site in IISCount  
  8. if (site.class="IIsWebServer") then   
  9. sitename(sites)=site.name  
  10. sites=sites+1  
  11. end if  
  12. next  
  13.  
  14. MsgBox "IIS中一共" & sites & "个站点,ID分别为:"  
  15. for j=1 to sites-1  
  16. MsgBox sitename(j)  
  17. next  
  18.  
  19. inputtime=inputbox("您要备份哪天的日志?如备份2004-01-01,则输入040101")  
  20. MsgBox "备份" &inputtime& "天的日志"  
  21.  
  22. MsgBox "开始备份.............."  
  23. for i=0 to sites-1  
  24. Set IISOBJect = GetObject("IIS://localhost/w3svc/" & sitename(i))   
  25. MsgBox "备份ID=" & sitename(i) &" Sitename=" & IISObject.servercomment &"的站点"  
  26. logfiledir = IISObject.LogFileDirectory & "w3svc" & sitename(i)'设置第一个站点的日志路径  
  27. if (Err.Number<>0) then logfiledir=IISCount.LogFileDirectory  
  28. Err.clear  
  29. 'if IISObject.LogFilePeriod=1 then LogFilePeriods="days"  
  30. 'if IISObject.LogFilePeriod=2 then LogFilePeriods="weeks"  
  31. 'if IISObject.LogFilePeriod=3 then LogFilePeriods="months"  
  32. 'if IISObject.LogFilePeriod=4 then LogFilePeriods="hours"  
  33. 'if IISObject.LogFilePeriod=0 AND IISObject.LogFileTruncateSize=-1 then LogFilePerirods="onefile"  
  34. 'if IISObject.LogFilePeriod=0 AND IISObject.LogFileTruncateSize>0 then LogFilePerirods="size"&IISObject.LogFileTruncateSize  
  35. LogFilePeriods=IISObject.LogFilePeriod  
  36. if (Err.Number<>0) then LogFilePeriods=IISCount.LogFilePeriod  
  37. Err.clear  
  38. if (NOT LogFilePeriods=1 AND NOT LogFilePeriods=4) then   
  39. MsgBox "对不起,您的日志偶就不给备份,咋地吧"  
  40. WScript.quit  
  41. end if  
  42. if (NOT fso.folderexists("d:backup")) then   
  43. MsgBox "the folder d:backup is not exist,now create it"  
  44. fso.CreateFolder("d:backup")  
  45. Msgbox "created d:backup succuful"  
  46. end if  
  47. if (NOT fso.FolderExists("d:backup"&IISObject.servercomment)) then  
  48. MsgBox "the folder d:backup" & IISObject.servercomment & " is not exist,now create it"  
  49. fso.CreateFolder("d:backup" & IISObject.servercomment)  
  50. MsgBox "created d:backup" & IISObject.servercomment &" succuful"  
  51. end if  
  52.  
  53. MsgBox "now backup the logfiles"  
  54. logfiledir = WshShell.ExpandEnvironmentStrings(logfiledir) '将环境变量转换成字符串  
  55. MsgBox logfiledir  
  56. fso.Copyfile logfiledir &"ex"&inputtime&"*","d:backup"& IISObject.servercomment  
  57. if Err.number<>0 then   
  58. MsgBox "this site no files"  
  59. else  
  60. MsgBox "backup logfiles succuful"  
  61. end if  
  62.  
  63. inputifdel=inputbox("是否删除已备份文件?输入“YES”进行删除")  
  64. if inputifdel="YES" then  
  65. fso.DeleteFile (logfiledir&"ex"&inputtime&"*")  
  66. MsgBox "删除文件成功"  
  67. else  
  68. MsgBox "文件已保留"  
  69. end if  
  70. next  
  71. MsgBox "备份所有的日志文件成功,嘿嘿"