Advertisement
jargon

Keal's Backup Utility.ps1

Apr 23rd, 2025
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # "Keal's Automatic Backup.ps1" - Copyright Tim Keal alias jargon 2025 03/03
  2. # This script automatically creates a full backup for the given project
  3.  
  4. param (
  5.     [string]$BaseFolder = (Get-Location).Path,
  6.     [string]$HostFolder = "\public",
  7.     [string]$Project = "",
  8.     [string]$Archive = ""
  9. )
  10.  
  11. # Normalize project/archive naming
  12. if ([string]::IsNullOrWhiteSpace($Project))
  13. {
  14.     Write-Host "ERROR: No project name was provided. Use -Project to specify one." -ForegroundColor Red
  15.     exit 1
  16. }
  17.  
  18. if ([string]::IsNullOrWhiteSpace($Archive))
  19. {
  20.     $Archive = $Project
  21. }
  22.  
  23. # Set core paths
  24. $SourceFolder = Join-Path -Path $BaseFolder -ChildPath $HostFolder.TrimStart("\")     # Folder to be archived
  25. $StorageDirectory = Join-Path -Path $BaseFolder -ChildPath "$Archive Archives"        # Where archives are stored
  26. $DestinationDrive = "F:\"                                                              # Destination drive root
  27. $SevenZipPath = "C:\Program Files\7-Zip\7z.exe"                                        # Path to 7z.exe
  28.  
  29. # Generate timestamp for unique filename
  30. $Timestamp = (Get-Date -Format "yyyy-MMdd-HHmm-ssfff") -replace "\d$"
  31.  
  32. # Prompt for a caption
  33. $Caption = Read-Host "Enter a caption for the backup of $Project"
  34. $Caption = $Caption -replace '[\\/:*?"<>|]', "_"    # Sanitize illegal characters
  35.  
  36. $Auto = "AUTOMATIC BACKUP"
  37. if ($Caption.Length -gt 0) { $Auto += " + $Caption" }
  38.  
  39. $ArchiveName = "$Project $Timestamp ($Auto).7z"
  40. $ArchivePath = Join-Path -Path $StorageDirectory -ChildPath $ArchiveName
  41.  
  42. # Announce archive path
  43. Write-Host "Archive will be created at: $ArchivePath"
  44.  
  45. # Validate 7-Zip path
  46. if (-Not (Test-Path $SevenZipPath)) {
  47.     Write-Host "ERROR: 7-Zip not found at $SevenZipPath" -ForegroundColor Red
  48.     exit 1
  49. }
  50.  
  51. # Validate source folder
  52. if (-Not (Test-Path $SourceFolder)) {
  53.     Write-Host "ERROR: Source folder '$SourceFolder' does not exist." -ForegroundColor Red
  54.     exit 1
  55. }
  56.  
  57. # Ensure storage directory exists
  58. if (-Not (Test-Path $StorageDirectory)) {
  59.     Write-Host "Creating storage directory: $StorageDirectory"
  60.     New-Item -ItemType Directory -Path $StorageDirectory -Force | Out-Null
  61. }
  62.  
  63. # Run 7-Zip to create archive
  64. Write-Host "Creating archive..."
  65. $Process = Start-Process -FilePath $SevenZipPath `
  66.     -ArgumentList "a", "-t7z", "`"$ArchivePath`"", "`"$SourceFolder`"", "-mx9" `
  67.     -NoNewWindow -Wait -PassThru
  68.  
  69. # Check for success
  70. if ($Process.ExitCode -ne 0) {
  71.     Write-Host "ERROR: 7-Zip failed to create archive. Exit code: $($Process.ExitCode)" -ForegroundColor Red
  72.     exit 1
  73. }
  74.  
  75. if (-Not (Test-Path $ArchivePath)) {
  76.     Write-Host "ERROR: Archive was not created." -ForegroundColor Red
  77.     exit 1
  78. }
  79.  
  80. Write-Host "Backup archive created successfully: $ArchivePath" -ForegroundColor Green
  81.  
  82. # Define destination path
  83. $DestinationStorage = Join-Path -Path $DestinationDrive -ChildPath (Split-Path -Leaf $StorageDirectory)
  84.  
  85. # Ensure destination exists
  86. if (-Not (Test-Path $DestinationStorage)) {
  87.     Write-Host "Creating destination storage directory: $DestinationStorage"
  88.     New-Item -ItemType Directory -Path $DestinationStorage -Force | Out-Null
  89.  
  90.     if (-Not (Test-Path $DestinationStorage)) {
  91.         Write-Host "ERROR: Failed to create destination directory." -ForegroundColor Red
  92.         exit 1
  93.     }
  94. }
  95.  
  96. # Sync .7z files from local archive storage to destination
  97. Write-Host "Syncing .7z archives to destination drive..."
  98. Get-ChildItem -Path $StorageDirectory -Filter "*.7z" | ForEach-Object {
  99.     $DestFile = Join-Path -Path $DestinationStorage -ChildPath $_.Name
  100.  
  101.     if (-Not (Test-Path $DestFile)) {
  102.         Write-Host "Copying: $($_.Name) -> $DestinationStorage"
  103.         Copy-Item -Path $_.FullName -Destination $DestFile -Force
  104.     } else {
  105.         Write-Host "Skipping existing: $($_.Name)"
  106.     }
  107. }
  108.  
  109. Write-Host "Backup and sync completed successfully." -ForegroundColor Green
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement