restore_sql_server_database
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| restore_sql_server_database [2018/12/08 12:49] – created 0.0.0.0 | restore_sql_server_database [2019/01/30 11:32] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Restore_SQL_Server_database ====== | + | |
| - | + | * Check the current security settings on the ONT database, use this script | |
| - | | + | * Restore over the existing database. Use the GUI interface to setup the restore, check the 'with replace' |
| - | * Check the current security settings on the ONT database, use this script | + | * There alter the script to get the following result |
| - | * Restore over the existing database. Use the GUI interface to setup the restore, check the 'with replace' | + | < |
| - | * There alter the script to get the following result | + | |
| ALTER DATABASE [[DBToRestore]] SET Single_user with rollback immediate | ALTER DATABASE [[DBToRestore]] SET Single_user with rollback immediate | ||
| GO | GO | ||
| RESTORE DATABASE ... | RESTORE DATABASE ... | ||
| - | | + | </ |
| - | * When finished, recreate the premission with the above generated script | + | |
| - | * Eventually clean up the MON accounts ... | + | * When finished, recreate the premission with the above generated script |
| - | * If any SQL accounts are restored, fix them with the following statement | + | * Eventually clean up the MON accounts ... |
| + | * If any SQL accounts are restored, fix them with the following statement | ||
| + | < | ||
| exec sp_change_users_login ' | exec sp_change_users_login ' | ||
| - | + | </ | |
| - | + | ||
| [[Category: | [[Category: | ||
| [[Category: | [[Category: | ||
| - | + | ||
| - | + | ||
| < | < | ||
| set nocount on | set nocount on | ||
| Line 24: | Line 26: | ||
| declare @as_ObjectName sysname | declare @as_ObjectName sysname | ||
| set @as_ObjectName = NULL | set @as_ObjectName = NULL | ||
| - | + | ||
| --database owner info | --database owner info | ||
| - | select 'alter authorization on database:: | + | select 'alter authorization on database::[[' |
| AS ' | AS ' | ||
| from master.sys.databases where name = db_name() | from master.sys.databases where name = db_name() | ||
| - | + | ||
| - | + | ||
| --drop and recreate users | --drop and recreate users | ||
| - | + | ||
| select '-- It is not always necessary to drop and recreate the users it will depend on the circumstances under which you need to run this script' | select '-- It is not always necessary to drop and recreate the users it will depend on the circumstances under which you need to run this script' | ||
| - | + | ||
| - | select 'drop user [' + name + ' | + | select 'drop user [[' + name + ']]' from sys.database_principals |
| where principal_id > 4 and owning_principal_id is NULL | where principal_id > 4 and owning_principal_id is NULL | ||
| and type != ' | and type != ' | ||
| order by name | order by name | ||
| - | + | ||
| - | select ' | + | select ' |
| case dp.type | case dp.type | ||
| when ' | when ' | ||
| else | else | ||
| - | ' WITH DEFAULT_SCHEMA=[' | + | ' WITH DEFAULT_SCHEMA=[[' |
| end | end | ||
| as '-- by default Orphaned users will not be recreated' | as '-- by default Orphaned users will not be recreated' | ||
| from sys.server_principals sp | from sys.server_principals sp | ||
| inner join sys.database_principals dp on dp.sid = sp.sid | inner join sys.database_principals dp on dp.sid = sp.sid | ||
| - | where dp.principal_id > 4 and dp.owning_principal_id is NULL and sp.name <> | + | where dp.principal_id > 4 and dp.owning_principal_id is NULL and sp.name <> |
| order by dp.name | order by dp.name | ||
| - | + | ||
| -- Recreate the User defined roles | -- Recreate the User defined roles | ||
| select '-- server created roles should be added by the correct processes' | select '-- server created roles should be added by the correct processes' | ||
| - | + | ||
| - | select ' | + | select ' |
| from sys.database_principals | from sys.database_principals | ||
| where name != ' | where name != ' | ||
| - | + | ||
| -- recreate application roles | -- recreate application roles | ||
| - | + | ||
| - | select ' | + | select ' |
| from sys.database_principals | from sys.database_principals | ||
| where type = ' | where type = ' | ||
| - | + | ||
| - | + | ||
| -- ADD ROLE MEMBERS | -- ADD ROLE MEMBERS | ||
| - | + | ||
| - | SELECT 'EXEC sp_addrolemember [' + dp.name + '], [' + USER_NAME(drm.member_principal_id) + '] ' AS [-- AddRolemembers] | + | SELECT 'EXEC sp_addrolemember |
| FROM sys.database_role_members drm | FROM sys.database_role_members drm | ||
| INNER JOIN sys.database_principals dp ON dp.principal_id = drm.role_principal_id | INNER JOIN sys.database_principals dp ON dp.principal_id = drm.role_principal_id | ||
| where USER_NAME(drm.member_principal_id) != ' | where USER_NAME(drm.member_principal_id) != ' | ||
| order by drm.role_principal_id | order by drm.role_principal_id | ||
| - | + | ||
| - | + | ||
| -- CREATE GRANT Object PERMISSIONS SCRIPT | -- CREATE GRANT Object PERMISSIONS SCRIPT | ||
| - | + | ||
| - | SELECT replace(state_desc,' | + | SELECT replace(state_desc,' |
| - | + OBJECT_SCHEMA_NAME(major_id) + ' | + | + OBJECT_SCHEMA_NAME(major_id) + ']].[[' + OBJECT_NAME(major_id) + ']]'+ |
| case minor_id | case minor_id | ||
| when 0 then ' ' | when 0 then ' ' | ||
| else | else | ||
| - | ' ([' | + | ' ([[' |
| end | end | ||
| - | +' TO [' + USER_NAME(grantee_principal_id)+' | + | +' TO [[' + USER_NAME(grantee_principal_id)+' |
| case | case | ||
| when state_desc like ' | when state_desc like ' | ||
| Line 95: | Line 97: | ||
| --AND OBJECT_SCHEMA_NAME(major_id) != ' | --AND OBJECT_SCHEMA_NAME(major_id) != ' | ||
| ORDER BY USER_NAME(grantee_principal_id), | ORDER BY USER_NAME(grantee_principal_id), | ||
| - | + | ||
| --SCHEMA permissions | --SCHEMA permissions | ||
| - | SELECT replace(state_desc,' | + | SELECT replace(state_desc,' |
| - | + SCHEMA_NAME(major_id) + ' | + | + SCHEMA_NAME(major_id) + ']]'+ |
| - | +' TO [' + USER_NAME(grantee_principal_id)+' | + | +' TO [[' + USER_NAME(grantee_principal_id)+' |
| case | case | ||
| when state_desc like ' | when state_desc like ' | ||
| Line 109: | Line 111: | ||
| WHERE class_desc = ' | WHERE class_desc = ' | ||
| ORDER BY USER_NAME(grantee_principal_id), | ORDER BY USER_NAME(grantee_principal_id), | ||
| - | + | ||
| - | SELECT replace(state_desc,' | + | SELECT replace(state_desc,' |
| - | ' TO [' + USER_NAME(grantee_principal_id)+' | + | ' TO [[' + USER_NAME(grantee_principal_id)+' |
| case | case | ||
| when state_desc like ' | when state_desc like ' | ||
| Line 122: | Line 124: | ||
| </ | </ | ||
| [[Category: | [[Category: | ||
| + | |||
restore_sql_server_database.1544273361.txt.gz · Last modified: 2018/12/08 12:49 by 0.0.0.0
