2

i am new to PowerShell and i have managed to google my way to getting the following PowerShell Script together:

#System Variable for backup Procedure

 $date = Get-Date -Format d.MMMM.yyyy
 New-PSDrive -Name "Backup" -PSProvider Filesystem -Root "\\server>\<path>"
 $source = "\\server>\<path>"
 $destination = "\\<server>\<path>\$date"
 $path = test-Path $destination

Email Variables

 $smtp = "smudmug.mug"
 $from = "James Clegg <email@email.com>"
 $to = "James Clegg <email@email.com>"
 $body = "Hi All, </br></br> Please find the back up log File for <Server>      attached.</br></br> Back up complete on $date</br></br>Kind Regards,</br></br> James"
 $subject = "Backup of <Server> is Complete $date"

Backup Process started

if ($path -eq $true) {
write-Host "Folder Already exists"
Remove-PSDrive "Backup"  
} elseif ($path -eq $false) {
        cd backup:\
        copy-Item -Recurse $source -passthru -Destination $destination
        $backup_log = Dir -Recurse $destination | out-File      "$destination\backup_log.txt"
        $attachment = "$destination\backup_log.txt"
        #Send an Email to User 
        send-MailMessage -SmtpServer $smtp -From $from -To $to -Subject        $subject -Attachments $attachment -Body $body -BodyAsHtml
        write-host "Backup Successful"
        cd c:\

     Remove-PSDrive "Backup"  
     }

   cmd /c pause | out-null

Can someone please help me which what i need to add to allow files in use to be copied.

Kind regards,

James

Community
  • 1
  • 1
  • `robocopy` retries for several seconds then plain fails if it tries to copy a file in use. You may try "Unlocker" utility from Sysinternals to get ahold of files, or try interacting with VSS for copying those files. I don't yet have experience with talking to VSS from Powershell, sorry. – Vesper Jun 26 '15 at 07:28

1 Answers1

2

Copying files that are open in another an application is not trivial. The problem is that even if you could copy the file, there is no guarantee that the file is in usable a state. All but the most simple text files are easily corrupted. Consider the following scenario:

   user           backup
    |                |
 (saves doc)         |
    |                |
(write n chars)      |
    |             (read file)   
(write m chars)   (write copy)
    |                |

What is the desired outcome? From user's point of view, the document should contain either state before save or before it. The actual outcome, only some saved changes, is hardly correct. If the file is more complex a structure, like a spreadsheet, the result can be corrupted beyond repair.

In order to work around this kind of concurrency issue, ther are open file agents. In Windows, there is a built-in VSS (Volume Shadow Copy Service), 3rd party products exist too. The idea is to watch the file for activity and make a consistent copy when all changes are flushed to the disk. This requires co-operation from the application too. For a detailed discussion about VSS, read Technet article. Your best bet is to use an existing backup solution that already has tackled the issue.

vonPryz
  • 22,996
  • 7
  • 54
  • 65