Created
April 15, 2023 16:42
-
-
Save giovannicandido/62cf574936dfd99af08770d899e6ade1 to your computer and use it in GitHub Desktop.
A simple Powershell script to backup winge programs and WSL distros
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
param( | |
[Parameter(Mandatory)] | |
[string]$Backup | |
) | |
$DateOfBackup = $(Get-Date -Format "dd-MM-yyyy_HH-mm") | |
# Function to clean up files | |
function Clean-Up-Files { | |
param ( | |
[Parameter(Mandatory)] | |
$TargetFolder, | |
[Parameter(Mandatory)] | |
$Days, | |
[Parameter(Mandatory)] | |
$Extension | |
) | |
$Now = Get-Date | |
$LastWrite = $Now.AddDays(-$Days) | |
$Files = Get-ChildItem $TargetFolder -include $Extension -Recurse | where {$_.LastWriteTime -le "$LastWrite"} | |
foreach ($File in $Files) | |
{ | |
if($File -ne $null) { | |
Write-Host "Deleting File $File" -BackgroundColor "DarkRed" | |
Remove-Item $File.FullName | Out-Null | |
} else { | |
Write-Host "No more files to delete" -ForegroundColor "Green" | |
} | |
} | |
} | |
# Backup Programs | |
function Backup-Programs { | |
Clean-Up-Files -TargetFolder "D:\winget" -Extension "*.json" -Days "7" | |
winget export d:\winget\winget-lista-$DateOfBackup.json | |
} | |
# Backup WSL | |
function Backup-WSL { | |
Clean-Up-Files -TargetFolder "D:\LinuxDistros\Backups" -Extension "*.tar" -Days "7" | |
wsl --export OracleLinux_9 D:\LinuxDistros\Backups\OracleLinux_9-$DateOfBackup.tar | |
} | |
Switch ($Backup) { | |
"wsl" { | |
Backup-WSL; | |
Break | |
} | |
"programs" { | |
Backup-Programs; | |
Break | |
} | |
Default { | |
Write-Host "Backup param needs to be wsl or programs" | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment