We help IT Professionals succeed at work.

Copy SQL database (and ASP.NET user database) from live into test system

574 Views
Last Modified: 2012-05-10
Hi Experts,

on my web server I have SQL databases containing my data (named Live) and the ASP.net users (named Live_aspnetdb). These I would like to copy into my test database (named Test and Test_aspnetdb), which reside on the same server.

So far I have restored database backups from Live and Live_aspnetdb to Test and Test_aspnetdb, but after that had to fix access rights manually, because the SQL users that can access the Live db may not access the Test db and vice versa. Having to do this for every such Copy process is pretty time consuming...

What is the best way to quickly copy the contents of the Live dbs into to Test dbs, without having to manually adjust access rights?

Thanks a lot!

Thomas
Comment
Watch Question

1. You can crate a one time script and can run it always when you move live database to test.

2. Other options is to create a linked server and add production server as linked server and then you can write Insert/update query (one time effort), using this way you dont need to move database files physically.

below is simple example for MySQL to MS-SQL, you can consider it as MS-SQL production to MS-SQL Test

http://www.codeproject.com/KB/database/migrate-mysql-to-mssql.aspx 

Author

Commented:
Thanks for the quick reply. I don't really understand how your 1) would help me - it's obvious I have to create some kind of script, but which - unless you mean that the key statement of 2) is the core of that script (SELECT * INTO testMySQL.dbo.shoutbox FROM openquery(MYSQL, 'SELECT * FROM tigerdb.shoutbox'). The problem with that SELECT is (as far as I understand) that it doesn't remove any old data from my test database first. This, on the other hand, could be tricky because of relationships. Also, it would quite likely not carry over all autoincrement IDs untouched, which would damage the consistency of the database.

Or am I missing a point?
It will be best if you Truncate Data from each destination table when you are moving data from one to other.

Author

Commented:
Well, I can't Truncate the ASP.net user databases, as there are foreign key relationships in the data structure. Using Delete doesn't reset a autoincrement IDs.

Isn't there a way to copy the databases on a database or table level, rather than per record (as in a Select * Into statement)?
CERTIFIED EXPERT
Top Expert 2012

Commented:
Consider purchasing a copy of Red-Gate's SQL Data Compare to do this:
SQL Data Compareā„¢
http://www.red-gate.com/products/SQL_Data_Compare/index.htm
EugeneZ ZhitomirskySQL SERVER EXPERT
CERTIFIED EXPERT

Commented:
you probably had "orphaned users" problem
see
Resolve Orphaned Users
 http://support.microsoft.com/kb/274188/
 http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=12615
more
How to move databases between computers that are running SQL Server
http://support.microsoft.com/kb/314546 
CERTIFIED EXPERT
Top Expert 2012

Commented:
>>acperkins' suggestions costs several hundered dollars<<
Then I can only assume you do not value your time or your data very much ...

Next time, please do not abandon the question for over a month, just because you did not like the responses.
EugeneZ ZhitomirskySQL SERVER EXPERT
CERTIFIED EXPERT

Commented:
Staudte:
You have never posted what is your sql server version and SP
you need to make sure you have a fresh SP:
If you restore on the same server db - you should have no problem - and no need to fix db user.

Author

Commented:
Dear All,

sorry for having been unresponsive. Let me clarify my reasons: This is a non-profit project into which I have invested hundreds of hours and where only extremely limited budget is available. Technically, I have (simplified):

DB_Live and DB_live_aspnetdb with user DB_Live_User
DB_Test and DB_test_aspnetdb with user DB_Test_User
DB_Dev and DB_dev_aspnetdb with user DB_Dev_User

Occasionally (every couple of weeks) I back up DB_Live and restore to DB_Dev and DB_Test using a SQL Backup Tool (dbsave). After that, the DB_Test_User and DB_Dev_User have lost rights to access their databases, while DB_Live_User can access all three. I have to manually remove DB_Live_User's rights from the two copies and reassing the other two users to their corresponding db's. There's nothing really incorrect or defective with the way the restore is handled, there are no orphaned users, just incorrect access rights.

I just want to copy the _data_, not access rights, from one DB to another. Also, I do value my time, but I don't throw money out for a tool that does something the already available environment should be very qualified to to. And  @EugeneZ, I just assumed that if I was posting in MS SQL 2008 zones and reference asp.net, it would be clear that I'm dealing with MS SQL 2008 - sorry for overlooking the general rule to clarify the environment. I'm using Windows 2008, MS SQL 2008 Express 10.0.2531.

Reconsidering the scenario, I fell that I need to look into SQL commands that do the following:

Back-Up a database into a file.
Restore the file into another database on the same server
(if still needed) Remove access rights from one user and add rights to another.

I'll put these commands into a SQL procedure file and invoke it manually using Management Studio as required.

Thanks again for taking your time looking into this.
CERTIFIED EXPERT
Top Expert 2012
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
EugeneZ ZhitomirskySQL SERVER EXPERT
CERTIFIED EXPERT

Commented:
sometimes people post Oracle questions in  MS SQL Server, SQL Server 2008 Zones ...

Author

Commented:
Please excuse my ignorance, but I don't get it to work.

I have restored the Live db into my Dev db and, as a result, the live user has access rights to liveDb (as before) and to devDb (expected, but not intended).

In SQL Server Management Studio I select the devDb and execute

USE [Jufo-Dev]
EXEC sp_change_users_login 'Report'
GO

That gives me a one-line-table with "Jufo" and a UserSID ("Jufo" is not a current username, they are called "Jufo-Live", "Jufo-Dev" and "Jufo-Test").

Doing a

USE [Jufo-Dev]
EXEC sp_change_users_login 'Auto_Fix', 'jufo-dev'
GO

returns a message, that 0 orphaned users were repaired.

The access rights have not changed after that procedure.

Author

Commented:
This question is progressing well now, attempting to delete it was my mistake. Please ignore this request and leave it open.
EugeneZ ZhitomirskySQL SERVER EXPERT
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.