Migrating SQL users

This is from http://support.microsoft.com/kb/918992/

Migrating SQL Logins from Server to Server

Okay, So chances are if you are reading this, you want to migrate an entire instance of SQL. That is pretty simple. You just want to detach all databases, and copy over the MDF and LDF users. But what about the Logins? … Yea … The logins AND Passwords.
Thats where this KB comes in handy.

SQL 2005 -> 2005

To transfer the logins and the passwords from the instance of SQL Server on server A to the instance of SQL Server on server B, follow these steps:

  • 1. On server A, start SQL Server Management Studio, and then connect to the instance of SQL Server from which you moved the database.
  • 2. Open a new Query Editor window, and then run the following script.
USE master
GO
IF OBJECT_ID ('sp_hexadecimal') IS NOT NULL
  DROP PROCEDURE sp_hexadecimal
GO
CREATE PROCEDURE sp_hexadecimal
    @binvalue varbinary(256),
    @hexvalue varchar (514) OUTPUT
AS
DECLARE @charvalue varchar (514)
DECLARE @i int
DECLARE @length int
DECLARE @hexstring char(16)
SELECT @charvalue = '0x'
SELECT @i = 1
SELECT @length = DATALENGTH (@binvalue)
SELECT @hexstring = '0123456789ABCDEF'
WHILE (@i <= @length)
BEGIN
  DECLARE @tempint int
  DECLARE @firstint int
  DECLARE @secondint int
  SELECT @tempint = CONVERT(int, SUBSTRING(@binvalue,@i,1))
  SELECT @firstint = FLOOR(@tempint/16)
  SELECT @secondint = @tempint - (@firstint*16)
  SELECT @charvalue = @charvalue +
    SUBSTRING(@hexstring, @firstint+1, 1) +
    SUBSTRING(@hexstring, @secondint+1, 1)
  SELECT @i = @i + 1
END

SELECT @hexvalue = @charvalue
GO

IF OBJECT_ID ('sp_help_revlogin') IS NOT NULL
  DROP PROCEDURE sp_help_revlogin
GO
CREATE PROCEDURE sp_help_revlogin @login_name sysname = NULL AS
DECLARE @name sysname
DECLARE @type varchar (1)
DECLARE @hasaccess int
DECLARE @denylogin int
DECLARE @is_disabled int
DECLARE @PWD_varbinary  varbinary (256)
DECLARE @PWD_string  varchar (514)
DECLARE @SID_varbinary varbinary (85)
DECLARE @SID_string varchar (514)
DECLARE @tmpstr  varchar (1024)
DECLARE @is_policy_checked varchar (3)
DECLARE @is_expiration_checked varchar (3)

DECLARE @defaultdb sysname

IF (@login_name IS NULL)
  DECLARE login_curs CURSOR FOR

      SELECT p.sid, p.name, p.type, p.is_disabled, p.default_database_name, l.hasaccess, l.denylogin FROM
sys.server_principals p LEFT JOIN sys.syslogins l
      ON ( l.name = p.name ) WHERE p.type IN ( 'S', 'G', 'U' ) AND p.name <> 'sa'
ELSE
  DECLARE login_curs CURSOR FOR

      SELECT p.sid, p.name, p.type, p.is_disabled, p.default_database_name, l.hasaccess, l.denylogin FROM
sys.server_principals p LEFT JOIN sys.syslogins l
      ON ( l.name = p.name ) WHERE p.type IN ( 'S', 'G', 'U' ) AND p.name = @login_name
OPEN login_curs

FETCH NEXT FROM login_curs INTO @SID_varbinary, @name, @type, @is_disabled, @defaultdb, @hasaccess, @denylogin
IF (@@fetch_status = -1)
BEGIN
  PRINT 'No login(s) found.'
  CLOSE login_curs
  DEALLOCATE login_curs
  RETURN -1
