2009-11-13

Move databases from SQL 2005 to SQL 2008

I am awfully new at SQL server and this was a real headache for me. So I decided to document this experience so I never have to go through all those google pages again :)

Contents:
Step 1 - Making a backup of database.
Step 2 - Restoring databases.
Step 3 - Transfer of logins and passwords



Source: Server A - SQL 2005
Destination: Server B - SQL 2008

Step 1 - Making a backup of database.

On the source server: Open Microsoft SQL Server Management Studio

method a)
(preferable when backuping a single database)

expand ServerA -> Databases
right click on the database you want to backup and choose Tasks -> Back Up
Backup type: Full
Backup component: Database
now choose where you want to put your backup file - click Add and enter path and file name (you can enter share location as well for example \\Server_B\backups\database1.bak)
Click OK.

method b) (when backuping few, lots or all databases)

expand ServerA -> Management
right click Maintenance Plans and choose Maintenance Plan Wizard
on the first page of the wizard click Next
on the second page enter name for your backup plan, choose "Single schedule for the entire plan or no schedule", enter schedule setings or leave it on demand if you want to run it manualy, and click Next.
third page select "Back Up Database (Full)" and click Next
fourth page - Next
fifth page - choose which databases you want to backup, select "Create a backup file for every database" and select a location for backups (preferably share on the destination server \\Server_B\backups\)
sixth page - Next
and at last - Finish
after wizard finishes to create your maintenance plan - click Close
Now expand Maintenante Plans and you will see your maintenance plan listed there. Right click it and Execute.


Step 2
- Restoring databases.


On the destination server: Open Microsoft SQL Server Management Studio
expand ServerB, right click Databases and choose Restore Database
enter the name of database you are going to restore
To database: database1
then choose "From device" and browse for you database backup file
once you add it select it and press OK

Step 3 - Transfer of logins and passwords (http://support.microsoft.com/kb/918992/)

3.1 On source Server A open New Query in the Microsoft SQL Management Studio
paste and execute the following code:

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


this creates a stored procedure named sp_help_revlogin in ServerA/Databases/System Databases/Master/Programmability/Stored Procedures

3.2 Open New Query window again and run this code:
EXEC sp_help_revlogin


3.3 copy the outcome of the step 3.2 and paste it in the New Query window on destination Server B, execute it.

2009-11-07

Enable Remote Desktop remotely via Group Policy

Have you been in a situation where you need to remotely connect to some workplace and do some administrator stuff, but can not because Remote Desktop is not enabled and the user who is near that PC has no admin rights to enable Remote Desktop?


So here is what you do (Windows Server 2008 R2 in this case):
Connect to your domain controller -> Open Group Policy Management Editor
navigate to:
Computer Configuration > Administrative Templates > Windows Components > Remote Desktop Services > Remote Desktop Session Host > Connections
Enable: Allow users to connect remotely using Remote Desktop Services

Also you might need to add an exception to Windows Firewall, to do this via Group Policy navigate to:
Computer Configuration > Administrative Templates > Network > Network Connections > Windows Firewall > Domain Profile
Enable: Windows Firewall: Allow inbound Remote Desktop exceptions

Links: http://www.twistedethics.com/2009/01/06/how-to-enable-remote-desktop-via-group-policy/comment-page-1/