Exporting SQL Server MDF and LDF database files - sql-server

How do I export a SQL Server 2000/2005 database to MDF/LDF files? I do not have access to detach the database nor do I have access to login to the database server to copy the files directly.

Can you run commands against the database from a query window? Do you know of a network path accessible to both you and the SQL Server instance? If so then you can issue the following query (instead of trying to coax some backup through the UI):
BACKUP DATABASE dbname TO DISK = '\\some_network_path_you\have_access_to\db.bak'
WITH COPY_ONLY, INIT;
COPY_ONLY is important so that you don't disrupt the production server's log chain.
Then you can copy that file and run RESTORE DATABASE wherever you like.
You don't want to somehow get access to the MDF / LDF files. For one, in order to copy those, you need to shut down the source SQL Server and detach them. This can lead to many bad things, but most importantly downtime on the production server, and the risk that if something should go wrong, you now have ZERO copies of your database.

Then you request that your dba provide the data to you.

Related

How can I backup and then restore SQL Server DBs to different names without overwriting existing DBs?

I'm working on a small project to backup and restore databases to different name(s), and I can't seem to figure it out.
I'm using SSMS v18.11.1 with SQL Server 2016, and I have seven databases, let's call them DBCompanyA_1 ... DBCompanyA_7. Their .mdf and .ldf files are stored in the default location of
C:\Program Files\Microsoft SQL Server/MSSQL13.MSSQLSERVER\MSSQL\DATA
I want to make a full backup of each of those 7 databases (with logs) and then restore them to a different name. Using my example above, let's say that I want to restore them to DBCompanyB_1 ... DBCompanyB_7.
The problem I keep encountering is that all attempts (using MOVE) overwrites the existing DBCompanyA_n databases, which is bad. I want to use backup and restore so that the DBs originals' permissions will carry over to the new DBs.
I don't care if the backup procedure is via UI or script, but the restore must be using T-SQL.

Attach database with NORECOVERY

Is there a way to attach a SQL Server mdf/ldf files such that the database is in Restoring mode and we can restore log backups on top of it.
I have a hardware array snapshot (crash consistent) that contains the mdf/ldf files. I need to attach these files to another SQL Server instance and then do log restores for a point in time recovery(using stopat)
The CREATE DATABASE .. FOR ATTACH command brings the database online. Log restore cannot be done on an online database.
Is there a way to accomplish this?
No. Unfortunately. Vote for the feature here:
DCR - Attach database with NORECOVERY

How to copy a database from one computer to another?

I have a database in SQL Server 2008, which I want to copy to another computer.
How do I make such a copy?
Once this is done, what should I do at the other computer to build the database once again from my copy?
Using SQL Server Management Studio, here are the steps:
1.Right-click the database and select Tasks | Backup
2.Make sure that the Backup type is Full
3.Click Add and specify the location and backup name
4.Copy the created backup file to another computer
5.In SQL Server Management Studio on another computer, right-click the SQL Server instance and select Restore Database
6.Select Device and click the elipsis button to navigate to the copied backup file
Simple Answer: Back it up, then restore it on the other computer.
Have a look here: http://technet.microsoft.com/en-us/library/cc966495.aspx
There's a lot of stuff there, but essentially, right click on the database, Tasks > Backup. Fill in the options to perform a full backup to somewhere.
Once it has created the backup (one big file, by convention with a BAK extension), on the second computer, right click the Databases folder, Restore Database and follow the prompts.
You can do it as well in SQL if you wish:
Backup: http://msdn.microsoft.com/en-us/library/ms186865.aspx
Restore: http://msdn.microsoft.com/en-us/library/ms186858.aspx
There might be times when it's better to detach and move, but this approach always feels a bit safer!
This will copy both structure and the data in the database.
Use TSQL backup and restore. This should help http://www.sqlmag.com/article/tsql3/use-t-sql-to-back-up-and-restore-sql-server-user-databases
One of the easiest ways to do this is back up and restore.
http://msdn.microsoft.com/en-us/library/ms187048.aspx
Another option you have is detaching your database from SQL Server, copying the MDF and LDF files to the target machine, and re-attaching there. Note, if you want to keep a copy of the database on the original server, you will also need to attach there too.
http://msdn.microsoft.com/en-us/library/ms190794.aspx
You can dump the content of your database and restore it on another machine. The same mechanism can be used also for backup purposes. Check the backup&restore functionality of MS SQL.

Opening a SQL Server .bak file (Not restoring!)

