Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # "jpsys Automatic Backup.ps1" - Copyright Tim Keal alias jargon 2025 03/03
- # This script automatically creates a full backup for my "Jurassic Park" web interface.
- # Define variables
- $Project = "jpsys"
- $SourceFolder = "C:\xampp-8.1.6-0\projects\sandbox.1.test\public\$Project" # Folder to be archived
- $StorageDirectory = "C:\xampp-8.1.6-0\projects\sandbox.1.test\JP Sys Archives" # Where archives are stored
- $DestinationDrive = "F:\" # Destination drive root
- $SevenZipPath = "C:\Program Files\7-Zip\7z.exe" # Path to 7z.exe (renamed to avoid syntax issues)
- # Get timestamp for unique filename
- $Timestamp = (Get-Date -Format "yyyy-MMdd-HHmm-ssfff") -replace "\d$"
- # Prompt for a caption
- $Caption = Read-Host "Enter a caption for the backup of $($Project)"
- $Caption = $Caption -replace '[\\/:*?"<>|]', "_"
- $Auto = "AUTOMATIC BACKUP"
- if ($Caption.Length -gt 0) {
- $Auto += " + $Caption"
- }
- $Caption = $Auto
- # Define archive name and path
- $ArchiveName = "$Project $Timestamp ($Caption).7z"
- $ArchivePath = Join-Path -Path $StorageDirectory -ChildPath $ArchiveName
- # Debug: Print archive path for validation
- Write-Host "Archive will be created at: $ArchivePath"
- # Ensure 7-Zip exists
- if (-Not (Test-Path $SevenZipPath)) {
- Write-Host "ERROR: 7-Zip executable not found at $SevenZipPath. Check installation." -ForegroundColor Red
- exit 1
- }
- # Ensure source directory exists
- if (-Not (Test-Path $SourceFolder)) {
- Write-Host "ERROR: Source folder '$SourceFolder' does not exist. Aborting backup." -ForegroundColor Red
- exit 1
- }
- # Ensure storage directory exists
- if (-Not (Test-Path $StorageDirectory)) {
- Write-Host "Creating storage directory: $StorageDirectory"
- New-Item -ItemType Directory -Path $StorageDirectory -Force | Out-Null
- }
- # Create the archive
- Write-Host "Creating archive: $ArchivePath"
- $Process = Start-Process -FilePath $SevenZipPath -ArgumentList "a -t7z `"$ArchivePath`" `"$SourceFolder`" -mx9" -NoNewWindow -Wait -PassThru
- # Check if 7-Zip operation succeeded
- if ($Process.ExitCode -ne 0) {
- Write-Host "ERROR: 7-Zip failed to create archive. Exit code: $($Process.ExitCode)" -ForegroundColor Red
- exit 1
- }
- # Verify archive creation
- if (-Not (Test-Path $ArchivePath)) {
- Write-Host "ERROR: Archive was not created." -ForegroundColor Red
- exit 1
- }
- Write-Host "Backup archive created successfully: $ArchivePath" -ForegroundColor Green
- # Define target storage path on destination drive
- $DestinationStorage = Join-Path -Path $DestinationDrive -ChildPath (Split-Path -Leaf $StorageDirectory)
- # Ensure the destination storage directory exists
- if (-Not (Test-Path $DestinationStorage)) {
- Write-Host "Creating destination storage directory: $DestinationStorage"
- New-Item -ItemType Directory -Path $DestinationStorage -Force | Out-Null
- # Verify directory creation
- if (-Not (Test-Path $DestinationStorage)) {
- Write-Host "ERROR: Failed to create destination directory. Exiting..." -ForegroundColor Red
- exit 1
- }
- }
- # Clone storage directory while preserving existing archives
- Write-Host "Cloning storage directory to destination drive, preserving existing archives..."
- Get-ChildItem -Path $StorageDirectory -Filter "*.7z" | ForEach-Object {
- $DestinationPath = Join-Path -Path $DestinationStorage -ChildPath $_.Name
- if (-Not (Test-Path $DestinationPath)) {
- Write-Host "Copying: $($_.Name) -> $DestinationStorage"
- Copy-Item -Path $_.FullName -Destination $DestinationPath -Force -Verbose
- } else {
- Write-Host "Skipping: $($_.Name), already exists in destination."
- }
- }
- Write-Host "Backup and storage synchronization completed successfully." -ForegroundColor Green
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement