Synchronize Production & DR Filespaces in PowerShell

  • Updated

Script links and mounts Production and DR Filespaces as separate instances on the same machine, performs a Robocopy mirror of the complete directory tree to the DR Filespace.

Ensures only 1 instance is running, therefore can be scheduled as a task to complete without overlap. Waits on all uploads to complete before synchronizing metadata, file data and unlinking. 

update <prod.filespace> and <dr.filespace> names, <rootpassword> with your individual Filespace variables and 'root' user passwords.

Schedule:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File "prod_dr_sync.ps1"

Script:

Function CheckRunning {
        [CmdletBinding()]
        Param (
            [Parameter(Mandatory=$true)]
            [ValidateNotNullorEmpty()]
            [String]$ScriptName
        )
        $PsScriptsRunning = get-wmiobject win32_process | where{$_.processname -eq 'powershell.exe'} | select-object commandline,ProcessId


        ForEach ($PsCmdLine in $PsScriptsRunning){
            [Int32]$OtherPID = $PsCmdLine.ProcessId
            [String]$OtherCmdLine = $PsCmdLine.commandline
                If (($OtherCmdLine -match $ScriptName) -And ($OtherPID -ne $PID) ){
                Write-host "[$ScriptName] already running"
                Start-Sleep -Second 5
                Exit           
            }
        }
    } 

function Link {
 Start-Process -WindowStyle hidden -FilePath "C:\Program Files\Lucid\Resources\Lucid.exe" -ArgumentList "--instance 1 daemon --fs <prod.filespace> --user root --password <rootpassword> --mount-point c:\prod"  
 Start-Process -WindowStyle hidden -FilePath "C:\Program Files\Lucid\Resources\Lucid.exe" -ArgumentList "--instance 2 daemon --fs <dr.filespace> --user root --password <rootpassword> --mount-point c:\dr"   
}

function ProdPath {
do {
$prod = "c:\prod"
$ProdExists = Test-Path $prod
If ($ProdExists -eq $False) {sleep 1}
} until ($ProdExists -eq $True)
}

function BkupPath {
do {
$bkup = "c:\dr"
$BkupExists = Test-Path $bkup
If ($BkupExists -eq $False) {sleep 1}
} until ($BkupExists -eq $True)
}

function Backup {
 C:\Windows\System32\Robocopy.exe c:\prod c:\dr /Z /E /R:0 /W:0 /MIR /COPYALL /NP
}

function Instance1 {
do { 
   $dirtyBytes = (lucid --instance 1 perf --cache dirtyBytes --seconds 0 --no-timestamp) -replace '[^0-9]' #get dirtyBytes, reduce to number value
   If ($? -ne "True") {Break} #break do if invoke-restmethod errors
   sleep 1 #reduce CPU load
   } until ($dirtyBytes -eq 0) #exit when dirtyBytes=0
}

function Instance2 {
do { 
   $dirtyBytes = (lucid --instance 2 perf --cache dirtyBytes --seconds 0 --no-timestamp) -replace '[^0-9]' #get dirtyBytes, reduce to number value
   If ($? -ne "True") {Break} #break do if invoke-restmethod errors
   sleep 1 #reduce CPU load
   } until ($dirtyBytes -eq 0) #exit when dirtyBytes=0
}

function Sync {
 Start-Process -WindowStyle hidden -FilePath "C:\Program Files\Lucid\Resources\Lucid.exe" -ArgumentList "--instance 1 sync" -Wait
 Start-Process -WindowStyle hidden -FilePath "C:\Program Files\Lucid\Resources\Lucid.exe" -ArgumentList "--instance 2 sync" -Wait
}

function Unlink {
 Start-Process -WindowStyle hidden -FilePath "C:\Program Files\Lucid\Resources\Lucid.exe" -ArgumentList "--instance 1 unlink" -Wait
 Start-Process -WindowStyle hidden -FilePath "C:\Program Files\Lucid\Resources\Lucid.exe" -ArgumentList "--instance 2 unlink" -Wait
}

function LLExit {
 Start-Process -WindowStyle hidden -FilePath "C:\Program Files\Lucid\Resources\Lucid.exe" -ArgumentList "--instance 1 exit" -Wait
 Start-Process -WindowStyle hidden -FilePath "C:\Program Files\Lucid\Resources\Lucid.exe" -ArgumentList "--instance 2 exit" -Wait
 }

function SnapshotDR {
  $time = (get-date).ToString('T')
  $time = $time -replace '[a-z,:]'
  Start-Process -WindowStyle hidden -FilePath "C:\Program Files\Lucid\Resources\Lucid.exe" -ArgumentList "--instance 2 snapshot --create $time --password <rootpassword>"
  }

#Check if running
$ScriptName = $MyInvocation.MyCommand.Name #Get name of current script
CheckRunning -ScriptName $ScriptName

#Link
Link

#Await paths
ProdPath
BkupPath

#Sync
Sync

#Backup
Backup

#Sync
Sync

#Cache flush
Instance1
Instance2

#Snapshot DR with basic timestamp
#SnapshotDR

#Unlink
Unlink

#Exit LucidLink
LLExit
optional: uncomment "SnapshotDR" function to snapshot the Filespace. you must provide your 'root' password. update <rootpassword> accordingly.
 
 

Was this article helpful?

0 out of 0 found this helpful