backup-configs.ps1
· 2.6 KiB · PowerShell
Raw
<#
.SYNOPSIS
Scans Program Files directories on a source drive for critical config/license files
and mirrors them to a backup location using Robocopy, preserving directory structure.
.DESCRIPTION
1. Scans "Program Files" and "Program Files (x86)" on the source drive.
2. Looks for specific extensions (.ini, .lic, .db, etc.).
3. Calculates the destination path by swapping the drive root.
4. Uses Robocopy to copy the file (and its attributes/timestamps).
#>
# --- CONFIGURATION ---
$SourceDrive = "D:"
$BackupRoot = "C:\shared\share\backups\rtp\lynch"
# The folders to scan on the Source Drive
$ScanPaths = @(
"$SourceDrive\Program Files",
"$SourceDrive\Program Files (x86)"
)
# The extensions to look for
$Extensions = @(
"*.ini", "*.conf", "*.cfg", "*.lic", "*.key",
"*.mdb", "*.db", "*.sqlite", "*.dat", "*.xml",
"*.properties", "*.config"
)
# ---------------------
foreach ($scanPath in $ScanPaths) {
if (Test-Path $scanPath) {
Write-Host "Scanning $scanPath for critical files..." -ForegroundColor Cyan
# Get all matching files
$foundFiles = Get-ChildItem -Path $scanPath -Include $Extensions -Recurse -ErrorAction SilentlyContinue
foreach ($file in $foundFiles) {
# 1. Determine the relative path (e.g., \Program Files\App\config.ini)
# We strip the "D:" part from the FullName
$relativePath = $file.FullName.Substring($SourceDrive.Length)
# 2. Build the destination file path
# Result: C:\backup\lynch\Program Files\App\config.ini
$destFileFull = Join-Path -Path $BackupRoot -ChildPath $relativePath
# 3. Get the destination FOLDER (Robocopy needs the folder, not the file name)
$destFolder = Split-Path -Path $destFileFull -Parent
# 4. Get just the file name for Robocopy
$fileName = $file.Name
# 5. Run Robocopy
# Source folder is the parent of the found file
$srcFolder = $file.DirectoryName
Write-Host "Backing up: $fileName" -ForegroundColor Gray
# /COPY:DAT = Copy Data, Attributes, Timestamps
# /R:0 /W:0 = No retries (fail fast if locked)
# /NJH /NJS = No Job Header/Summary (keeps output clean)
# /NFL /NDL = No File/Dir List (we already printed the name)
# /XJ = Exclude Junction points
robocopy "$srcFolder" "$destFolder" "$fileName" /COPY:DAT /R:0 /W:0 /NJH /NJS /NFL /NDL /XJ
}
} else {
Write-Warning "Path not found: $scanPath"
}
}
Write-Host "`nBackup Complete." -ForegroundColor Green
| 1 | <# |
| 2 | .SYNOPSIS |
| 3 | Scans Program Files directories on a source drive for critical config/license files |
| 4 | and mirrors them to a backup location using Robocopy, preserving directory structure. |
| 5 | |
| 6 | .DESCRIPTION |
| 7 | 1. Scans "Program Files" and "Program Files (x86)" on the source drive. |
| 8 | 2. Looks for specific extensions (.ini, .lic, .db, etc.). |
| 9 | 3. Calculates the destination path by swapping the drive root. |
| 10 | 4. Uses Robocopy to copy the file (and its attributes/timestamps). |
| 11 | #> |
| 12 | |
| 13 | # --- CONFIGURATION --- |
| 14 | $SourceDrive = "D:" |
| 15 | $BackupRoot = "C:\shared\share\backups\rtp\lynch" |
| 16 | |
| 17 | # The folders to scan on the Source Drive |
| 18 | $ScanPaths = @( |
| 19 | "$SourceDrive\Program Files", |
| 20 | "$SourceDrive\Program Files (x86)" |
| 21 | ) |
| 22 | |
| 23 | # The extensions to look for |
| 24 | $Extensions = @( |
| 25 | "*.ini", "*.conf", "*.cfg", "*.lic", "*.key", |
| 26 | "*.mdb", "*.db", "*.sqlite", "*.dat", "*.xml", |
| 27 | "*.properties", "*.config" |
| 28 | ) |
| 29 | # --------------------- |
| 30 | |
| 31 | foreach ($scanPath in $ScanPaths) { |
| 32 | if (Test-Path $scanPath) { |
| 33 | Write-Host "Scanning $scanPath for critical files..." -ForegroundColor Cyan |
| 34 | |
| 35 | # Get all matching files |
| 36 | $foundFiles = Get-ChildItem -Path $scanPath -Include $Extensions -Recurse -ErrorAction SilentlyContinue |
| 37 | |
| 38 | foreach ($file in $foundFiles) { |
| 39 | # 1. Determine the relative path (e.g., \Program Files\App\config.ini) |
| 40 | # We strip the "D:" part from the FullName |
| 41 | $relativePath = $file.FullName.Substring($SourceDrive.Length) |
| 42 | |
| 43 | # 2. Build the destination file path |
| 44 | # Result: C:\backup\lynch\Program Files\App\config.ini |
| 45 | $destFileFull = Join-Path -Path $BackupRoot -ChildPath $relativePath |
| 46 | |
| 47 | # 3. Get the destination FOLDER (Robocopy needs the folder, not the file name) |
| 48 | $destFolder = Split-Path -Path $destFileFull -Parent |
| 49 | |
| 50 | # 4. Get just the file name for Robocopy |
| 51 | $fileName = $file.Name |
| 52 | |
| 53 | # 5. Run Robocopy |
| 54 | # Source folder is the parent of the found file |
| 55 | $srcFolder = $file.DirectoryName |
| 56 | |
| 57 | Write-Host "Backing up: $fileName" -ForegroundColor Gray |
| 58 | |
| 59 | # /COPY:DAT = Copy Data, Attributes, Timestamps |
| 60 | # /R:0 /W:0 = No retries (fail fast if locked) |
| 61 | # /NJH /NJS = No Job Header/Summary (keeps output clean) |
| 62 | # /NFL /NDL = No File/Dir List (we already printed the name) |
| 63 | # /XJ = Exclude Junction points |
| 64 | robocopy "$srcFolder" "$destFolder" "$fileName" /COPY:DAT /R:0 /W:0 /NJH /NJS /NFL /NDL /XJ |
| 65 | } |
| 66 | } else { |
| 67 | Write-Warning "Path not found: $scanPath" |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | Write-Host "`nBackup Complete." -ForegroundColor Green |