Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # "Keal's Automatic Backup.ps1" - Copyright Tim Keal alias jargon 2025 03/03
- # This script automatically creates a full backup for the given project
- param (
- [string]$BaseFolder = (Get-Location).Path,
- [string]$HostFolder = "\public",
- [string]$Project = "",
- [string]$Archive = ""
- )
- # Normalize project/archive naming
- if ([string]::IsNullOrWhiteSpace($Project))
- {
- Write-Host "ERROR: No project name was provided. Use -Project to specify one." -ForegroundColor Red
- exit 1
- }
- if ([string]::IsNullOrWhiteSpace($Archive))
- {
- $Archive = $Project
- }
- # Set core paths
- $SourceFolder = Join-Path -Path $BaseFolder -ChildPath $HostFolder.TrimStart("\") # Folder to be archived
- $StorageDirectory = Join-Path -Path $BaseFolder -ChildPath "$Archive Archives" # Where archives are stored
- $DestinationDrive = "F:\" # Destination drive root
- $SevenZipPath = "C:\Program Files\7-Zip\7z.exe" # Path to 7z.exe
- # Generate 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 '[\\/:*?"<>|]', "_" # Sanitize illegal characters
- $Auto = "AUTOMATIC BACKUP"
- if ($Caption.Length -gt 0) { $Auto += " + $Caption" }
- $ArchiveName = "$Project $Timestamp ($Auto).7z"
- $ArchivePath = Join-Path -Path $StorageDirectory -ChildPath $ArchiveName
- # Announce archive path
- Write-Host "Archive will be created at: $ArchivePath"
- # Validate 7-Zip path
- if (-Not (Test-Path $SevenZipPath)) {
- Write-Host "ERROR: 7-Zip not found at $SevenZipPath" -ForegroundColor Red
- exit 1
- }
- # Validate source folder
- if (-Not (Test-Path $SourceFolder)) {
- Write-Host "ERROR: Source folder '$SourceFolder' does not exist." -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
- }
- # Run 7-Zip to create archive
- Write-Host "Creating archive..."
- $Process = Start-Process -FilePath $SevenZipPath `
- -ArgumentList "a", "-t7z", "`"$ArchivePath`"", "`"$SourceFolder`"", "-mx9" `
- -NoNewWindow -Wait -PassThru
- # Check for success
- if ($Process.ExitCode -ne 0) {
- Write-Host "ERROR: 7-Zip failed to create archive. Exit code: $($Process.ExitCode)" -ForegroundColor Red
- exit 1
- }
- 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 destination path
- $DestinationStorage = Join-Path -Path $DestinationDrive -ChildPath (Split-Path -Leaf $StorageDirectory)
- # Ensure destination exists
- if (-Not (Test-Path $DestinationStorage)) {
- Write-Host "Creating destination storage directory: $DestinationStorage"
- New-Item -ItemType Directory -Path $DestinationStorage -Force | Out-Null
- if (-Not (Test-Path $DestinationStorage)) {
- Write-Host "ERROR: Failed to create destination directory." -ForegroundColor Red
- exit 1
- }
- }
- # Sync .7z files from local archive storage to destination
- Write-Host "Syncing .7z archives to destination drive..."
- Get-ChildItem -Path $StorageDirectory -Filter "*.7z" | ForEach-Object {
- $DestFile = Join-Path -Path $DestinationStorage -ChildPath $_.Name
- if (-Not (Test-Path $DestFile)) {
- Write-Host "Copying: $($_.Name) -> $DestinationStorage"
- Copy-Item -Path $_.FullName -Destination $DestFile -Force
- } else {
- Write-Host "Skipping existing: $($_.Name)"
- }
- }
- Write-Host "Backup and sync completed successfully." -ForegroundColor Green
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement