Advertisement
robbinespu

mssql database backup

Jan 26th, 2024
1,029
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 2.49 KB | Source Code | 0 0
  1. # Function to calculate hash value for a file
  2. function Get-FileHashValue($filePath) {
  3.     $hashAlgorithm = 'SHA256' # You can change this to other algorithms like MD5, SHA1, etc.
  4.     $fileHash = Get-FileHash -Path $filePath -Algorithm $hashAlgorithm
  5.     return $fileHash.Hash
  6. }
  7.  
  8. # Database name
  9. $databaseName = "MY-DATABASE-NAME-HERE"
  10.  
  11. # Source and destination folders
  12. $sourceFolder = "D:\Database\Data"
  13. $destinationFolder = "F:\Database\Data"
  14.  
  15. # Check if the source folder exists
  16. if (Test-Path $sourceFolder -PathType Container) {
  17.  
  18.     # Create the destination folder if it doesn't exist
  19.     if (-not (Test-Path $destinationFolder -PathType Container)) {
  20.         New-Item -ItemType Directory -Force -Path $destinationFolder
  21.     }
  22.  
  23.     # Get all files in the source folder and filter based on the database name
  24.     $sourceDataFiles = Get-ChildItem -Path $sourceFolder | Where-Object { $_.Name -like "$databaseName*.ndf" -or $_.Name -like "$databaseName*.mdf" }
  25.  
  26.     # Build paths for the source and destination data files
  27.     $destinationDataFiles = $sourceDataFiles | ForEach-Object { Join-Path -Path $destinationFolder -ChildPath $_.Name }
  28.  
  29.     $totalFiles = $sourceDataFiles.Count
  30.     $completedFiles = 0
  31.     $failedFiles = @()
  32.  
  33.     # Copy the data files with progress information and check hash values
  34.     foreach ($file in $sourceDataFiles) {
  35.         $destinationPath = Join-Path -Path $destinationFolder -ChildPath $file.Name
  36.         Write-Host "Copying $($file.FullName) to $($destinationPath)..."
  37.         Copy-Item -Path $file.FullName -Destination $destinationPath -Force
  38.  
  39.         # Check hash values
  40.         $sourceHash = Get-FileHashValue -FilePath $file.FullName
  41.         $destinationHash = Get-FileHashValue -FilePath $destinationPath
  42.  
  43.         if ($sourceHash -eq $destinationHash) {
  44.             Write-Host "File copied successfully. Hash values match."
  45.         } else {
  46.             Write-Host "Error copying file: $($file.FullName). Hash values do not match."
  47.             $failedFiles += $file.Name
  48.         }
  49.  
  50.         $completedFiles++
  51.         $copyPercentage = ($completedFiles / $totalFiles) * 100
  52.         Write-Host "Progress: $($completedFiles) of $($totalFiles) files copied. Copy percentage: $($copyPercentage)%`n"
  53.     }
  54.  
  55.     if ($failedFiles.Count -eq 0) {
  56.         Write-Host "All files copied successfully."
  57.     } else {
  58.         Write-Host "Files with hash mismatches: $($failedFiles -join ', ')"
  59.     }
  60.  
  61. } else {
  62.     Write-Host "Source folder does not exist."
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement