Skip to content

Instantly share code, notes, and snippets.

@TheTrigger
Last active January 3, 2023 14:21
Show Gist options
  • Save TheTrigger/758c5fe82d73a8572eb3bc018961cd6f to your computer and use it in GitHub Desktop.
Save TheTrigger/758c5fe82d73a8572eb3bc018961cd6f to your computer and use it in GitHub Desktop.
Windows Lazy Backup
Set-Variable -Name "username" -Value $env:UserName
Set-Variable -Name "destination" -Value "D:\Backup\20230103"
# D:\Dropbox\Configurazioni\
$dirs = @{
"C:\Users\$username\AppData\Roaming\Sublime Text 3\Local\" = "$destination\Sublime_Text\"
"C:\Users\$username\AppData\Local\Google\Chrome\User Data\" = "$destination\GoogleProfile\"
"C:\Users\$username\AppData\Roaming\AnyDesk\" = "$destination\AnyDesk\"
"C:\Users\$username\AppData\Roaming\DBeaverData\" = "$destination\DBeaver\"
"C:\Users\$username\AppData\Roaming\Mikrotik\Winbox\" = "$destination\Winbox\"
"C:\Users\$username\AppData\Local\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\" = "$destination\WindowsTerminal\"
"C:\Users\$username\.ssh\" = "$destination\.ssh\"
"C:\Users\$username\source\repos\" = "$destination\VS_local_repos\"
"C:\Users\$username\Documents\Teardown\" = "$destination\games\Teardown\"
"C:\Users\$username\Documents\Vault#1\" = "$destination\Obsidian\"
"C:\Program Files (x86)\DrayTek\Smart VPN Client\profiles.cfg" = "$destination\DrayTek\Smart_VPN\profiles.cfg"
}
$option = Read-Host "Please type 1 for BACKUP or 2 to RESTORE"
if ($option -eq 1) {
$dirs.getEnumerator() | ForEach-Object {
Clear-Host
$item = Get-Item $_.key
Write-Host ("Key = " + $_.key + " and Value = " + $_.value);
$DST_path = $_.value;
if ($item.PSIsContainer) {
Write-Output "The item is a folder."
$DST_path = $_.value
New-Item -Path $DST_path -ItemType Directory -ErrorAction Ignore # create base folder
$DST_file_name = (Get-Item -Path $_.key).BaseName # get base name (of original path)
$DST_archive = Join-Path -Path $DST_path -ChildPath ($DST_file_name + '.zip') # get path + file and zip extension
}
else {
Write-Output "The item is a file."
$DST_path = Split-Path $_.value -Parent # get the path without filename
New-Item -Path $DST_path -ItemType Directory -ErrorAction Ignore # create base folder
$DST_file_name = (Get-Item -Path $_.key).BaseName # get base name (of original path)
$DST_archive = Join-Path -Path $DST_path -ChildPath ($DST_file_name + '.zip') # get path + file and zip extension
}
#Write-Host (Join-Path -Path $_.value -ChildPath ($DST_path + ".zip"))
Write-Host 'Sync' $DST_path '->' $DST_archive
try {
#$argu = @(
# $_.key
# $DST_path
# "/NFL"
# "/NDL"
# "/MIR"
#)
#& robocopy @argu
$parameters = @{
Path = Join-Path -Path $_.key -ChildPath '*'
DestinationPath = $DST_archive
CompressionLevel = "Fastest"
}
$errorList = @()
Compress-Archive @parameters -Update -ErrorVariable errorList
if ($errorList.Count -gt 0) {
Write-Output "Errors occurred while creating the archive:"
Write-Output $errorList
}
#read-host "Press ENTER to continue..."
}
catch [System.Exception] {
Write-Error "An exception occurred: $($_.Exception.Message)"
read-host "Press ENTER to continue..."
}
}
}
elseif ($option -eq 2) {
# do something if option 2 is selected
$dirs.getEnumerator() | ForEach-Object {
Clear-Host
$item = Get-Item $_.key
Write-Host ("Key = " + $_.key + " and Value = " + $_.value);
if ($item.PSIsContainer) {
Write-Output "The item is a folder."
$DST_path = $_.value
New-Item -Path $DST_path -ItemType Directory -ErrorAction Ignore # create base folder
$DST_file_name = (Get-Item -Path $_.value).BaseName # get base name
$DST_archive = Join-Path -Path $DST_path -ChildPath ($DST_file_name + '.zip') # get path + file and zip extension
}
else {
Write-Output "The item is a file."
$DST_path = Split-Path $_.value -Parent # get the path without filename
New-Item -Path $DST_path -ItemType Directory -ErrorAction Ignore # create base folder
$DST_file_name = (Get-Item -Path $DST_path).BaseName # get base name
$DST_archive = Join-Path -Path $DST_path -ChildPath ($DST_file_name + '.zip') # get path + file and zip extension
}
$parameters = @{
Path = $DST_archive
DestinationPath = $_.key
}
Expand-Archive @parameters -Force
read-host "Press ENTER to continue..."
}
}
else {
Write-Error "Invalid input, cya"
}
read-host "Job Done! Press ENTER to close..."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment