Page 1 of 1

Powershell and Robocopy - script to backup production files

Posted: 2021-Jul-25, 10:49 am
by MigrationUser
22 Nov 2011 21:13
carltonjacobson

Hi all,

This is my first attempt at using either Powershell or Robocopy, so I went into this doubly blind. Perhaps others will be able to use this script or not make some of the simple time consuming mistakes I did. This is still a work in progress, perhaps someone else who has used Powershell together with Robocopy will have some suggestions? One thing I am going to work on his how to parse and log errors that are not going to be captured in the robocopy log file I am creating.

The goal was to make backups of files from production of all files, using directories and names of files in another directory. There was also some needing to parse the file names for production items that are being renamed and retired. That sounds confusing even to me, so here is an example:

These files and folders exist in the holding directory:
holding-directory\incident-number\COMMANDS\job1.rex
holding-directory\incident-number\COMMANDS\job2.rex
holding-directory\incident-number\COMMANDS\SPECIALS\incident-number-job3.rex <--BEING RETIRED, FILE HAS SPECIAL NAME

Now, we need to copy the folder structure and files from the production directory to the backup directory, to backup the production files
production-directory\COMMANDS\job1.rex -> copy to backup-directory\incident-number\COMMANDS\job1.rex
production-directory\COMMANDS\job2.rex -> copy to backup-directory\incident-number\COMMANDS\job2.rex
production-directory\COMMANDS\SPECIALS\job3.rex -> copy to backup-directory\incident-number\COMMANDS\SPECIALS\job3.rex

I accomplished this using powerscript and robocopy. It took me a long time to find out about using the ampersand (&) preceding calling robocopy to force robocopy to parse what I was sending to it correctly. However once finding that out, I was able to do a lot more than I was in the batch files I was using before, which were getting confusing and also would not output errors. I also had to make sure not to pass an empty list of files to robocopy or it would start copying the entire production directory to my test backup directory.

Here is the script I wrote:

backup-test.ps1
----------------

Code: Select all

$root = "U:\test\copytest" 
$backupRoot = "U:\backup"
$incNumber = "incident-number"
$copySource = "R:\commands\"
$roboOptions = @("/is", "/s", "/log+:U:\log\$incNumber-backup-log.txt", "/np", "/v", "/r:0", "/w:0")
                                                 
Function RoboFunction($directory)
{
   $copyTarget = $backupRoot + $directory.Replace(($root + "\" + $incNumber), "")
   $contents = dir $directory | Where-Object {-not $_.PsIsContainer}
   $contents = $contents -Replace(($incnumber + "-"), "")
   
# The below check if $contents contains data and only execute robocopy then,
# avoiding passing an emptpy value to robocopy and copying the entire directory

   if ($contents)
     {
      &robocopy $copySource $copyTarget $contents $roboOptions
     } 
}

$directories = dir $root\$incNumber\ -recurse | Where-Object {$_.PsIsContainer} 
foreach ($d in $directories)
  {
   RoboFunction ($d.FullName)
  }
--------------------

Cheers,
Carlton