Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy SQL Server 2008 R2 database from one machine to another

Tags:

I have a database in SQL Server 2008 R2, and I want to copy this database onto another machine.

How do I make a copy?

And how do I restore it?

Thanks

like image 255
DK007 Avatar asked Sep 18 '13 07:09

DK007


People also ask

How do I copy a database from one server to another locally?

Manual Method to Copy Database from one Server to Another First of all, launch the SQL Server Management Studio from Object Explorer and connect to the Source Server. Right-click on the database, select the option Tasks and then choose the Copy Database option.


1 Answers

You can't copy Database to another machine. Yes you can take back up to same machine and copy it to another machine and do restore.

To take backup follow procedure:

  1. Right Click on the database you want to take backup.
  2. Choose Task -> Back Up.
  3. In Destination, Choose Add.
  4. In File Name click on ... button and choose destination folder where you want to backup with backupname.bak . Click Ok, Ok and Ok. and wait until backup process is completed. Click Ok.

Now copy that backup file into pendrive or any media and paste it to another machine and Open SQL Server 2008 R2

To restore backup follow procedure:

  1. Right Click on the Databases.
  2. Choose Restore Database.
  3. Write database name which you want to restore in To Database field
  4. Select From device radio button in Source for restore. Click on ...
  5. Click on Add button, Select database backup file you have pasted. Click Ok, Ok.
  6. Check the checkbox of Restore in Select the beckup sets to restore.
  7. Go on Options Check Overwrite the existing database & Preserve the replication settings (this fields needed to check only when you try to restore database which is already resided on that another device)
  8. Click Ok. wait until restore complete and click ok.

Tell me if you face any problem.


By Code

To Backup:

USE DATABASE_NAME; GO BACKUP DATABASE DATABASE_NAME TO DISK = 'D:\DATABASE_NAME.Bak'    WITH FORMAT, MEDIANAME = 'D_SQLServerBackups',    NAME = 'Full Backup of DATABASE_NAME'; GO 

(If you want to put backup in any folder, the folder must be exist before you take the backup.)

To Restore:

Step 1: Retrive the Logical file name of the database from backup.

RESTORE FILELISTONLY FROM DISK = 'D:BackUpYourBaackUpFile.bak' GO 

Step 2: Use the values in the LogicalName Column in following Step. ----Make Database to single user Mode

ALTER DATABASE YourDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE 

----Restore Database

RESTORE DATABASE YourDB FROM DISK = 'D:BackUpYourBaackUpFile.bak' WITH MOVE 'YourMDFLogicalName' TO 'D:DataYourMDFFile.mdf', MOVE 'YourLDFLogicalName' TO 'D:DataYourLDFFile.ldf' 

/If there is no error in statement before database will be in multiuser mode. If error occurs please execute following command it will convert database in multi user./

ALTER DATABASE YourDB SET MULTI_USER GO 
like image 87
Dhwani Avatar answered Oct 18 '22 08:10

Dhwani