END
SET @tmpstr = '/* sp_help_revlogin script '
PRINT @tmpstr
SET @tmpstr = '** Generated ' + CONVERT (varchar, GETDATE()) + ' on ' + @@SERVERNAME + ' */'
PRINT @tmpstr
PRINT ''
WHILE (@@fetch_status <> -1)
BEGIN
  IF (@@fetch_status <> -2)
  BEGIN
    PRINT ''
    SET @tmpstr = '-- Login: ' + @name
    PRINT @tmpstr
    IF (@type IN ( 'G', 'U'))
    BEGIN -- NT authenticated account/group

      SET @tmpstr = 'CREATE LOGIN ' + QUOTENAME( @name ) + ' FROM WINDOWS WITH DEFAULT_DATABASE = [' + @defaultdb + ']'
    END
    ELSE BEGIN -- SQL Server authentication
        -- obtain password and sid
            SET @PWD_varbinary = CAST( LOGINPROPERTY( @name, 'PasswordHash' ) AS varbinary (256) )
        EXEC sp_hexadecimal @PWD_varbinary, @PWD_string OUT
        EXEC sp_hexadecimal @SID_varbinary,@SID_string OUT

        -- obtain password policy state
        SELECT @is_policy_checked = CASE is_policy_checked WHEN 1 THEN 'ON' WHEN 0 THEN 'OFF' ELSE NULL END FROM sys.sql_logins WHERE name = @name
        SELECT @is_expiration_checked = CASE is_expiration_checked WHEN 1 THEN 'ON' WHEN 0 THEN 'OFF' ELSE NULL END FROM sys.sql_logins WHERE name = @name

            SET @tmpstr = 'CREATE LOGIN ' + QUOTENAME( @name ) + ' WITH PASSWORD = ' + @PWD_string + ' HASHED, SID = ' + @SID_string + ', DEFAULT_DATABASE = [' + @defaultdb + ']'

        IF ( @is_policy_checked IS NOT NULL )
        BEGIN
          SET @tmpstr = @tmpstr + ', CHECK_POLICY = ' + @is_policy_checked
        END
        IF ( @is_expiration_checked IS NOT NULL )
        BEGIN
          SET @tmpstr = @tmpstr + ', CHECK_EXPIRATION = ' + @is_expiration_checked
        END
    END
    IF (@denylogin = 1)
    BEGIN -- login is denied access
      SET @tmpstr = @tmpstr + '; DENY CONNECT SQL TO ' + QUOTENAME( @name )
    END
    ELSE IF (@hasaccess = 0)
    BEGIN -- login exists but does not have access
      SET @tmpstr = @tmpstr + '; REVOKE CONNECT SQL TO ' + QUOTENAME( @name )
    END
    IF (@is_disabled = 1)
    BEGIN -- login is disabled
      SET @tmpstr = @tmpstr + '; ALTER LOGIN ' + QUOTENAME( @name ) + ' DISABLE'
    END
    PRINT @tmpstr
  END

  FETCH NEXT FROM login_curs INTO @SID_varbinary, @name, @type, @is_disabled, @defaultdb, @hasaccess, @denylogin
   END
CLOSE login_curs
DEALLOCATE login_curs
RETURN 0
GO

Note This script creates two stored procedures in the master database. The two stored procedures are named the sp_hexadecimal stored procedure and the sp_help_revlogin stored procedure.

  • 3. Run the following statement.

EXEC sp_help_revlogin

The output script that is generated by the sp_help_revlogin stored procedure is the login script. This login script creates the logins that have the original Security Identifier (SID) and the original password.

  • 4. On server B, start SQL Server Management Studio, and then connect to the instance of SQL Server to which you moved the database. Important Before you go to step 5, review the information in the “Remarks” section.
  • 5. Open a new Query Editor window, and then run the output script that is generated in step 3.
 

Cloning a VM without VC

taken from:

http://blogs.sun.com/danielc/entry/a_little_script_to_clone and http://hsukumar.wordpress.com/2008/05/14/esx-clone-without-virtual-center/

1. Log into the ESX server using the vmware infrastructure client and turn off the server you want to clone. 2. SSH into the server, sudo to root
3. run: /clone.sh <origina> <clone> example: /clone.sh “App Server 1″ “App Server 2″
4. Once the clone process finishs go back to the Vmware infrastructure client and create a new VM.
5. Select Custom configuration then browse through the options setting anything you’d like until you get to Select a disk. Browse to the vmdk that you cloned and select it, then select finish.
6. Power on the VM, log in and change the IP

The script:

SOURCEVM=$1
TARGETVM=$2
DATASTORE=/vmfs/volumes/MY-VM-STORE

mkdir $DATASTORE/”$2″

vmkfstools -i $DATASTORE/”$1″/”$1″.vmdk $DATASTORE/”$2″/”$2″.vmdk

 

Install mod_suphp

suphp configuration:

download a copy of an rpm from http://dag.wieers.com/rpm/packages/mod_suphp/

after the rpm is installed install

Plesk

1) make following changes to /etc/suphp.conf:

; docroot=/
[handlers]
;Handler for php-scripts
;x-httpd-php=php:/usr/bin/php
suphp-script=php:/usr/bin/php-cgi

;Handler for CGI-scripts
x-suphp-cgi=execute:!self

2) make following changes to /etc/httpd/conf.d/suphp.conf (these are the most important changes, as they’ll prevent other vhosts from using suPHP or just breaking randomly)

suPHP_Engine off
#AddHandler x-httpd-php .php
#AddHandler x-httpd-php .php .php4 .php3 .phtml

3) create /var/www/vhosts/<domain>/conf/vhost.conf as follows:

<Directory /var/www/vhosts/<domain/httpdocs>
suPHP_Engine on
suPHP_UserGroup <user> <group>
suPHP_ConfigPath /var/www/vhosts/<domain>/etc
AddHandler suphp-script .php
</Directory>

4) create /var/www/vhosts/<domain>/etc and copy /etc/php.ini to it

5) create var/www/vhosts/<domain>/tmp and update session save path in php.ini

6) run /usr/local/psa/admin/sbin/websrvmng -av, and restart apache

Not plesk:
remove the /etc/httpd/conf.d/suphp.conf file and did everything in the httpd.conf folder

These are the lines that are needed outside the vhosts

LoadModule suphp_module modules/mod_suphp.so
suPHP_Engine on

These are the lines needed inside of the vhosts

<Directory /var/www/html/<domain>/httpdocs>
       php_admin_flag engine off
       suPHP_Engine on
       suPHP_UserGroup user user
       suPHP_ConfigPath /var/www/html/mobile/etc/php.ini
       SuPHP_AddHandler suphp-script
       AddHandler suphp-script .php
   </Directory>