Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. <#
  2. This script will call the cosmos db migration tool with the correct parameters based
  3. on a list of databases that need to be backed up. It depends on a json param file that
  4. contains the list of cosmos db services that have databases that require backup.
  5.  
  6. This script has a couple of dependencies:
  7.  
  8. (1) the dt.exe that it runs (the cosmos db migration tool and we assume the associated files/dlls
  9. in the compiled folder) needs to be locally available.
  10.  
  11. (2) A configured json file to list out the cosmos services and databases that require backups.
  12. Care should be taken (encrypt the file and provide access to the keys to a limited set of users)
  13. as the read-only keys for the cosmos-db service will be stored here.
  14. #>
  15.  
  16. $retentionDays = 14
  17. $backupList = "C:\temp\CosmosBackup.json" # point these at the appropriate folders
  18. $backupPath = 'C:\temp\'
  19. $pathtoEXE = 'C:\temp\drop\dt.exe'
  20. $backups = Get-Content -Raw -Path $backupList | ConvertFrom-Json
  21.  
  22. foreach($s in $backups.service)
  23. {
  24. $sName = $s.name
  25. Write-Output "Processing service $sName..."
  26. $cosmosConnectString = $s.connectString
  27.  
  28. foreach($d in $s.databases)
  29. {
  30. $database = $d.name
  31. if ($d.backupFlag -eq $true)
  32. {
  33. Write-Output " Backing up collections in $database..."
  34.  
  35. foreach($c in $d.collections)
  36. {
  37. $collection = $c.name
  38. Write-Output " Backing up collection $collection."
  39. <# configure export arguments #>
  40. $connectString = "$cosmosConnectString;Database=$database"
  41. $date = Get-Date
  42. $dateString = $date.ToString('yyyyMMdd')
  43. $dateString = $dateString + '_' + $date.ToString('hhmm')
  44. $targetFile = "$collection`_$dateString.json"
  45.  
  46. $args = "/ErrorLog:" + "$backupPath\backups\$sName\$database\$collection`_$dateString`_log.csv /OverwriteErrorLog"
  47. $args = $args + " /ErrorDetails:Critical /s:DocumentDB /s.ConnectionString:$connectString"
  48. $args = $args + " /s.ConnectionMode:Gateway /s.Collection:$collection /s.Query:`"SELECT * FROM c`""
  49. $args = $args + " /t:JsonFile /t.File:$backupPath\backups\$Name\$database\$targetFile /t.Prettify /t.Overwrite"
  50.  
  51. <# now we are all configured: run the collection backup #>
  52. Start-Process -FilePath $pathtoEXE -ArgumentList $args -Wait
  53. }
  54. }
  55. else {
  56. Write-Output " Skipping $dName backupFlag <> true."
  57. }
  58. }
  59. }
  60.  
  61. $purgeDate = (Get-Date).AddDays(-1 * ($retentionDays + 1))
  62.  
  63. <# remove old logs and backups #>
  64. Get-ChildItem -Path $backupPath -Recurse -Include *.json -Exclude *cosmosBackup.json | Where-Object {$_.CreationTime -lt $purgeDate} | Remove-Item
  65. Get-ChildItem -Path $backupPath -Recurse -Include *.csv | Where-Object {$_.CreationTime -lt $purgeDate} | Remove-Item
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement