Restore and Backup MSDE Database with TSQL via OSQL

Some instructions on how to restore a database .bak via an interactive OSQL session for MS SQL Server.

Copied, trimmed, and edited from: http://www.experts-exchange.com/Databases/Q_21503049.html

Login to OSQL:

osql -U sa -p -S localhost



1. Get list of file names as embedded in the SQL Server .bak file:

RESTORE FILELISTONLY FROM DISK = 'C:\MSSQL\Data\mydatabasebackup.bak'

2. Restore.

If you are restoring a database back to it's source, then you just need this:

RESTORE DATABASE mydatabase
FROM DISK = 'C:\MSSQL\Data\mydatabasebackup.bak'


If, instead, the database has moved, e.g. copied from a different computer, use the values from step 1's query output to restore files to a different location:

Pay attention to the logical file names:

RESTORE DATABASE mydatabase
FROM DISK = 'C:\MSSQL\Data\mydatabasebackup.bak'
WITH REPLACE
, MOVE 'mydatabase_data' to 'C:\MSSQL\Data\mydatabase.mdf'
, MOVE 'mydatabase_log' to 'C:\MSSQL\Data\mydatabase.ldf'

Backup Syntax:

OSQL -Usa -PmyPasword -n -Q "BACKUP DATABASE msdb TO DISK = 'c:\msdb.dat_bak'"


Tags: restore, backup, .bak, osql
Related Scribbles:
  • Attach / Detach Database OSQL Example
  • List Databases MSSQL
  • SQL Server


  • ID: 730
    Author:
    leonard
    Date Updated:
    2011-04-29 12:41:24
    Date Created:
    2006-01-03 16:43:18

    Edit

    Comments?
     >> Leonard Chan's Homepage  >> Scribble Web  >> Restore and Backup MSDE Database with TSQL via OSQL
    leonard.lotus-land.ca is hosted by Perceptus Solutions Inc.