creating more backup scripts!
All checks were successful
Check scripts syntax / check-scripts-syntax (push) Successful in 1m23s
All checks were successful
Check scripts syntax / check-scripts-syntax (push) Successful in 1m23s
This commit is contained in:
52
windows-backup.ps1
Normal file
52
windows-backup.ps1
Normal file
@@ -0,0 +1,52 @@
|
||||
$7zipPath = "$env:ProgramFiles\7-Zip\7z.exe"
|
||||
|
||||
# Check if 7-Zip is installed
|
||||
if (!(Test-Path "$env:ProgramFiles\7-Zip\7z.exe")) {
|
||||
Write-Host "7-Zip is not installed. Please install it to use this script."
|
||||
exit 1
|
||||
}
|
||||
|
||||
$BackupSource = @(
|
||||
"$env:USERPROFILE\Documents",
|
||||
"$env:USERPROFILE\Desktop",
|
||||
"$env:USERPROFILE\Pictures"
|
||||
)
|
||||
|
||||
$NASDestination = "\\OMV\Backup\$env:COMPUTERNAME"
|
||||
$TempDir = "$env:TEMP\BackupTemp"
|
||||
$Date = Get-Date -Format "yyyy-MM-dd"
|
||||
|
||||
# Create temp directory
|
||||
New-Item -ItemType Directory -Path $TempDir -Force
|
||||
|
||||
# Create NAS destination if it doesn't exist
|
||||
if (!(Test-Path $NASDestination)) {
|
||||
New-Item -ItemType Directory -Path $NASDestination -Force
|
||||
}
|
||||
|
||||
foreach ($Folder in $BackupSource) {
|
||||
if (Test-Path $Folder) {
|
||||
$FolderName = Split-Path $Folder -Leaf
|
||||
$ZipFile = "$TempDir\$FolderName-$Date.zip"
|
||||
|
||||
Write-Host "Compressing $Folder..."
|
||||
& "$7zipPath" a -tzip "$ZipFile" "$Folder\*" -mx=9
|
||||
|
||||
Write-Host "Copying $ZipFile to NAS..."
|
||||
Copy-Item $ZipFile $NASDestination -Force
|
||||
|
||||
Write-Host "Removing $ZipFile..."
|
||||
Remove-Item $ZipFile
|
||||
}
|
||||
}
|
||||
|
||||
Write-Host "Removing Files older than 15 days from $NASDestination..."
|
||||
$OldFiles = Get-ChildItem -Path $NASDestination -File | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-15) }
|
||||
foreach ($OldFile in $OldFiles) {
|
||||
Remove-Item $OldFile.FullName -Force
|
||||
Write-Host "Removed: $($OldFile.FullName)"
|
||||
}
|
||||
|
||||
# Cleanup
|
||||
Remove-Item $TempDir -Recurse -Force
|
||||
Write-Host "Backup completed!"
|
Reference in New Issue
Block a user