Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for backup2externalhdd.vbs

(view source code of backup2externalhdd.vbs as plain text)

  1. Option Explicit
  2.  
  3. Dim intAnswer, intButtons
  4. Dim colEvents, objEvent, objFSO, objWMIService, wshShell
  5. Dim strCLArgs, strDisk, strLogDir, strLogFile, strMessage, strSerial, strSourceDir, strTitle
  6.  
  7. strLogFile = ""
  8.  
  9. With WScript.Arguments
  10. 	If .Named.Count   > 0 Then Syntax
  11. 	If .Unnamed.Count < 2 Then Syntax
  12. 	If .Unnamed.Count > 3 Then Syntax
  13. 	strSourceDir = .Unnamed.Item(0)
  14. 	strSerial    = Replace( .Unnamed.Item(1), "-", "" )
  15. 	If .Unnamed.Count = 3 Then
  16. 		strLogFile = .Unnamed.Item(2)
  17. 	End If
  18. End With
  19.  
  20. If Len( strSerial ) <> 8 Then Syntax
  21.  
  22. Set objFSO = CreateObject( "Scripting.FileSystemObject" )
  23. If Not objFSO.FolderExists( strSourceDir ) Then Syntax
  24.  
  25. If strLogFile <> "" Then
  26. 	strLogDir = objFSO.GetParentFolderName( strLogFile )
  27. 	If Not objFSO.FolderExists( strLogDir ) Then Syntax
  28. End If
  29.  
  30.  
  31. Set wshShell      = CreateObject( "WScript.Shell" )
  32. Set objWMIService = GetObject( "winmgmts://./root/CIMV2" )
  33. Set colEvents     = objWMIService.ExecNotificationQuery( "SELECT * FROM __InstanceOperationEvent WITHIN 10 WHERE TargetInstance ISA 'Win32_LogicalDisk'" )
  34. Do While True ' Endless loop
  35. 	Set objEvent = colEvents.NextEvent
  36. 	If objEvent.TargetInstance.VolumeSerialNumber = strSerial Then
  37. 		If objEvent.Path_.Class = "__InstanceCreationEvent" Then
  38. 			strDisk    = objEvent.TargetInstance.DeviceID
  39. 			strTitle   = "External Harddisk " & strDisk & " Connected"
  40. 			strMessage = "Do you want to start backing up to the external harddisk (drive " & strDisk & " """ & objEvent.TargetInstance.VolumeName & """) now?"
  41. 			intButtons = vbYesNoCancel + vbQuestion + vbSystemModal
  42. 			intAnswer  = MsgBox( strMessage, intButtons, strTitle )
  43. 			If intAnswer = vbCancel Then Exit Do
  44. 			If intAnswer = vbYes Then
  45. 				' Replace /S by /MIR if you want to purge deleted files from the backup drive; type ROBOCOPY /? for help
  46. 				strCLArgs = """" & strSourceDir & """ """ & strDisk & Mid( strSourceDir, 3 ) & """ /S /XJ /COPY:DT /DCOPY:T /NP /W:0 /R:0"
  47. 				If strLogFile <> "" Then strCLArgs = strCLArgs & " /LOG:""" & strLogFile & """"
  48. 				' Feel free to use any customized backup command or script instead of this hard-coded ROBOCOPY command
  49. 				wshShell.Run "CMD.EXE /C START ROBOCOPY.EXE " & strCLArgs, 7, False
  50. 			End If
  51. 		ElseIf objEvent.Path_.Class = "__InstanceDeletionEvent" Then
  52. 			strTitle   = "External Harddisk " & strDisk & " Disconnected"
  53. 			strMessage = "The externe harddisk " & strDisk & " has been disconnected." & vbCrLf & vbCrLf & "If this was done before the backup was completed, please reconnect the external harddisk and restart the backup when prompted."
  54. 			intButtons = vbOKOnly + vbExclamation + vbSystemModal
  55. 			MsgBox strMessage, intButtons, strTitle
  56. 		End If
  57. 	End If
  58. Loop
  59.  
  60. Set colEvents     = Nothing
  61. Set objWMIService = Nothing
  62. Set wshShell      = Nothing
  63. Set objFSO        = Nothing
  64.  
  65.  
  66. Sub Syntax( )
  67. 	Dim strHelp
  68. 	strHelp = vbCrLf _
  69. 	        & "Backup2ExternalHDD.vbs,  Version 1.00" _
  70. 	        & vbCrLf _
  71. 	        & "Automatically start a backup whenever a specific external harddisk is connected" _
  72. 	        & vbCrLf & vbCrLf _
  73. 	        & "Usage:  WSCRIPT.EXE  Backup2ExternalHDD.vbs  sourcedir  volserial" _
  74. 	        & vbCrLf & vbCrLf _
  75. 	        & "Where:  sourcedir    directory to be backed up" _
  76. 	        & vbCrLf _
  77. 	        & "        volserial    external harddisk's volume serial number without hyphen" _
  78. 	        & vbCrLf & vbCrLf _
  79. 	        & "Notes:  To look up the volume serial number of a drive, use the VOL command," _
  80. 	        & vbCrLf _
  81. 	        & "        e.g. VOL L: to find the volume serial number of drive L:." _
  82. 	        & vbCrLf & vbCrLf _
  83. 	        & "        If you want to use a rotating set of external harddisks for backups," _
  84. 	        & vbCrLf _
  85. 	        & "        make their volume serial numbers identical using Microsoft's VolumeID" _
  86. 	        & vbCrLf _
  87. 	        & "        tool: https://technet.microsoft.com/en-us/sysinternals/bb897436.aspx" _
  88. 	        & vbCrLf & vbCrLf _
  89. 	        & "        Do NOT set the volume serial number to 0000-0000, that would make WMI" _
  90. 	        & vbCrLf _
  91. 	        & "        return a blank volume serial number, and the backup would never run." _
  92. 	        & vbCrLf & vbCrLf _
  93. 	        & "        The backup command used by this script is a hard-coded ROBOCOPY" _
  94. 	        & vbCrLf _
  95. 	        & "        command; feel free to replace it by any other suitable command or" _
  96. 	        & vbCrLf _
  97. 	        & "        batch file." _
  98. 	        & vbCrLf _
  99. 	        & "        The backup command used by this script duplicates the source" _
  100. 	        & vbCrLf _
  101. 	        & "        directory's folder structure on the backup drive; feel free to" _
  102. 	        & vbCrLf _
  103. 	        & "        change it to suit your requirements." _
  104. 	        & vbCrLf & vbCrLf _
  105. 	        & "        This script is intended to be be scheduled at logon." _
  106. 	        & vbCrLf & vbCrLf _
  107. 	        & "Written by Rob van der Woude" _
  108. 	        & vbCrLf _
  109. 	        & "http://www.robvanderwoude.com"
  110. 	WScript.Echo strHelp
  111. 	Set objFSO = Nothing
  112. 	WScript.Quit 1
  113. End Sub
  114.  

page last uploaded: 2017-08-21, 14:26