Friday, 12 June 2020

Copy to latest backup file and rename the backup file and restore on Destination server.


Below script save in .ps1 format and run with Powershell. 

#start Powershell.exe

# Delete all existing backup from below location <D:\ProdBackup\>
Remove-Item D:\ProdBackup\*.bak


# Source backup file location (Server Name : JaiVermaIN0-1 )
$_sourcePath ="\\R00EIEI02\sqlbackups\FULL"



# Location where files will be copied to
$_destinationPath = "D:\ProdBackup";



# Pick latest *.bak file from source and Copying file Destination location and Rename file name

@(Get-ChildItem $_sourcePath -Filter *.bak | Sort LastWriteTime -Descending)[0] | % { Copy-Item -path $_.FullName -destination $("$_destinationPath\MyTest_DB.bak") -force}



#Restore the backup on (Server Name: JaiVermaIN0-2)

Restore-SqlDatabase -ServerInstance "." -Database "testDB" -BackupFile "D:\ProdBackup\MyTest_DB.bak" -ReplaceDatabase

No comments:

Post a Comment