Powershell

Mirror Hard drives without using RAID

For home lab, I wanted to maximize disk space while still having redundancy for certain folders let's see how to Mirror Hard drives without using RAID.

When we think of traditional hard drive mirrors with RAID technology, we are usually talking about a RAID controller either software or hardware based that is performing parity operations on two hard drives and present the two hard drives as one hard drive or volume to Windows. ย The downside to mirroring or RAID1 as it is otherwise known is that you lose half of your available disk space. ย So 2 TB shows as 1 TB, etc. ย Let’s see how we can Mirror Hard drives without using RAID.

Mirror Hard drives without using RAID

To be totally transparent and forthcoming here, if we are talking about Enterprise applications or boot drives for servers, there is no substitute for a mirrored set of drives using a RAID controller and this is definitely best practice. However, in a home lab, where drive space is more the desired outcome, it made me think about mirroring my 4TB drives and losing 4TB of space simply to mirror mainly a few select folders on those drives.

What I chose to do was rather select one of the drives as a primary storage location for certain shares setup in the home lab network, and then using PowerShell and Robocopy, create a task that mirrors those select folders to the other 4TB drive. This way I have all 8TB available for usage and still have some redundancy of the files in the folders between the two. ย This use case make work best if the following is true:

  • The files are not being changed every few seconds or minutes (very true of a home lab scenario)
  • You only have a select number of folders and or data that you want to protect. ย So not every single byte of one hard drive protected. ย (also true of my home lab).
  • A scheduled task that runs the mirror job setup at a particular interval will be enough redundancy for you to sleep at night. ย (May not be true of all, but true in my case with the files in question).

Using Powershell with Robocopy

Using the powerful nature of PowerShell we can easily loop through a list of folders and mirror the folders we place in a simple text file along with logging the action. Here is the PowerShell script.

$logtime = Get-Date -Format "MM-dd-yyyy_hh-mm-ss"
$logfile='c:mydirlogrobo_'+$logtime+".log"
$dirs = get-content c:mydirdirs.txt

foreach ($dir in $dirs) { 
robocopy d:$dir e:$dir /MIR /Z /XA:H /W:5 | out-file $logfile -append
}

The above short script is fairly intuitive. In the

  • $logtime variable we are getting the current date and time.
  • $logfile variable adds that captured time to the name of our logfile that we are generating
  • $dirs variable is the text file that we simply place the name of the source folders we want to mirror. This way the script is modular if we add folders we want to mirror in the future.
  • foreach” section, the D: drive is my primary 4TB hard drive that is the target of a few shares that I want to mirror. The E: drive is the secondary 4TB hard drive that serves as my mirrored copy.

Using robocopy we perform the mirror function. Below are what the various switches do.

/MIR – tells Robocopy to mirror. A caution here is that using mirror can mean that files are deleted in the destination folder but not the source. So keep that in mind.
/Z – Robocopy can resume the transfer
/XA:H – This ignores hidden files
/W:5 – this lowers the error wait timeout

Scheduled Task

Now to create the Scheduled Task, we can simply create a task that launches our PowerShell file to run the robocopy job as often as we want. ย Be sure to name the task and select toย run whether user is logged on or not as well asย run with highest privileges.

robomir01

On theย Action tab, we can populate the appropriate parameters for our script. ย In theย Program/script box, we add the directive for PowerShell itself.

C:WINDOWSsystem32windowspowershellv1.0powershell.exe

In theย add argumentsย field we pass the script itself, along with the edits to the execution policy on the fly.

-command C:mydirrobo.ps1 -executionpolicy bypass

Also, to alleviate issues with scheduled tasks, I always populate theย Start in box with the working directory of the script.

robomir02

Thoughts

Using PowerShell, Robocopy, and scheduled tasks we can effectivelyย Mirror Hard drives without using RAID. ย Again this is a special use case here especially for a home lab where I wanted to have the most available disk space as opposed to strict redundancy.

Subscribe to VirtualizationHowto via Email ๐Ÿ””

Enter your email address to subscribe to this blog and receive notifications of new posts by email.



Brandon Lee

Brandon Lee is the Senior Writer, Engineer and owner at Virtualizationhowto.com, and a 7-time VMware vExpert, with over two decades of experience in Information Technology. Having worked for numerous Fortune 500 companies as well as in various industries, He has extensive experience in various IT segments and is a strong advocate for open source technologies. Brandon holds many industry certifications, loves the outdoors and spending time with family. Also, he goes through the effort of testing and troubleshooting issues, so you don't have to.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.