Created
July 2, 2024 03:29
-
-
Save valadas/99b80d9dcd20f31e7af844fee422431a to your computer and use it in GitHub Desktop.
Does a file backup of a specific subfolder, excluding some glob patterns.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Set the path to the 7-Zip executable | |
$zipExePath = "C:\Program Files\7-Zip\7z.exe" | |
# Define the parent directory from which child folders will be listed | |
$parentDirectory = "C:\wwwroot" | |
# List all child directories | |
$directories = Get-ChildItem -Path $parentDirectory -Directory | |
# Check if there are any directories found | |
if ($directories.Count -eq 0) { | |
Write-Host "No directories found in the parent folder." | |
exit | |
} | |
# Display each directory with an index for the user to select | |
$index = 0 | |
$directories | ForEach-Object { | |
Write-Host "[$index] $($_.Name)" | |
$index++ | |
} | |
# Ask user for choice index | |
$choiceIndex = Read-Host "Please enter the index of the folder to backup" | |
# Validate the input | |
if ($choiceIndex -notmatch '^\d+$' -or $choiceIndex -lt 0 -or $choiceIndex -ge $directories.Count) { | |
Write-Host "Invalid index entered. Exiting script." | |
exit | |
} | |
# Get the selected folder based on user choice | |
$selectedFolder = $directories[$choiceIndex].FullName | |
# Extract just the folder name from the selected path | |
$folderName = Split-Path -Path $selectedFolder -Leaf | |
# Get the current timestamp | |
$timestamp = Get-Date -Format "yyyyMMdd-HHmmss" | |
# Define the output file path with folder name and timestamp | |
$outputFilePath = "C:\Backups\SiteFiles\${folderName}_$timestamp.7z" | |
# List of file extensions to potentially exclude | |
$potentialExcludes = @( | |
"*.pdf", | |
"*.jpg", | |
"*.svg", | |
"*.log.resources", | |
"*.zip", | |
"*.xlsx", | |
"*.log", | |
"*.csv", | |
"*.swf", | |
"*.mp3", | |
"App_Data\ExtensionPackages", | |
"Search", | |
"Portals/*/*.png" | |
) | |
# Initialize the exclusion list | |
$excludeTypes = @() | |
# Ask the user about each file extension | |
foreach ($ext in $potentialExcludes) { | |
$response = Read-Host "Do you want to exclude $ext ? (Y/N, default: Y)" | |
if ($response -eq "" -or $response.ToLower() -eq "y") { | |
$excludeTypes += "-xr!$ext" | |
} | |
} | |
# Log and execute the command | |
$arguments = 'a', '-t7z', "`"$outputFilePath`"", "`"$selectedFolder\*`"", $excludeTypes | |
Write-Host "Executing 7-Zip with command: " -NoNewline | |
Write-Host $arguments -join ' ' | |
# Run the 7zip command | |
& $zipExePath @arguments |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment