简体   繁体   中英

Robocopy in Powershell with directory list

I am writing my own backup script to backup some (but not all) directories from my computer. At the moment the directory list is programmed within my (powershell) script file but the perfect way would be an external file list.

This is what I have until now.

Clear-Host
$host.ui.RawUI.WindowTitle = "Backup script"
# get drive name
$driveName="smrbackup"
if ($backupDrive=(Get-PSDrive | Where-Object {$_.Description -eq     $driveName}).root) {
    write-host "found drive"
}
else {
    write-error "no backup drive"
    exit 1
}

$destFolder="robackup"
$dest=Join-Path -Path $backupDrive -ChildPath $destFolder
Write-host "backup destination is $dest"

# robocopy parameters
# /MIR Spiegelt eine Verzeichnisstruktur und löscht Dateien, die in der Quelle gelöscht wurden
# /V verbose output
# /DCOPY:T kopiert Verzeichnistimestamp
$parameters = @("/MIR", "/NP", "/R:3", "/W:2", "/FFT", "/MT:16")



$shutdown= Read-Host "`r`nshutdown after backup? (j/n/q = quit)"
if ($shutdown -eq "j") {
    "Der Computer wird nach dem Backup heruntergefahren!"
}
elseif ($shutdown -eq "q"){
    exit 0
}

cd c:\
# Liste aller zu kopierenden Verzeichnisse
"users\Stefan M\Documents",
"users\Stefan M\Desktop",
"users\Stefan M\Downloads",
"users\Stefan M\Music",
"users\Stefan M\VirtualBox VMs",
"MESS" | % { robocopy $_ ($dest + "/" + $_) $parameters }

Some questions:

  1. I found the last line (pipe and the command) in the internet and it seems to work. Could anyone explain to me whats happening here?
  2. with rsync I can do all the directories in one seesion of rsync and after the backup I have a summary how many files have been copied and if errors occured. With the above line Robocopy is started for every directory so I can hardly see if the previous folder was copied or if an error occured. How can I get a summary of all folders at the end?

For 2 I could build something with a for loop but how can I see the total copy result? Any more elegant than collecting the exit codes?

Cheers, Stefan

edit: I found this line to use a files list. So the remaining question is how can I get a total summary after all robocopy runs?

cd c:\
gc "c:\Users\Stefan Mayrhofer\Documents\script\backup-src.txt" | % { robocopy $_ ($dest + "/" + $_) $parameters }

Your last line creates an array and executes robocopy foreach -line ( % is an alias of foreach ). $_ is a placeholder for the item, that is currently processed.

To get the same result with a file storing your data, create a .txt-file (or .csv if you like) with one path per line. Now use get-content to get the content of that file as array and store it in a variable or use it directly in the pipe:

Get-Content -Path $YourFilePath | foreach { robocopy $_ ($dest + "/" + $_) $parameters }

If you want to collect the results of robocopy, just store them in a variable:

$Output = Get-Content -Path $YourFilePath | foreach { robocopy $_ ($dest + "/" + $_) $parameters }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM