Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.06 KB | None | 0 0
  1. #Author : Ruud Baars
  2. #Date : 11/09/2008
  3. #Edited : 11/15/2012 By: Wilson S.
  4. #edited : 11/27/2012 By: Mike J.
  5.  
  6. # NOTE: Update script to only remove recovery points on Disk. Recovery points removed will be from the oldest one up to the date
  7. # entered by the user while the script is running
  8. #deletes all recovery points before 'now' on selected data source.
  9. $version="V4.7"
  10. $ErrorActionPreference = "silentlycontinue"
  11. add-pssnapin sqlservercmdletsnapin100
  12. Add-PSSnapin -Name Microsoft.DataProtectionManager.PowerShell
  13.  
  14. $MB=1024*1024
  15. $logfile="DPMdeleteRP.LOG"
  16. $wait=10 #seconds
  17. $confirmpreference = "None"
  18.  
  19. function Show_help
  20. {
  21. cls
  22. $l="=" * 79
  23. write-host $l -foregroundcolor magenta
  24. write-host -nonewline "`t<<<" -foregroundcolor white
  25. write-host -nonewline " DANGEROUS :: MAY DELETE MANY RECOVERY POINTS " -foregroundcolor red
  26. write-host ">>>" -foregroundcolor white
  27. write-host $l -foregroundcolor magenta
  28. write-host "Version: $version" -foregroundcolor cyan
  29. write-host "A: User Selects data source to remove recovery points for" -foregroundcolor green
  30. write-host "B: User enters date / time (using 24hr clock) to Delete recovery points" -foregroundcolor green
  31. write-host "C: User Confirms deletion after list of recovery points to be deleted is displayed." -foregroundcolor green
  32. write-host "Appending to log file $logfile`n" -foregroundcolor white
  33. write-host "User Accepts all responsibilities by entering a data source or just pressing [Enter] " -foregroundcolor white -backgroundcolor blue
  34.  
  35. }
  36.  
  37.  
  38. "**********************************" >> $logfile
  39. "Version $version" >> $logfile
  40. get-date >> $logfile
  41. show_help
  42.  
  43. $DPMservername=&"hostname"
  44. "Selected DPM server = $DPMservername" >> $logfile
  45. write-host "`nConnnecting to DPM server retrieving data source list...`n" -foregroundcolor green
  46. $pglist = @(Get-ProtectionGroup $DPMservername) # WILSON - Created PGlist as array in case we have a single protection group.
  47. $ds=@()
  48. $tapes=$null
  49. $count = 0
  50. $dscount = 0
  51. foreach ($count in 0..($pglist.count - 1))
  52. {
  53. # write-host $pglist[$count].friendlyname
  54. $ds += @(get-datasource $pglist[$count]) # WILSON - Created DS as array in case we have a single protection group.
  55. # write-host $ds
  56. # write-host $count -foreground yellow
  57. }
  58. if ( Get-Datasource $DPMservername -inactive) {$ds += Get-Datasource $DPMservername -inactive}
  59.  
  60. $i=0
  61. write-host "Index Protection Group Computer Path"
  62. write-host "---------------------------------------------------------------------------------"
  63. foreach ($l in $ds)
  64. {
  65. "[{0,3}] {1,-20} {2,-20} {3}" -f $i, $l.ProtectionGroupName, $l.psinfo.netbiosname, $l.logicalpath
  66. $i++
  67. }
  68. $DSname=read-host "`nEnter a data source index from list above - Note co-located datasources on same replica will be effected"
  69.  
  70. if (!$DSname)
  71. {
  72. write-host "No datasource selected `n" -foregroundcolor yellow
  73. "Aborted on Datasource name" >> $logfile
  74. exit 0
  75. }
  76. $DSselected=$ds[$DSname]
  77. if (!$DSselected)
  78. {
  79. write-host "No datasource selected `n" -foregroundcolor yellow
  80. "Aborted on Datasource name" >> $logfile
  81. exit 0
  82. }
  83. $rp=get-recoverypoint $DS[$dsname]
  84. $rp
  85. # $DoTape=read-host "`nDo you want to remove when recovery points are on tape ? [y/N]"
  86. # "Remove tape recovery point = $DoTape" >> $logfile
  87.  
  88. write-host "`nCollecting recoverypoint information for datasource $DSselected.name" -foregroundcolor green
  89. if ($DSselected.ShadowCopyUsedspace -gt 0)
  90. {
  91. while ($DSSelected.TotalRecoveryPoints -eq 0)
  92. { # "still 0"
  93. }
  94.  
  95. #this is on disk
  96. $oldShadowUsage=[math]::round($DSselected.ShadowCopyUsedspace/$MB,1)
  97. $line=("Total recoverypoint usage {0} MB on DISK in {1} recovery points" -f $oldShadowUsage ,$DSselected.TotalRecoveryPoints )
  98. $line >> $logfile
  99. write-host $line`n -foregroundcolor white
  100. }
  101.  
  102. #this is on tape
  103. #$trptot=0
  104. #$tp= Get-RecoveryPoint($dsselected) | where {($_.Datalocation -eq "Media")}
  105. #foreach ($trp in $tp) {$trptot += $trp.size }
  106. #if ($trptot -gt 0 )
  107. #{
  108. # $line=("Total recoverypoint usage {0} MB on TAPE in {1} recovery points" -f ($trptot/$MB) ,$DSselected.TotalRecoveryPoints )
  109. # $line >> $logfile
  110. # write-host $line`n -foregroundcolor white
  111. #}
  112.  
  113. [datetime]$afterdate="1/1/1980"
  114. #$answer=read-host "`nDo you want to delete recovery points from the beginning [Y/n]"
  115. #if ($answer -eq "n" )
  116. #{
  117. # [datetime]$afterdate=read-host "Delete recovery points AFTER date [MM/DD/YYYY hh:mm]"
  118. #}
  119. [datetime]$enddate=read-host "Delete ALL Disk based recovery points BEFORE and Including date/time entered [MM/DD/YYYY hh:mm]"
  120. "Deleting recovery points until $enddate" >>$logfile
  121. write-host "Deleting recovery points until and $enddate" -foregroundcolor yellow
  122. $rp=get-recoverypoint $DSselected
  123.  
  124. if ($DoTape -ne "y" )
  125. {
  126. $RPselected=$rp | where {($_.representedpointintime -le $enddate) -and ($_.Isincremental -eq $FALSE) -and ($_.DataLocation -eq "Disk")}
  127. }
  128. else
  129. {
  130. $RPselected=$rp | where {($_.representedpointintime -le $enddate) -and ($_.Isincremental -eq $FALSE)}
  131. }
  132.  
  133. if (!$RPselected)
  134. {
  135. write-host "No recovery points found!" -foregroundcolor yellow
  136. "No recovery points found, aborting...!" >> $logfile
  137. exit 0
  138. }
  139. $reselect = $enddate
  140. $adjustflag = $false
  141. foreach ($onerp in $RPselected)
  142. {
  143. $rtime=[string]$onerp.representedpointintime
  144. $rsize=[math]::round(($onerp.size/$MB),1)
  145. $line= "Found {0}, RP size= {1} MB (If 0 MB, co-located datasource cannot be computed), Incremental={2} "-f $rtime, $rsize,$onerp.Isincremental
  146. $line >> $logfile
  147. write-host "$line" -foregroundcolor yellow
  148. #
  149. #Get dependent rp's for data source
  150. #
  151. $allRPtbd=$DSselected.GetAllRecoveryPointsToBeDeleted($onerp)
  152. foreach ($oneDrp in $allRPtbd)
  153. {
  154. if ($oneDrp.IsIncremental -eq $FALSE) {continue}
  155. $rtime=[string]$oneDrp.representedpointintime
  156. $rsize=[math]::round(($oneDrp.size/$MB),1)
  157. $line= ("`t...is dependancy for {0} size {1} `tIncremental={2}" -f $rtime, $rsize, $oneDrp.Isincremental)
  158. $line >> $logfile
  159. if ($oneDrp.representedpointintime -ge $enddate)
  160. {
  161. #stick to latest full ($oneDrp = dependents, $onerp = full)
  162. $adjustflag = $true
  163. $reselect = $onerp.representedpointintime
  164. "<< Dependents newer than BEFORE date >>>" >> $logfile
  165. Write-Host -nonewline "`t <<< later than BEFORE date >>>" -foregroundcolor white -backgroundcolor red
  166. write-host "$line" -foregroundcolor yellow
  167. }
  168. else
  169. {
  170. #Ok, include current latest incremental
  171. $reselect = $oneDrp.representedpointintime
  172. write-host "$line" -foregroundcolor yellow
  173. }
  174. }
  175. }
  176. if ($reselect -lt $oneDrp.representedpointintime)
  177. {
  178. #we adjusted further backward than latest incremental within selection
  179. $reselect = $rtime
  180. $line = "Adjusted BEFORE date to be $reselect to include dependents to $enddate"
  181. $line >> $logfile
  182. Write-Host $line -foregroundcolor white -backgroundcolor blue
  183. }
  184. $line="`n<<< SECOND TO LAST CHANCE TO ABORT - ONE MORE PROMPT TO CONFIRM. >>>"
  185. write-host $line -foregroundcolor white -backgroundcolor blue
  186. $line >> $logfile
  187. $line="Above recovery points within adjusted range will be permanently deleted !!!"
  188. write-host $line -foregroundcolor red
  189. $line >> $logfile
  190. $line="These RP's include dependent recovery points and may contain co-located datasource(s)"
  191. write-host $line -foregroundcolor red
  192. $line >> $logfile
  193. $line="Data source activity = " + $DSselected.Activity
  194. $line >> $logfile
  195. write-host $line -foregroundcolor white
  196. $DoDelete=""
  197. while (($DoDelete -ne "N" ) -and ($DoDelete -ne "Y"))
  198. {
  199. $line="Continue with deletion (must answer) Y/N? "
  200. write-host $line -foregroundcolor white
  201. $DoDelete=read-host
  202. $line = $line + $DoDelete
  203. $line >> $logfile
  204. }
  205.  
  206. if (!$DSselected.Activity -eq "Idle")
  207. {
  208. $line="Data source not idle, do you want to wait Y/N ? "
  209. write-host $line -foregroundcolor yellow
  210. $Y=read-host
  211. $line = $line + $Y
  212. $line >> $logfile
  213. if ($Y -ieq "Y")
  214. {
  215. Write-Host "Waiting for data source to become idle..." -foregroundcolor green
  216. while ($DSselected.Activity -ne "Idle")
  217. {
  218. ("Waiting {0} seconds" -f $wait) >>$logfile
  219. Write-Host -NoNewline "..." -ForegroundColor blue
  220. start-sleep -s $wait
  221. }
  222. }
  223. }
  224.  
  225. if ($DoDelete -eq "Y")
  226. {
  227. foreach ($onerp in $RPselected)
  228. {
  229. #reselect is adjusted to safe range relative to what was requested
  230. #--- if adjustflag not set then all up to including else only older because we must keep the full
  231. if ((($onerp.representedpointintime -le $reselect) -and ($adjustflag -eq $false)) -or ($onerp.representedpointintime -lt $reselect))
  232. {
  233. $rtime=[string]$onerp.representedpointintime
  234. write-host `n$line -foregroundcolor red
  235. $line >>$logfile
  236. if (($onerp ) -and ($onerp.IsIncremental -eq $FALSE)) { remove-recoverypoint -RecoveryPoint $onerp -confirm:$True} # >> $logfile}
  237. $line =("---`nDeleting recoverypoint -> " + $rtime)
  238. $line >>$logfile
  239. }
  240. }
  241. }
  242. "All Done!" >> $logfile
  243. write-host "`nAll Done!`n`n" -foregroundcolor white
  244. $line="Do you want to View DPMdeleteRP.LOG file Y/N ? "
  245. write-host $line -foregroundcolor white
  246. $Y=read-host
  247. $line = $line + $Y
  248. $line >> $logfile
  249. if ($Y -ieq "Y")
  250. {
  251. Notepad DPMdeleteRP.LOG
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement