Move Ola Hallengren SQL Server backups

The Ola Hallengren backup solution is good.  If you need to redo security on the folders, and carefully move files to a new folder, here is a Powershell script to assist. Set your paths at the top, and it will go through each database, and each FULL, DIFF, and LOG directory to move the files.

cls;
$OldRootPath = "\\FileServer\SQLBackups\ProductName_old\ServerName\";
$NewRootPath = "\\FileServer\SQLBackups\ProductName\ServerName\";
$Folders = Get-ChildItem -Path $OldRootPath | ?{ $_.PSIsContainer };

foreach($Folder in $Folders)
{
 $DatabaseName = $Folder.Name;
 $OldDatabasePath = $OldRootPath + $DatabaseName;
 $NewDatabasePath = $NewRootPath + $DatabaseName;
 
 $DatabaseName;
 $FullOldPath = $OldDatabasePath + "\FULL\";
 $FullNewPath = $NewDatabasePath + "\FULL";
 $FullBAKPath = $FullOldPath + "*.BAK";
 
 $DiffOldPath = $OldDatabasePath + "\DIFF\";
 $DiffNewPath = $NewDatabasePath + "\DIFF\";
 $DiffBAKPath = $DiffOldPath + "*.BAK";
 
 $LogOldPath = $OldDatabasePath + "\LOG\";
 $LogNewPath = $NewDatabasePath+ "\LOG\";
 $LogTRNPath = $LogOldPath + "*.TRN";
 
 Move-Item $FullBAKPath $FullNewPath
 "FULL files remaining:"
 ls $FullOldPath
 if(Test-Path $DiffOldPath)
 {
 Move-Item $DiffBAKPath $DiffNewPath
 "DIFF files remaining:"
 ls $DiffOldPath
 }
 else
 {
 "No diff path"
 }
 if(Test-Path $LogOldPath)
 {
 Move-Item $LogTRNPath $LogNewPath
 #Remove-Item $LogTRNPath
 "LOG files remaining:"
 ls $LogOldPath
 }
 else
 {
 "No log path"
 }
}
Advertisements
This entry was posted in Powershell. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s