Link to home
Start Free TrialLog in
Avatar of Merlin Seby
Merlin Seby

asked on

script for restoring database

I want to write a script for automatically restoring the database from back-upset . can you please help me to provide that in case if someone has written it .

Thanks,
Merlin.s
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Sorry but we'll need more information.

What scripting language?
What database product?
How is the backup being performed?
If you are using Power Shell scripting
import-module sqlps

$database = "NewDb"
$backupLocation = "D:\Backups\CurrentDB.bak"
$dataFileLocation = "D:\Databases\NewDB.mdf"
$logFileLocation = "D:\Logs\NewDB_log.ldf"

$sql = @"

USE [master]

ALTER DATABASE [$database] 
    SET SINGLE_USER WITH ROLLBACK IMMEDIATE

RESTORE DATABASE [$database] 
FROM DISK = N'$backupLocation' 
WITH FILE = 1,  
     MOVE N'CurrentDB' TO N'$dataFileLocation',  
     MOVE N'CurrentDB_log' TO N'$logFileLocation',  
     NOUNLOAD, REPLACE, STATS = 5

ALTER DATABASE [$database] 
    SET MULTI_USER
"@

invoke-sqlcmd $sql

Open in new window


You can find some here
https://www.red-gate.com/simple-talk/sql/backup-and-recovery/backup-and-restore-sql-server-with-the-sql-server-2012-powershell-cmdlets/
>> to backup and restore SQL server database

What in the question leads you to believe it is a SQL Server database?
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.