Powershell Script to Copy Full VM (.VBK) Backup Files to External Drive (excluding .VIB)

by Danny
0 comment

READING TIME: 5 MINUTES

The title is a little wonky, but let me explain. We use Veeam for backups at our company. We have a pretty standard backup procedure: incremental backups are taken on weekdays, and full VM backups are taken on weekends. Each backup job goes to it’s own folder. Within those folders, there are tons of .VIB and .VBK files, for however long our retention policy is set for.

For example, BackupJob1 stores backups to E:\Backups\BackupJob1. BackupJob2 stores backups to E:\Backups\BackupJob2.

The problem I’m trying to solve is this: I wanted a way to automatically copy ONLY the full Veeam .VBK files that were taken within the last 7 days to an external harddrive, and then send a Microsoft Teams notification to our IT Team. I call this my “Offline Backup Rotation”.

I usually did this manually, but it required a lot of babysitting and checking in to make sure the files copied successfully.

In this guide, I’ll show you how to create a Powershell script that copies only .VBK files from a folder to your external harddrive.


How It Works

First, purchase as many harddrives as you need. If you want to keep 4 rolling offline copies of backups, purchase 5 harddrives. My backups average 12tb of full VM backup data, so I purchased several 16tb Seagate harddrives for this. Purchase only as much as disk space as you need.

Step 1: Create folders on external drive with the same name as your backup job.

Step 2: Change out paths in the Powershell script below.

Step 3: Set script to run as Scheduled Task.

Like I said, my full VM backups are stored locally on the E:\ drive and are completed on weekends, so I set my scheduled task to run the script on Mondays. That means it’ll copy all of the .VBK files that were generated within the last 7 days; not every .VBK file in the folder.


Powershell Script to Automatically Copy Backups Offline

First, the script will delete any files in the destination folder. The D:\ drive in my script is my external drive.

Then, it will copy any .VBK files created within the last 7 days. If you’d like to copy a different internal, such as nightly, then change .AddDays(-7) to .AddDays(-1).

The last line of the script is optional, but if you want to receive a Microsoft Teams notification to your IT department chat or channel once the script has finished copying over all your backups, you can generate a webhook and paste in the URL.

I then told my IT department to simply swap out the harddrive with the next-in-line one. The beauty of this is that it’s virtually hands-off. I no longer have to manually copy full VM backups once a week. All I have to do is swap harddrives.

Remove-Item -Path D:\BackupJob1\* -verbose -ErrorAction Ignore

get-childitem "E:\BackupJob1\" *.vbk | where-object {$_.LastWriteTime -gt (get-date).AddDays(-7)} | Copy-Item -destination "D:\BackupJob1\" -verbose

#Optional. Enter Teams Webhook URL below and then uncomment the code below
#Invoke-RestMethod -Method post -ContentType 'Application/Json' -Body '{"text":"Weekly offline USB copy complete. Please swap out harddrives @team"}' -Uri https://site.webhook.office.com

How to Generate A Teams Webhook

If you use Teams and want to generate a webhook, this guide will show you exactly how to do that: https://sankalpit.com/plugins/documentation/how-to-get-channel-webhook-url/


Create Scheduled Task

To schedule this to run on a schedule, open Task Scheduler > Create Task. Give it a name and configure the correct operating system version and user to run as.

  • Under Triggers, select your frequency.
  • Under Actions, choose Start A Program.
    • Program Script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
    • Add Arguments: path to file (C:\users\Scripts\FullVMCopyToExternal.ps1)

Wrapping Up

Overall, I think this is a very creative solution to an otherwise manual task. Once the data is on the external drive, you can also take it offsite or store it in a fireproof safe. You can also have this script set to run quarterly for data archiving purposes.

Sure, I could simply create a separate “External Harddrive Backup Job” within Veeam and only take full backups which go to the external hdd, but then I’m basically running another job for X amount of hours, which in my opinion is unnecessary as I already have full VM backups. I don’t need a second job running; I just need to copy the already backed up files somewhere else for safekeeping.


My Homelab Equipment

Here is some of the gear I use in my Homelab. I highly recommend each of them.

The full list of server components I use can be found on my Equipment List page.

0 comment

You may also like

Leave a Comment