Files
ScriptCollection/Backup2ExternalHD.ps1
2021-11-06 14:03:55 +01:00

63 lines
1.9 KiB
PowerShell

$SourcePath = "E:\Backup"
$BackupPath = "G:\Backup\GigabyteWinServer"
function Copy-Dir($Source,$Destination){
Write-Host("Copy Folder: $Source to Folder: $Destination")
$files = Get-ChildItem $Source -Recurse
foreach ($f in $files){
$dest = $f.FullName.Replace($Source,$Destination)
if ($f.PSIsContainer){
$n = New-Item -ItemType Directory -Force -Path $dest
} else {
if ($f.Length -gt 10000000){
#Start-BitsTransfer -Source $f.FullName -Destination $dest
} else {
#Copy-Item -Path $f.FullName -Destination $dest
}
}
}
}
function Backup-Data($SourcePath,$BackupPath){
$sources = Get-ChildItem -Path $SourcePath
$sources = $Sources | Sort-Object -Property FullName
$BackupDrive = $BackupPath -replace(':.*','')
$move = $true
foreach($f in $sources){
$Source = $f.FullName
if ($f -eq $sources[$sources.Length-1]){
Write-Host("Copying: $f")
$move = $false
} else {
Write-Host("Moving: $f")
$move = $true
}
$foldersize = (Get-ChildItem -path $Source -Recurse | Measure-Object -Property Length -sum).Sum / 1Gb
$free = (Get-PSDrive -Name $BackupDrive).Free / 1Gb
Write-Host(" Backup-Data-Size: $foldersize (free: $free)")
if ($foldersize -lt $free){
$Destination = $BackupPath+"\"+$f
Write-Host(" Source-Path: $Source")
Write-Host(" Target-Path: $Destination")
Copy-Dir -Source $SourcePath -Destination $Destination
if ($move){
Remove-Item -Recurse -Force $Source
}
} else {
Write-Host("ERROR: Not enough free space on Backup-Drive!")
}
}
}
#main
if (Test-Path $BackupPath){
if (Test-Path $SourcePath){
Backup-Data -SourcePath $SourcePath -BackupPath $BackupPath
} else {
Write-Host("ERROR: Source Path: $SourcePath isn't available!")
}
} else {
Write-Host("ERROR: Backup Path: $BackupPath isn't available!")
}