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.
Related
I want to backup some specific data of SQL database by using vb.net code. In fact i want to save this file as .BAK file and then i want to restore it.
I couldn't find anything. all the solutions in the web was about full backup of database and there was not any method to backup some tables via code.
SQL Server does not permit backing up a single table. Full stop. No provision is made for this whatsoever.
It does permit restoring a single table from a complete backup, but this does not seem to be what you want.
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.
I need to copy the contents (tables, views, procs, DATA, etc.) of a SQL Server database and copy it to another SQL Server database. I need to do this entirely in a script as I am not able to carry over files to the destination web server. Using Microsoft SQL Server Management Studio, how can I accomplish this?
Note: The database I am copying to will have the same name and be completely empty.
You can script the database schema and data using SSMS
Right click on the database. Choose tasks....then choose generate sql scripts.
Specify all objects. Then in the options menu choose at the bottom to generate scripts for data.
Below are some links that will be helpful
http://blog.sqlauthority.com/2007/08/21/sql-server-2005-create-script-to-copy-database-schema-and-all-the-objects-stored-procedure-functions-triggers-tables-views-constraints-and-all-other-database-objects/
http://blog.sqlauthority.com/2007/11/16/sql-server-2005-generate-script-with-data-from-database-database-publishing-wizard/
You can backup the database and restore to any instance you'd like. Since I don't like posting the "how to" when I learned it someplace else originally - here's the MSDN describing the process.
http://msdn.microsoft.com/en-us/library/ms187048.aspx
It is how we do things in our production environment. We back up and restore to our other instances.
The easiest way would be to restore from a backup, but since you can't copy files to the file system, the Copy database wizard will do this.
http://msdn.microsoft.com/en-us/library/ms188664.aspx
Dumb question - what's the best way to copy instances in an environment where I want to refresh a development server with instances from a production server?
I've done backup-restore, but I've heard detach-copy-attach and one guy even told me he would just copy the datafiles between the filesystems....
Are these the three (or two, the last one sounds kind of suspect) accepted methods?
My understanding is that the second method is faster but requires downtime on the source because of the detach aspect.
Also, in this situation (wanting an exact copy of production on a dev server) what's the accepted practice for transferring logins,etc.? Should I just backup and restore the user databases + master + msdb?
Easiest way is actually a script.
Run this on production:
USE MASTER;
BACKUP DATABASE [MyDatabase]
TO DISK = 'C:\temp\MyDatabase1.bak' -- some writeable folder.
WITH COPY_ONLY
This one command makes a complete backup copy of the database onto a single file, without interfering with production availability or backup schedule, etc.
To restore, just run this on your dev or test SQL Server:
USE MASTER;
RESTORE DATABASE [MyDatabase]
FROM DISK = 'C:\temp\MyDatabase1.bak'
WITH
MOVE 'MyDatabase' TO 'C:\Sql\MyDatabase.mdf', -- or wherever these live on target
MOVE 'MyDatabase_log' TO 'C:\Sql\MyDatabase_log.ldf',
REPLACE, RECOVERY
Then save these scripts on each server. One-click convenience.
Edit:
if you get an error when restoring that the logical names don't match, you can get them like this:
RESTORE FILELISTONLY
FROM disk = 'C:\temp\MyDatabaseName1.bak'
If you use SQL Server logins (not windows authentication) you can run this after restoring each time (on the dev/test machine):
use MyDatabaseName;
sp_change_users_login 'Auto_Fix', 'userloginname', null, 'userpassword';
The fastest way to copy a database is to detach-copy-attach method, but the production users will not have database access while the prod db is detached. You can do something like this if your production DB is for example a Point of Sale system that nobody uses during the night.
If you cannot detach the production db you should use backup and restore.
You will have to create the logins if they are not in the new instance. I do not recommend you to copy the system databases.
You can use the SQL Server Management Studio to create the scripts that create the logins you need. Right click on the login you need to create and select Script Login As / Create.
This will lists the orphaned users:
EXEC sp_change_users_login 'Report'
If you already have a login id and password for this user, fix it by doing:
EXEC sp_change_users_login 'Auto_Fix', 'user'
If you want to create a new login id and password for this user, fix it by doing:
EXEC sp_change_users_login 'Auto_Fix', 'user', 'login', 'password'
UPDATE:
My advice below tells you how to script a DB using SQL Server Management Studio, but the default settings in SSMS miss out all sorts of crucial parts of a database (like indexes and triggers!) for some reason. So, I created my own program to properly script a database including just about every type of DB object you may have added. I recommend using this instead. It's called SQL Server Scripter and it can be found here:
https://bitbucket.org/jez9999/sqlserverscripter
I'm surprised no-one has mentioned this, because it's really useful: you can dump out a database (its schema and data) to a script, using SQL Server Management Studio.
Right-click the database, choose "Tasks | Generate Scripts...", and then select to script specific database objects. Select the ones you want to copy over to the new DB (you probably want to select at least the Tables and Schemas). Then, for the "Set Scripting Options" screen, click "Advanced", scroll down to "Types of data to script" and select "Schema and data". Click OK, and finish generating the script. You'll see that this has now generated a long script for you that creates the database's tables and inserts the data into them! You can then create a new database, and change the USE [DbName] statement at the top of the script to reflect the name of the new database you want to copy the old one to. Run the script and the old database's schema and data will be copied to the new one!
This allows you to do the whole thing from within SQL Server Management studio, and there's no need to touch the file system.
Below is what I do to copy a database from production env to my local env:
Create an empty database in your local sql server
Right click on the new database -> tasks -> import data
In the SQL Server Import and Export Wizard, select product env's servername as data source. And select your new database as the destination data.
Its hard to detach your production dB or other running dB's and deal with that downtime, so I almost always use a Backup / restore method.
If you also want to make sure to keep your login's in sync check out the MS KB article on using the stored proc sp_help_revlogin to do this.
The detach/copy/attach method will take down the database. That's not something you'd want in production.
The backup/restore will only work if you have write permissions to the production server. I work with Amazon RDS and I don't.
The import/export method doesn't really work because of foreign keys - unless you do tables one by one in the order they reference one another. You can do an import/export to a new database. That will copy all the tables and data, but not the foreign keys.
This sounds like a common operation one needs to do with database. Why isn't SQL Server handling this properly? Every time I had to do this it was frustrating.
That being said, the only painless solution I've encountered was Sql Azure Migration Tool which is maintained by the community. It works with SQL Server too.
I run an SP to DROP the table(s) and then use a DTS package to import the most recent production table(s) onto my development box.
Then I go home and come back the following morning. It's not elegant; but it works for me.
If you want to take a copy of a live database, do the Backup/Restore method.
[In SQLS2000, not sure about 2008:] Just keep in mind that if you are using SQL Server accounts in this database, as opposed to Windows accounts, if the master DB is different or out of sync on the development server, the user accounts will not translate when you do the restore. I've heard about an SP to remap them, but I can't remember which one it was.
I need to copy about 40 databases from one server to another. The new databases should have new names, but all the same tables, data and indexes as the original databases. So far I've been:
1) creating each destination database
2) using the "Tasks->Export Data" command to create and populate tables for each database individually
3) rebuilding all of the indexes for each database with a SQL script
Only three steps per database, but I'll bet there's an easier way. Do any MS SQL Server experts out there have any advice?
Given that you're performing this on multiple databases -- you want a simple scripted solution, not a point and click solution.
This is a backup script that i keep around.
Get it working for one file and then modify it for many.
(on source server...)
BACKUP DATABASE Northwind
TO DISK = 'c:\Northwind.bak'
(target server...)
RESTORE FILELISTONLY
FROM DISK = 'c:\Northwind.bak'
(look at the device names... and determine where you want the mdf and
ldf files to go on this target server)
RESTORE DATABASE TestDB
FROM DISK = 'c:\Northwind.bak'
WITH MOVE 'Northwind' TO 'c:\test\testdb.mdf',
MOVE 'Northwind_log' TO 'c:\test\testdb.ldf'
GO
Maybe the easiest is to detach/reattach. Right-click in the server manager on the DB, tasks --> detach. Then copy the MDF/LDF files to the new server and then reattach by clicking on the server icon and tasks-->attach. It will ask you for the MDF file - make sure the name etc is accurate.
In order of ease
stop server/fcopy/attach is probably easiest.
backup/restore - can be done disconnected pretty simple and easy
transfer DTS task - needs file copy permissions
replication - furthest from simple to setup
Things to think about permissions, users and groups at the destination server esp. if you're transferring or restoring.
There are better answers already but this is an 'also ran' because it is just another option.
For the low low price of free you could look at the Microsoft SQL Server Database Publishing Wizard. This tool allows you to script the schema, data or data and schema. Plus is can be run from a UI or command line <- think CI process.
Backup -> Restore is the simplest, if not to use the replication.
If you use the Backup/Restore solution you're likely to have orphaned users so be sure to check out this article<microsoft> on how to fix them.
Another one to check out that is quick and simple:
Simple SQL BULK Copy
http://projects.c3o.com/files/3/plugins/entry11.aspx
Backup the databases using the standard SQL backup tool in Enterprise Manager, then when you restore on the second server you can specify the name of the new database.
This is the best way to maintain the schema in its entirety.
use backups to restore the databases to the new server with the new names.
Redgate SQL Compare and SQL Data Compare. The Comparison Bundle was by far the best investment a company I worked for ever made. Moving e-training content was a breeze with it.
Check those links:
For multiple db's backup
and single db restore