I have been reading a LOT of google posts and StackOverflow questions about how to restore a database in SQL Server from a .bak file.
But none of them states how to just READ the tables in the database-backup.
(None that I could find anyway?)
I just want to check out some old information which now has been deleted, without actually restoring the full database.
Is this possible?
.
EDIT:
I just wanted to post my T-SQL solution to the problem, so others may use it and I can go back and look it up ;)
First I created a new database called backup_lookup and took it offline.
After this I could restore my old database mydb to the new one, without ever touching my original.
USE master
GO
RESTORE DATABASE backup_lookup
FROM DISK = 'D:\backup\mydb.bak'
WITH REPLACE,
MOVE 'mydb' TO 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\backup_lookup.mdf',
MOVE 'mydb_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\backup_lookup_log.ldf'
GO
I hope this helps :)
From SQL Server 2008 SSMS (SQL Server Management Studio), simply:
Connect to your database instance (for example, "localhost\sqlexpress")
Either:
a) Select the database you want to restore to; or, alternatively
b) Just create a new, empty database to restore to.
Right-click, Tasks, Restore, Database
Device, [...], Add, Browse to your .bak file
Select the backup.
Choose "overwrite=Y" under options.
Restore the database
It should say "100% complete", and your database should be on-line.
PS: Again, I emphasize: you can easily do this on a "scratch database" - you do not need to overwrite your current database. But you do need to RESTORE.
PPS: You can also accomplish the same thing with T-SQL commands, if you wished to script it.
The only workable solution is to restore the .bak file. The contents and the structure of those files are not documented and therefore, there's really no way (other than an awful hack) to get this to work - definitely not worth your time and the effort!
The only tool I'm aware of that can make sense of .bak files without restoring them is Red-Gate SQL Compare Professional (and the accompanying SQL Data Compare) which allow you to compare your database structure against the contents of a .bak file. Red-Gate tools are absolutely marvelous - highly recommended and well worth every penny they cost!
And I just checked their web site - it does seem that you can indeed restore a single table from out of a .bak file with SQL Compare Pro ! :-)
There is no standard way to do this. You need to use 3rd party tools such as ApexSQL Restore or SQL Virtual Restore. These tools don’t really read the backup file directly. They get SQL Server to “think” of backup files as if these were live databases.
Just to add my TSQL-scripted solution:
First of all; add a new database named backup_lookup.
Then just run this script, inserting your own databases' root path and backup filepath
USE [master]
GO
RESTORE DATABASE backup_lookup
FROM DISK = 'C:\backup.bak'
WITH REPLACE,
MOVE 'Old Database Name' TO 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\backup_lookup.mdf',
MOVE 'Old Database Name_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\backup_lookup_log.ldf'
GO
It doesn't seem possible with SQL Server 2008 alone. You're going to need a third-party tool's help.
It will help you make your .bak act like a live database:
http://www.red-gate.com/products/dba/sql-virtual-restore/

Backup remote SQL Server database to local

I'm trying to backup a live database to my computer and I can't find the option to do it. I'm connecting to it using Microsoft SQL Server Management Studio 2008 R2. I'm a MySQL monkey, so I'm used to being able to backup to .sql files and move them around.
Anyone have any idea how I can create a file backup of the database? I've found the backup option which only backs up on the server, or the export, which seems to only allow a single table, or code an SQL query, which I'm not too sure on, short of putting in something like SHOW TABLES;
Anyone have any ideas? I'm limited to readonly access for various reasons, nothing bad, promise!
You will only be able to backup the database to a location the service account for SQL has access to. If you have access to a central share on the server/network that you can access and the service can, you might backup to that location and then browse from your computer to pull it down.
If you are just wanting the database structure you could script the database out to a file. This would let you save it locally. If you also want the data though doing a full backup is the quickest way I know of.
EDIT
I would use the T-SQL BACKUP comand and include WITH COPY_ONLY to backup the database, since you stated this is a "live" database. If a scheduled job is performing backups against the database and you break in to do an additional one you will effect the backup recovery chain of the database. Using the COPY_ONLY will allow you to get a backup of the database without requiring it in the event of a recovery need.
You can also create sql dumps with Management Studio.
Right-click the database and select Tasks - Generate Scripts. This will open a wizard that allows you to select what the dump should include (e.g. tables, indices, views, ...).
Make sure you set "Script Data" to true if you want your dump to include inserts.
You can enter a valid UNC path in the Backup option.

Resources