# PSMinIO Enterprise Automation Example # Demonstrates enterprise-grade automation scenarios including monitoring, policies, and scheduled operations # Import the module Import-Module ..\..\Module\PSMinIO\PSMinIO.psd1 # Connection details (replace with your actual values) $endpoint = "https://minio.example.com" $accessKey = "your-access-key" $secretKey = "your-secret-key" # Configuration $logFile = "minio-automation-$(Get-Date -Format 'yyyyMMdd').log" $reportFile = "minio-report-$(Get-Date -Format 'yyyyMMdd-HHmmss').html" function Write-Log { param([string]$Message, [string]$Level = "INFO") $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" $logEntry = "[$timestamp] [$Level] $Message" $logEntry | Out-File -FilePath $logFile -Append -Encoding UTF8 $logEntry } try { Write-Log "Starting MinIO enterprise automation demo" # Connect to MinIO $connection = Connect-MinIO -Endpoint $endpoint -AccessKey $accessKey -SecretKey $secretKey Write-Log "Connected to MinIO server: $endpoint" "=== 1. Infrastructure Health Check ===" # Perform comprehensive health checks Write-Log "Performing infrastructure health check" # Check server connectivity and basic operations try { $buckets = Get-MinIOBucket Write-Log "✅ Server connectivity: OK ($($buckets.Count) buckets found)" "✅ Server connectivity: OK ($($buckets.Count) buckets found)" } catch { Write-Log "❌ Server connectivity: FAILED - $($_.Exception.Message)" "ERROR" "❌ Server connectivity: FAILED" throw } # Test bucket operations $testBucketName = "health-check-$(Get-Date -Format 'yyyyMMdd-HHmmss')" try { New-MinIOBucket -BucketName $testBucketName $testExists = Test-MinIOBucketExists -BucketName $testBucketName Remove-MinIOBucket -BucketName $testBucketName -Force Write-Log "✅ Bucket operations: OK" "✅ Bucket operations: OK" } catch { Write-Log "❌ Bucket operations: FAILED - $($_.Exception.Message)" "ERROR" "❌ Bucket operations: FAILED" } "" "=== 2. Storage Statistics and Monitoring ===" # Collect comprehensive storage statistics Write-Log "Collecting storage statistics" $stats = Get-MinIOStats -MaxObjectsToCount 1000 Write-Log "Storage stats collected: $($stats.TotalBuckets) buckets, $($stats.TotalObjects) objects, $($stats.TotalSizeFormatted)" "Storage Overview:" $stats | Format-List TotalBuckets, TotalObjects, TotalSizeFormatted, AverageObjectSizeFormatted "" # Detailed bucket analysis "Bucket Analysis:" $bucketStats = Get-MinIOBucket -IncludeStatistics $bucketStats | Format-Table Name, @{Name="Objects";Expression={$_.ObjectCount}}, @{Name="Size";Expression={$_.SizeFormatted}}, CreationDate -AutoSize "" "=== 3. Automated Backup Operations ===" # Simulate automated backup scenario Write-Log "Starting automated backup operations" $backupBucket = "automated-backups-$(Get-Date -Format 'yyyyMMdd')" New-MinIOBucket -BucketName $backupBucket Write-Log "Created backup bucket: $backupBucket" # Create sample data to backup $backupData = @{ "config-backup.json" = @{ timestamp = Get-Date server = $env:COMPUTERNAME version = "1.0" } | ConvertTo-Json "database-backup.sql" = "-- Database backup generated on $(Get-Date)`nSELECT * FROM users;" "logs-backup.txt" = "Application logs backup`n$(Get-Date) - System started`n$(Get-Date) - Backup initiated" } foreach ($file in $backupData.Keys) { $backupData[$file] | Out-File -FilePath $file -Encoding UTF8 } # Perform backup with organized directory structure $backupDate = Get-Date -Format "yyyy/MM/dd" $backupTime = Get-Date -Format "HH-mm" $backupResults = New-MinIOObject -BucketName $backupBucket -Files $backupData.Keys -BucketDirectory "daily-backups/$backupDate/$backupTime" Write-Log "Backup completed: $($backupResults.Count) files uploaded" "Backup Results:" $backupResults | Format-Table Name, @{Name="Duration";Expression={$_.Duration}}, @{Name="Speed";Expression={$_.AverageSpeedFormatted}} "" "=== 4. Data Lifecycle Management ===" # Simulate data lifecycle management Write-Log "Performing data lifecycle management" # Create lifecycle demo bucket $lifecycleBucket = "lifecycle-demo-$(Get-Date -Format 'yyyyMMdd-HHmmss')" New-MinIOBucket -BucketName $lifecycleBucket # Upload files with different "ages" (simulated by different directories) $lifecycleFiles = @( @{ Name = "current-data.txt"; Content = "Current data"; Directory = "current" } @{ Name = "week-old-data.txt"; Content = "Week old data"; Directory = "archive/weekly" } @{ Name = "month-old-data.txt"; Content = "Month old data"; Directory = "archive/monthly" } @{ Name = "year-old-data.txt"; Content = "Year old data"; Directory = "archive/yearly" } ) foreach ($file in $lifecycleFiles) { $file.Content | Out-File -FilePath $file.Name -Encoding UTF8 New-MinIOObject -BucketName $lifecycleBucket -Files $file.Name -BucketDirectory $file.Directory Remove-Item $file.Name -Force } # Analyze data by lifecycle stage $allLifecycleObjects = Get-MinIOObject -BucketName $lifecycleBucket -ObjectsOnly "Data Lifecycle Analysis:" $allLifecycleObjects | Group-Object { ($_.Name -split '/')[0] } | Format-Table Name, Count -AutoSize "" "=== 5. Security and Compliance Audit ===" # Perform security audit Write-Log "Performing security and compliance audit" $auditResults = @() # Check bucket policies foreach ($bucket in $buckets) { try { $policy = Get-MinIOBucketPolicy -BucketName $bucket.Name $auditResults += [PSCustomObject]@{ Bucket = $bucket.Name HasPolicy = $policy -ne $null PolicySize = if ($policy) { $policy.Length } else { 0 } Status = if ($policy) { "Policy Configured" } else { "No Policy" } } } catch { $auditResults += [PSCustomObject]@{ Bucket = $bucket.Name HasPolicy = $false PolicySize = 0 Status = "Policy Check Failed" } } } "Security Audit Results:" $auditResults | Format-Table Bucket, HasPolicy, Status -AutoSize Write-Log "Security audit completed: $($auditResults.Count) buckets audited" "" "=== 6. Performance Monitoring ===" # Monitor performance metrics Write-Log "Collecting performance metrics" # Test upload/download performance $perfTestFile = "performance-test.txt" $perfTestContent = "Performance test data " * 1000 # ~20KB $perfTestContent | Out-File -FilePath $perfTestFile -Encoding UTF8 -NoNewline $perfBucket = "performance-test-$(Get-Date -Format 'yyyyMMdd-HHmmss')" New-MinIOBucket -BucketName $perfBucket # Measure upload performance $uploadStart = Get-Date $uploadResult = New-MinIOObject -BucketName $perfBucket -Files $perfTestFile $uploadEnd = Get-Date $uploadDuration = $uploadEnd - $uploadStart # Measure download performance $downloadStart = Get-Date $downloadResult = Get-MinIOObjectContent -BucketName $perfBucket -ObjectName $perfTestFile -FilePath "downloaded-$perfTestFile" $downloadEnd = Get-Date $downloadDuration = $downloadEnd - $downloadStart "Performance Metrics:" [PSCustomObject]@{ Operation = "Upload" Duration = $uploadDuration.TotalMilliseconds Speed = $uploadResult.AverageSpeedFormatted FileSize = (Get-Item $perfTestFile).Length } | Format-Table [PSCustomObject]@{ Operation = "Download" Duration = $downloadDuration.TotalMilliseconds Speed = $downloadResult.AverageSpeedFormatted FileSize = (Get-Item "downloaded-$perfTestFile").Length } | Format-Table Write-Log "Performance test completed - Upload: $($uploadDuration.TotalMilliseconds)ms, Download: $($downloadDuration.TotalMilliseconds)ms" "" "=== 7. Generating HTML Report ===" # Generate comprehensive HTML report Write-Log "Generating HTML report" $htmlReport = @"
Generated: $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')
Server: $endpoint
$($auditResults.Count) buckets audited
Buckets with policies: $(($auditResults | Where-Object HasPolicy).Count)