Sysadmin Blog Just another IT Blog

26Mar/130

Move SQL databases between two servers

Had to look into moving all SharePoint Databases to a new SQL Server (2012 of course). Used the backup script I previously posted and found a restore script:

http://blogs.msdn.com/b/ai/archive/2012/08/09/migrating-sql-server-2000-user-databases-to-sql-server-2012.aspx

I used the 2008 to 2012 restore script but edited it slightly:

 /********************************************************************************

*             Restore SQL server 2008 backed databases to

*             SQL server 2012

********************************************************************************/

use master;

go

/*   Please un-comment below part to enable cp_cmdshell procedures

Don't forget to disable it after migration completed.

EXEC sp_configure'xp_cmdshell', 1

RECONFIGURE

GO

EXEC sp_configure 'show advanced options', 1

RECONFIGURE

GO*/

DECLARE @Return int

IF OBJECT_ID('tempdb..#SQL2008BackedUpdatabases') IS NOT NULL DROP TABLE #SQL2008BackedUpdatabases

CREATE TABLE #SQL2008BackedUpdatabases (BakFileName varchar(400), ID int IDENTITY(1,1))

INSERT  #SQL2008BackedUpdatabases EXECUTE @Return = master.dbo.xp_cmdshell  'dir /B *.bak D:\SharePoint'

DECLARE UserDatabases_CTE_Cursor Cursor

FOR

-- Restoring user database names.

select BakFileName from #SQL2008BackedUpdatabases

where  BakFileName like '%bak'

OPEN UserDatabases_CTE_Cursor

DECLARE @BakFileName varchar(200);

DECLARE @dbName varchar(200);

DECLARE @backupPath varchar(200);

DECLARE @SQL2012RestoreFolder varchar(200);

DECLARE @backupQuery varchar(600);

-- make sure that the below path exists

set @backupPath = 'D:\SharePoint\'

-- make sure that the below path exists

set @SQL2012RestoreFolder = 'G:\MSSQL11.SQLSHAREPOINT\MSSQL\DATA\'

set @SQL2012RestoreFolder = 'G:\MSSQL11.SQLSHAREPOINT\MSSQL\DATA\'

Fetch NEXT FROM UserDatabases_CTE_Cursor INTO @BakFileName

While (@@FETCH_STATUS <> -1)

BEGIN

set @dbName = REPLACE(@BakFileName, '.bak', '')

-- Restore SQL 2008 backed databases to SQL server 2012

set @backupQuery =  'RESTORE DATABASE [' +@dbName

+ '] FROM DISK = ''' + @backupPath + @BakFileName +''' '

+'WITH MOVE ''' + @dbName +''' TO ''' + @SQL2012RestoreFolder + @dbName + '.mdf'','

+ 'MOVE ''' + @dbName +'_Log'' TO ''' + @SQL2012RestoreFolder + @dbName +'.ldf'''

-- Print SQL statement

print @backupQuery

-- Execute backup script

EXEC (@backupQuery)

-- Get next database

Fetch NEXT FROM UserDatabases_CTE_Cursor INTO @BakFileName

END

CLOSE UserDatabases_CTE_Cursor

DEALLOCATE UserDatabases_CTE_Cursor

GO

 

If you want to simply create the restore commands without actually restoring you can comment out the EXEC line at the end. Also make sure to adjust the parts marked in red to your needs.

 

Thanks,

Andreas

14Mar/110

Office applications requesting Authentication for SharePoint

Had a problem recently with SharePoint 2010 when we changed our main way of accessing the site to a public domain name. This change made all Office Applications 2007 and 2010 request authentication when opening and saving to or from SharePoint.

After a longer research into the issue I found this Microsoft KB.

In there Microsoft informs about how WebDav secures sites or networks. I always knew that SharePoint uses WebDav to share documents to Office applications however I had no idea that you had to specify a trusted location if it uses an Internet FQDN instead of a local netbios name.

So I followed the KB and I actually deloyed the registry key via Group Policy Prefferences and after people restarted their PCs no more calls about having to authenticate.

Thanks,

Andreas

3Sep/100

Transfer logins between SQL instances

I just moved our SharePoint 2010 and Project 2010 databases (Yeah it uses like 10). Anyway I realized that moving the database and creating the SQL Alias, so the server believes that the Database is still local, is going to be easy. However I also wanted the same logins on the new remote instance as I had on the local instance. After a quick google I found a script from Microsoft support: http://support.microsoft.com/kb/918992/

First run this Query:

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 two stored procedures in the master database.

To actually use the stored procedure to create a script for us you need to be logged in with a user that has the SQL server sysadmin role.

If you have that role go ahead and run:

EXEC sp_help_revlogin

as a new Query.

The result of the query should be a SQL query script that creates the logins on the new server. Before I ran it on the new server I deleted all the logins that already exist for example my own, local system and etc.

Now you only have to be sysadmin on the new SQL instance so you can run the script on there and you should be all set.

I hope it works for you as good as it did for me

Andreas