Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 SQL Server Administration (2008)
 Restoring a database alongside existing DB

Author  Topic 

doodleman99
Starting Member

2 Posts

Posted - 2015-04-17 : 09:58:44
Hi there,
I need to recover some data in a table but i'm not 100% sure the right way to do this safely.
I'll need to query the two tables to compare the before and after but how do i go about restoring/attaching the backup database to SQL without causing conflicts?

if i restore, i assume this would just overwrite which is obviously the worst thing that can happen. if i attach the backup, how does this affect the current live DB? how do i make sure that it's not getting accessed and mistaken for the live DB?

the SQL server is 2008 R2 running as a VM.

Many thanks,
JV

Kristen
Test

22859 Posts

Posted - 2015-04-17 : 10:39:18
Assuming you have enough disk space you can RESTORE a database backup file to a new database with a different name. The new, presumably temporary!, database does not need to exist - the RESTORE command will hapily create a brand new database for you.

You can then use 3-part naming to compare the restored table with the current one

SELECT C.NumCol1, O.NumCol1, C.StrCol2, O.StrCol2, ...
FROM MyLiveDatabase.dbo.MyTable as C -- "Current"
JOIN MyRestoredDatabase.dbo.MyTable AS O -- "Old/Original"
ON O.PKey = C.PKey
WHERE (C.NumCol1 <> O.NumCol1
OR (C.NumCol1 IS NULL AND O.NumCol1 IS NOT NULL)
OR (C.NumCol1 IS NOT NULL AND O.NumCol1 IS NULL))
OR (C.StrCol2 <> O.StrCol2 COLLATE Latin1_General_BIN2
OR DATALENGTH(C.StrCol2) <> DATALENGTH(O.StrCol2)

OR (C.StrCol2 IS NULL AND O.StrCol2 IS NOT NULL)
OR (C.StrCol2 IS NOT NULL AND O.StrCol2 IS NULL))
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2015-04-17 : 10:42:49
Although: If you database is backed up direct to tape, using a "SQL Agent" of some sort to directly extract the database from the disk, then it might be complicated restoring it to a differently named database. Let's hope that is NOT the case! This job is much MUCH easier if you backup to a File on the server, and THEN backup the File TO TAPE

If you need to restore the database to a different machine you could then use four part naming

FROM MyLiveServer.MyLiveDatabase.dbo.MyTable as C -- "Current"
JOIN MyOtherServer.MyRestoredDatabase.dbo.MyTable AS O -- "Old/Original"

Go to Top of Page

doodleman99
Starting Member

2 Posts

Posted - 2015-04-17 : 17:08:12
quote:
Originally posted by Kristen

Although: If you database is backed up direct to tape, using a "SQL Agent" of some sort to directly extract the database from the disk, then it might be complicated restoring it to a differently named database. Let's hope that is NOT the case! This job is much MUCH easier if you backup to a File on the server, and THEN backup the File TO TAPE

If you need to restore the database to a different machine you could then use four part naming

FROM MyLiveServer.MyLiveDatabase.dbo.MyTable as C -- "Current"
JOIN MyOtherServer.MyRestoredDatabase.dbo.MyTable AS O -- "Old/Original"





Many thanks for your help. all fixed!
i was actually wondering about the Agent vs MSSQL Backup options?
i do both - i wonder if it's even worth me using the backup agent? think i'm safe with just using my SQL Backup on it's own? are there benefits to using Backup Exec (or similar) agent backups?

Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2015-04-18 : 02:11:52
We get questions on here from people using a Backup Agent who then cannot recover their database. I'm sure there is nothing wrong with the software, but the user upgrades something (maybe an MS SQL Service Pack not compatible with the version of the Backup Agent) or has to restore all-or-nothing or some other trouble.

Backing up to File and then the File to Tape means that you still have the file on disk. if you need to restore "today" then you don't have to resort to tape (and the tape drive might be busy with some other backup / action) as the most recent file is still on-disk. If you want to restore to point-in-time you need to restore Full Backup and then Log Backups (up to that point-in-time). Maybe that is possible with tape Backup Agents, I don't know.

Perhaps you have a corruption and want to restore Full Backup and then Log Backups to the time of corruption / user accidental deletion. You are probably going to have to restore multiple times before you figure out exactly when the corruption / accident occurred. If all the backups are files on disk you can just re-run the Restore multiple times and it won't take long. If on tape you will probably have a far longer job making multiple restores.

Also, so easy with a disk backup file to restore to a new, temporary, database name and run a DBCC CHECKDB to check that the backup file can be restored. No doubt you can do that with tape, but its extra effort and I suspect far fewer organisations using Backup Agents make test restores.
Go to Top of Page
   

- Advertisement -