Advertisement
jargon

Sandbox Ripper Automatic Backup.ps1

Mar 11th, 2025
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # "Sandbox Ripper Automatic Backup.ps1" - Copyright Tim Keal alias jargon 2025 03/03
  2.  
  3. # Define variables
  4. $Project = "Sandbox Ripper"
  5. $SourceFolder = "C:\xampp-8.1.6-0\projects\sandbox.1.test\public\"     # Folder to be archived
  6. $StorageDirectory = "C:\xampp-8.1.6-0\projects\sandbox.1.test\$Project Archives" # Where archives are stored
  7. $DestinationDrive = "F:\"                      # Destination drive root
  8. $SevenZipPath = "C:\Program Files\7-Zip\7z.exe"    # Path to 7z.exe (renamed to avoid syntax issues)
  9.  
  10. # Get timestamp for unique filename
  11. $Timestamp = (Get-Date -Format "yyyy-MMdd-HHmm-ssfff") -replace "\d$"
  12.  
  13. # Prompt for a caption
  14. $Caption = Read-Host "Enter a caption for the backup of $($Project)"
  15. $Caption = $Caption -replace '[\\/:*?"<>|]', "_"
  16.  
  17. $Auto = "AUTOMATIC BACKUP"
  18.  
  19. if ($Caption.Length -gt 0) {
  20.     $Auto += " + $Caption"
  21. }
  22.  
  23. $Caption = $Auto
  24.  
  25. # Define archive name and path
  26. $ArchiveName = "$Project $Timestamp ($Caption).7z"
  27. $ArchivePath = Join-Path -Path $StorageDirectory -ChildPath $ArchiveName
  28.  
  29. # Debug: Print archive path for validation
  30. Write-Host "Archive will be created at: $ArchivePath"
  31.  
  32. # Ensure 7-Zip exists
  33. if (-Not (Test-Path $SevenZipPath)) {
  34.     Write-Host "ERROR: 7-Zip executable not found at $SevenZipPath. Check installation." -ForegroundColor Red
  35.     exit 1
  36. }
  37.  
  38. # Ensure source directory exists
  39. if (-Not (Test-Path $SourceFolder)) {
  40.     Write-Host "ERROR: Source folder '$SourceFolder' does not exist. Aborting backup." -ForegroundColor Red
  41.     exit 1
  42. }
  43.  
  44. # Ensure storage directory exists
  45. if (-Not (Test-Path $StorageDirectory)) {
  46.     Write-Host "Creating storage directory: $StorageDirectory"
  47.     New-Item -ItemType Directory -Path $StorageDirectory -Force | Out-Null
  48. }
  49.  
  50. # Create the archive
  51. Write-Host "Creating archive: $ArchivePath"
  52. $Process = Start-Process -FilePath $SevenZipPath -ArgumentList "a -t7z `"$ArchivePath`" `"$SourceFolder`" -mx9" -NoNewWindow -Wait -PassThru
  53.  
  54. # Check if 7-Zip operation succeeded
  55. if ($Process.ExitCode -ne 0) {
  56.     Write-Host "ERROR: 7-Zip failed to create archive. Exit code: $($Process.ExitCode)" -ForegroundColor Red
  57.     exit 1
  58. }
  59.  
  60. # Verify archive creation
  61. if (-Not (Test-Path $ArchivePath)) {
  62.     Write-Host "ERROR: Archive was not created." -ForegroundColor Red
  63.     exit 1
  64. }
  65.  
  66. Write-Host "Backup archive created successfully: $ArchivePath" -ForegroundColor Green
  67.  
  68. # Define target storage path on destination drive
  69. $DestinationStorage = Join-Path -Path $DestinationDrive -ChildPath (Split-Path -Leaf $StorageDirectory)
  70.  
  71. # Ensure the destination storage directory exists
  72. if (-Not (Test-Path $DestinationStorage)) {
  73.     Write-Host "Creating destination storage directory: $DestinationStorage"
  74.     New-Item -ItemType Directory -Path $DestinationStorage -Force | Out-Null
  75.  
  76.     # Verify directory creation
  77.     if (-Not (Test-Path $DestinationStorage)) {
  78.         Write-Host "ERROR: Failed to create destination directory. Exiting..." -ForegroundColor Red
  79.         exit 1
  80.     }
  81. }
  82.  
  83. # Clone storage directory while preserving existing archives
  84. Write-Host "Cloning storage directory to destination drive, preserving existing archives..."
  85. Get-ChildItem -Path $StorageDirectory -Filter "*.7z" | ForEach-Object {
  86.     $DestinationPath = Join-Path -Path $DestinationStorage -ChildPath $_.Name
  87.  
  88.     if (-Not (Test-Path $DestinationPath)) {
  89.         Write-Host "Copying: $($_.Name) -> $DestinationStorage"
  90.         Copy-Item -Path $_.FullName -Destination $DestinationPath -Force -Verbose
  91.     } else {
  92.         Write-Host "Skipping: $($_.Name), already exists in destination."
  93.     }
  94. }
  95.  
  96. Write-Host "Backup and storage synchronization completed successfully." -ForegroundColor Green
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement