Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4600580
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:42:15+00:00 2026-05-21T23:42:15+00:00

We have implemented log shipping as a database disaster recovery solution and want to

  • 0

We have implemented log shipping as a database disaster recovery solution and want to know if there is a way I can use T-SQL to script all the logins, users, roles permissions etc to the master database on the secondary server so that the T-SQL can be sheduled to run as an SQL Job?

My aim is that in the event of a D/R situation we can simply restore the transaction logs for each database to the secondary server and not have to worry about orphaned users etc.

Thanks for you help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T23:42:16+00:00Added an answer on May 21, 2026 at 11:42 pm

    There is a login copy script here designed to copy logins to another server for disaster recovery purposes:

    http://www.sqlsoldier.com/wp/sqlserver/transferring-logins-to-a-database-mirror

    Use master;
    Go
    
    If Exists (Select 1 From INFORMATION_SCHEMA.ROUTINES
                Where ROUTINE_NAME = 'dba_CopyLogins'
                And ROUTINE_SCHEMA = 'dbo')
        Drop Procedure dbo.dba_CopyLogins
    Go
    
    SET ANSI_NULLS ON
    SET QUOTED_IDENTIFIER ON
    GO
    
    Create Procedure dbo.dba_CopyLogins
        @PartnerServer sysname,
        @Debug bit = 0
    As
    
    Declare @MaxID int,
        @CurrID int,
        @SQL nvarchar(max),
        @LoginName sysname,
        @IsDisabled int,
        @Type char(1),
        @SID varbinary(85),
        @SIDString nvarchar(100),
        @PasswordHash varbinary(256),
        @PasswordHashString nvarchar(300),
        @RoleName sysname,
        @Machine sysname,
        @PermState nvarchar(60),
        @PermName sysname,
        @Class tinyint,
        @MajorID int,
        @ErrNumber int,
        @ErrSeverity int,
        @ErrState int,
        @ErrProcedure sysname,
        @ErrLine int,
        @ErrMsg nvarchar(2048)
    Declare @Logins Table (LoginID int identity(1, 1) not null primary key,
                            [Name] sysname not null,
                            [SID] varbinary(85) not null,
                            IsDisabled int not null,
                            [Type] char(1) not null,
                            PasswordHash varbinary(256) null)
    Declare @Roles Table (RoleID int identity(1, 1) not null primary key,
                        RoleName sysname not null,
                        LoginName sysname not null)
    Declare @Perms Table (PermID int identity(1, 1) not null primary key,
                        LoginName sysname not null,
                        PermState nvarchar(60) not null,
                        PermName sysname not null,
                        Class tinyint not null,
                        ClassDesc nvarchar(60) not null,
                        MajorID int not null,
                        SubLoginName sysname null,
                        SubEndPointName sysname null)
    
    Set NoCount On;
    
    If CharIndex('\', @PartnerServer) > 0
      Begin
        Set @Machine = LEFT(@PartnerServer, CharIndex('\', @PartnerServer) - 1);
      End
    Else
      Begin
        Set @Machine = @PartnerServer;
      End
    
    -- Get all Windows logins from principal server
    Set @SQL = 'Select P.name, P.sid, P.is_disabled, P.type, L.password_hash' + CHAR(10) +
            'From ' + QUOTENAME(@PartnerServer) + '.master.sys.server_principals P' + CHAR(10) +
            'Left Join ' + QUOTENAME(@PartnerServer) + '.master.sys.sql_logins L On L.principal_id = P.principal_id' + CHAR(10) +
            'Where P.type In (''U'', ''G'', ''S'')' + CHAR(10) +
            'And P.name <> ''sa''' + CHAR(10) +
            'And P.name Not Like ''##%''' + CHAR(10) +
            'And CharIndex(''' + @Machine + '\'', P.name) = 0;';
    
    Insert Into @Logins (Name, SID, IsDisabled, Type, PasswordHash)
    Exec sp_executesql @SQL;
    
    -- Get all roles from principal server
    Set @SQL = 'Select RoleP.name, LoginP.name' + CHAR(10) +
            'From ' + QUOTENAME(@PartnerServer) + '.master.sys.server_role_members RM' + CHAR(10) +
            'Inner Join ' + QUOTENAME(@PartnerServer) + '.master.sys.server_principals RoleP' +
            CHAR(10) + char(9) + 'On RoleP.principal_id = RM.role_principal_id' + CHAR(10) +
            'Inner Join ' + QUOTENAME(@PartnerServer) + '.master.sys.server_principals LoginP' +
            CHAR(10) + char(9) + 'On LoginP.principal_id = RM.member_principal_id' + CHAR(10) +
            'Where LoginP.type In (''U'', ''G'', ''S'')' + CHAR(10) +
            'And LoginP.name <> ''sa''' + CHAR(10) +
            'And LoginP.name Not Like ''##%''' + CHAR(10) +
            'And RoleP.type = ''R''' + CHAR(10) +
            'And CharIndex(''' + @Machine + '\'', LoginP.name) = 0;';
    
    Insert Into @Roles (RoleName, LoginName)
    Exec sp_executesql @SQL;
    
    -- Get all explicitly granted permissions
    Set @SQL = 'Select P.name Collate database_default,' + CHAR(10) +
            '   SP.state_desc, SP.permission_name, SP.class, SP.class_desc, SP.major_id,' + CHAR(10) +
            '   SubP.name Collate database_default,' + CHAR(10) +
            '   SubEP.name Collate database_default' + CHAR(10) +
            'From ' + QUOTENAME(@PartnerServer) + '.master.sys.server_principals P' + CHAR(10) +
            'Inner Join ' + QUOTENAME(@PartnerServer) + '.master.sys.server_permissions SP' + CHAR(10) +
            CHAR(9) + 'On SP.grantee_principal_id = P.principal_id' + CHAR(10) +
            'Left Join ' + QUOTENAME(@PartnerServer) + '.master.sys.server_principals SubP' + CHAR(10) +
            CHAR(9) + 'On SubP.principal_id = SP.major_id And SP.class = 101' + CHAR(10) +
            'Left Join ' + QUOTENAME(@PartnerServer) + '.master.sys.endpoints SubEP' + CHAR(10) +
            CHAR(9) + 'On SubEP.endpoint_id = SP.major_id And SP.class = 105' + CHAR(10) +
            'Where P.type In (''U'', ''G'', ''S'')' + CHAR(10) +
            'And P.name <> ''sa''' + CHAR(10) +
            'And P.name Not Like ''##%''' + CHAR(10) +
            'And CharIndex(''' + @Machine + '\'', P.name) = 0;'
    
    Insert Into @Perms (LoginName, PermState, PermName, Class, ClassDesc, MajorID, SubLoginName, SubEndPointName)
    Exec sp_executesql @SQL;
    
    Select @MaxID = Max(LoginID), @CurrID = 1
    From @Logins;
    
    While @CurrID <= @MaxID
      Begin
        Select @LoginName = Name,
            @IsDisabled = IsDisabled,
            @Type = [Type],
            @SID = [SID],
            @PasswordHash = PasswordHash
        From @Logins
        Where LoginID = @CurrID;
    
        If Not Exists (Select 1 From sys.server_principals
                    Where name = @LoginName)
          Begin
            Set @SQL = 'Create Login ' + quotename(@LoginName)
            If @Type In ('U', 'G')
              Begin
                Set @SQL = @SQL + ' From Windows;'
              End
            Else
              Begin
                Set @PasswordHashString = '0x' +
                    Cast('' As XML).value('xs:hexBinary(sql:variable("@PasswordHash"))', 'nvarchar(300)');
    
                Set @SQL = @SQL + ' With Password = ' + @PasswordHashString + ' HASHED, ';
    
                Set @SIDString = '0x' +
                    Cast('' As XML).value('xs:hexBinary(sql:variable("@SID"))', 'nvarchar(100)');
                Set @SQL = @SQL + 'SID = ' + @SIDString + ';';
              End
    
            If @Debug = 0
              Begin
                Begin Try
                    Exec sp_executesql @SQL;
                End Try
                Begin Catch
                    Set @ErrNumber = ERROR_NUMBER();
                    Set @ErrSeverity = ERROR_SEVERITY();
                    Set @ErrState = ERROR_STATE();
                    Set @ErrProcedure = ERROR_PROCEDURE();
                    Set @ErrLine = ERROR_LINE();
                    Set @ErrMsg = ERROR_MESSAGE();
                    RaisError(@ErrMsg, 1, 1);
                End Catch
              End
            Else
              Begin
                Print @SQL;
              End
    
            If @IsDisabled = 1
              Begin
                Set @SQL = 'Alter Login ' + quotename(@LoginName) + ' Disable;'
                If @Debug = 0
                  Begin
                    Begin Try
                        Exec sp_executesql @SQL;
                    End Try
                    Begin Catch
                        Set @ErrNumber = ERROR_NUMBER();
                        Set @ErrSeverity = ERROR_SEVERITY();
                        Set @ErrState = ERROR_STATE();
                        Set @ErrProcedure = ERROR_PROCEDURE();
                        Set @ErrLine = ERROR_LINE();
                        Set @ErrMsg = ERROR_MESSAGE();
                        RaisError(@ErrMsg, 1, 1);
                    End Catch
                  End
                Else
                  Begin
                    Print @SQL;
                  End
              End
            End
        Set @CurrID = @CurrID + 1;
      End
    
    Select @MaxID = Max(RoleID), @CurrID = 1
    From @Roles;
    
    While @CurrID <= @MaxID
      Begin
        Select @LoginName = LoginName,
            @RoleName = RoleName
        From @Roles
        Where RoleID = @CurrID;
    
        If Not Exists (Select 1 From sys.server_role_members RM
                    Inner Join sys.server_principals RoleP
                        On RoleP.principal_id = RM.role_principal_id
                    Inner Join sys.server_principals LoginP
                        On LoginP.principal_id = RM.member_principal_id
                    Where LoginP.type In ('U', 'G', 'S')
                    And RoleP.type = 'R'
                    And RoleP.name = @RoleName
                    And LoginP.name = @LoginName)
          Begin
            If @Debug = 0
              Begin
                Exec sp_addsrvrolemember @rolename = @RoleName,
                                @loginame = @LoginName;
              End
            Else
              Begin
                Print 'Exec sp_addsrvrolemember @rolename = ''' + @RoleName + ''',';
                Print '     @loginame = ''' + @LoginName + ''';';
              End
          End
    
        Set @CurrID = @CurrID + 1;
      End
    
    Select @MaxID = Max(PermID), @CurrID = 1
    From @Perms;
    
    While @CurrID <= @MaxID
      Begin
        Select @PermState = PermState,
            @PermName = PermName,
            @Class = Class,
            @LoginName = LoginName,
            @MajorID = MajorID,
            @SQL = PermState + space(1) + PermName + SPACE(1) +
                Case Class When 101 Then 'On Login::' + QUOTENAME(SubLoginName)
                        When 105 Then 'On ' + ClassDesc + '::' + QUOTENAME(SubEndPointName)
                        Else '' End +
                ' To ' + QUOTENAME(LoginName) + ';'
        From @Perms
        Where PermID = @CurrID;
    
        If Not Exists (Select 1 From sys.server_principals P
                    Inner Join sys.server_permissions SP On SP.grantee_principal_id = P.principal_id
                    Where SP.state_desc = @PermState
                    And SP.permission_name = @PermName
                    And SP.class = @Class
                    And P.name = @LoginName
                    And SP.major_id = @MajorID)
          Begin
            If @Debug = 0
              Begin
                Begin Try
                    Exec sp_executesql @SQL;
                End Try
                Begin Catch
                    Set @ErrNumber = ERROR_NUMBER();
                    Set @ErrSeverity = ERROR_SEVERITY();
                    Set @ErrState = ERROR_STATE();
                    Set @ErrProcedure = ERROR_PROCEDURE();
                    Set @ErrLine = ERROR_LINE();
                    Set @ErrMsg = ERROR_MESSAGE();
                    RaisError(@ErrMsg, 1, 1);
                End Catch
              End
            Else
              Begin
                Print @SQL;
              End
          End
    
        Set @CurrID = @CurrID + 1;
      End
    
    Set NoCount Off;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have implemented VirtualPathProvider class so I can keep all my views in the
I have implemented a linked list as a self-referencing database table: CREATE TABLE LinkedList(
i have implemented facebook log in using Facebook javascript SDK in Jquery mobile ,
I have implemented remember me functionality in Symfony2. When I log in with remember
I have implemented a file selector with a combobox. I want to write the
I have implemented a sync adapter and I want to get a callback when
I have implemented a curry function this way: function curry (fn) { var slice
I have implemented RollingFileAppender to log my ASP errors and it works fine. Now
I have implemented what I thought was a pretty decent representation of MVC in
I have implemented a simple file upload-download mechanism. When a user clicks a file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.