Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
BITS Transfer loop-over-files example
# AUTHOR: Nate Johnson, @njohnson9402/@natethedba, natethedba.wordpress.com
# LICENSE: https://choosealicense.com/licenses/unlicense/
# TYPE: PowerShell script
# DESCRIPTION/USAGE: example of 2 file copying loops using BITS Transfer cmdlets, targeted at SQL backups
# See corresponding blog post at https://natethedba.wordpress.com/powershell-and-bits/
# ASSUMPTION: your SQL backup files are named with a date component in the form "yyyyMMdd", and use extension ".bak"
$source = "\\BigSQL\Backup\BigSQLInstance\" #network share or local directory
$destiny = "X:\Restore\BigSQLInstance\" #LOCAL drive destination
$testonly = 1 #set to 0 to actually do the copying!
$sleepSeconds = 3 #how many seconds to sleep between each progress-check during each file transfer
# use some date-math to get last Sunday's FULL backups
$datePattern = $(Get-Date).AddDays(-$((Get-Date).DayOfWeek.value__)).ToString("yyyyMMdd") #e.g. "20170910"
$files = Get-ChildItem -Path $source -Recurse -Filter ("*_FULL_" + $datePattern + "*.bak")
Write-Host ("About to copy " + $files.Count + " files to " + $destiny + " ...")
for ($i = 0; $i -lt $files.Count; $i++)
{
$srcfile = $files[$i].FullName
$dstfile = $destiny + "\" + $files[$i].Name
Write-Host (" Copying " + $srcfile)
if (Test-Path $dstfile)
{
Write-Host (" File already exists: " + $dstfile)
}
elseif ($testonly -eq 0)
{
$bitsJob = Start-BitsTransfer -Source $srcFile -Destination $destiny -Asynchronous -Priority Low
while (($bitsJob.JobState.ToString() -eq "Transferring") -or ($bitsJob.JobState.ToString() -eq "Connecting"))
{
#only display progress when transferring
if ($bitsJob.JobState.ToString() -eq "Transferring")
{
$pctCmplt = ($bitsJob.BytesTransferred / $bitsJob.BytesTotal)
#see https://technet.microsoft.com/en-us/library/ee692795.aspx for number formatting trick
Write-host ($bitsJob.JobState.ToString() + " - " + $("{0:P2}" -f $pctCmplt))
}
Sleep $sleepSeconds
}
Complete-BitsTransfer -BitsJob $bitsJob
}
Write-Host (" Finished transferring " + $srcfile + " to " + $destiny)
}
# today's DIFF backups
$datePattern = $(Get-Date).ToString("yyyyMMdd") #e.g. "20170912"
$files = Get-ChildItem -Path $source -Recurse -Filter ("*_DIFF_" + $datePattern + "*.bak")
Write-Host ("About to copy " + $files.Count + " files to " + $destiny + " ...")
for ($i = 0; $i -lt $files.Count; $i++)
{
$srcfile = $files[$i].FullName
$dstfile = $destiny + "\" + $files[$i].Name
Write-Host (" Copying " + $srcfile)
if (Test-Path $dstfile)
{
Write-Host (" File already exists: " + $dstfile)
}
elseif ($testonly -eq 0)
{
$bitsJob = Start-BitsTransfer -Source $srcFile -Destination $destiny -Asynchronous -Priority Low
while (($bitsJob.JobState.ToString() -eq "Transferring") -or ($bitsJob.JobState.ToString() -eq "Connecting"))
{
#only display progress when transferring
if ($bitsJob.JobState.ToString() -eq "Transferring")
{
$pctCmplt = ($bitsJob.BytesTransferred / $bitsJob.BytesTotal)
#see https://technet.microsoft.com/en-us/library/ee692795.aspx for number formatting trick
Write-host ($bitsJob.JobState.ToString() + " - " + $("{0:P2}" -f $pctCmplt))
}
Sleep $sleepSeconds
}
Complete-BitsTransfer -BitsJob $bitsJob
}
Write-Host (" Finished transferring " + $srcfile + " to " + $destiny)
}
Write-Host ("All done!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.