Script to capture logins and user permissions prior to a restore?
Posted on 2014-04-01
I'm trying to figure out a way to automate the creation of logins and user permissions. I'm new to SQL administration and inadvertently keep missing one permission here and there and I'm trying to find a full-proof way not to miss any logins and permissions. Below is the manual process I do simply because I don't know any better. Surely there is an automated way to do this?
I have a DB on our test server. I've been asked to restore the DB from production to test and to keep the existing logins and permissions that were on the test DB before the restore.
1) I log onto the test DB.
2) Run the "sp_helpuser" command to capture the existing logins and permissions. Take a screenshot of the results using Snagit or if too large to display all on the screen I copy all with headers and paste it into an Excel spreadsheet.
3) Select the task to "Generate Scripts" and select the following options.
a) Select specific database objects
b) Select "users"
c) Select "Database Roles"
d) Copy to clipboard
4) Then I store the script to my log file.
5) Backup DB.
1) Restore DB.
2) Run script that re-creates the Users and Database Roles.
3) Run "sp_helpuser" and reset the missing permissions and orphaned users. I miss these occasionally.
a) I type the following command to fix the orphaned user issue.
i) sp_change_users_login 'Auto_Fix', 'User_ID'
Does anyone have a better way of preserving and restoring the logins, users and permissions than what I am doing? I am open to any and all suggestions as I hate making mistakes.