Advertisement
jargon

jpsys Automatic Backup.ps1

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