I wrote a Powershell script which does following Steps.
- It RoboCopies Files from a Server to an External Hard drive (incremental backup)
- Next time it is supposed to check if any files were deleted on the Server, if so move those Files from the Backup Folder to a Folder Called _DeletedFiles in the Backup Hard drive.
- RoboCopy with /MIR (which will delete the files which are deleted on the server also on the backup, and that's okay because I saved them already on the _DeletedFiles Folder)
Point of this _DeletedFiles Folder is that even if someone deleted a file we want to keep it somewhere for at least 60 Days.
Now this script is a little bit more complex including writing to log file, testing paths, and first run if statement etc.
All seems to work except the step where I want to copy the Files which are deleted on the Server from the BackUp to a new Folder.
This step looks similar to this:
$testfolder = Test-Path "$UsbDisk\$backupFolder"
# If a Backup folder already exist then Compare files and see if any changes have been made
if ( $testfolder -eq $true ) { #IF_2
# Copy deleted files to BackUp Folder
MyLog "Check for Deleted Files on E:\" 0
$source = "E:\"
$sourcelist = Get-ChildItem $source -Recurse
$destination = "$UsbDisk\$backupFolder\Data_01\_DeletedFiles"
foreach ($file in $sourcelist){
$result = test-path -path "E:\*" -include $file
if ($result -like "False"){
Copy-Item $file -Destination "$destination"
}
}
# Start Synchronizing E:\
MyLog "Start Synchronizing E:\" 0
Robocopy "E:\" "$UsbDisk\$backupFolder\Data_01" /mir /r:2 /w:3 /M /XD VM_*
MyLog "E:\ is up to Date" 0
# Copy deleted files to BackUp Folder
MyLog "Check for Deleted Files on F:\" 0
$source = "F:\"
$sourcelist = Get-ChildItem $source -Recurse
$destination = "$UsbDisk\$backupFolder\Data_02\_DeletedFiles"
foreach ($file in $sourcelist){
$result = test-path -path "F:\*" -include $file
if ($result -like "False"){
Copy-Item $file -Destination "$destination"
# Then Delete it from the BackUp
}
}
# Start Synchronizing F:\
MyLog "Start Synchronizing F:\" 0
Robocopy "F:\" "$UsbDisk\$backupFolder\Data_02" /mir /r:2 /w:3 /M /XD VM_*
MyLog "F:\ is up to Date" 0
}
The error I get that files can't be copied because they do not exist at the destination; however, it tries to copy files which shouldn't be copied in the first place.
I wonder if anyone has an idea to solve this more elegant, or how to fix my code snip.