Restore de Múltiplos Datafiles
26/06/2014 19:45
Este script torna indisponível o banco, realiza o restore de múltiplos datafiles e ativa todos os logins existentes nele.
USE [master]
GO
ALTER DATABASE [CUSTO_PRE_PROD] SET RESTRICTED_USER WITH ROLLBACK IMMEDIATE
GO
RESTORE DATABASE [CUSTO_PRE_PROD] FROM DISK = N'N:\MSSQL2005\BACKUP\CUSTO\CUSTO_1_4.bak', DISK = N'N:\MSSQL2005\BACKUP\CUSTO\CUSTO_3_4.bak', DISK = N'N:\MSSQL2005\BACKUP\CUSTO\CUSTO_4_4.bak', DISK = N'N:\MSSQL2005\BACKUP\CUSTO\CUSTO_2_4.bak' WITH FILE = 1, MOVE N'CUSTO_Data' TO N'S:\MSSQL2005\Data\CUSTO_PreProd.MDF', MOVE N'CUSTO_Data2' TO N'R:\MSSQL2005\Data\CUSTO2_PreProd.MDF', MOVE N'CUSTO_Log' TO N'L:\MSSQL2005\Log\CUSTO_Preprod.LDF', NOUNLOAD, REPLACE, STATS = 10
GO
USE [CUSTO_PRE_PROD]
GO
EXEC sp_change_users_login 'Report'
GO
DECLARE @username VARCHAR(25)
DECLARE @create_login NVARCHAR(1000)
DECLARE fixusers CURSOR
FOR
SELECT
UserName = name
FROM
sysusers
WHERE
issqluser = 1 AND (sid IS NOT NULL AND sid <> 0x0) AND SUSER_SNAME(sid) IS NULL
ORDER BY
name
OPEN fixusers
FETCH NEXT FROM fixusers INTO @username
WHILE @@FETCH_STATUS = 0
BEGIN
BEGIN TRY
EXEC sp_change_users_login 'update_one', @username, @username
PRINT @username + ' atualizado.'
END TRY
BEGIN CATCH
PRINT CONVERT(VARCHAR(10), @@ERROR) + ' ' + ERROR_MESSAGE();
SET @create_login = N'CREATE LOGIN ' + @username + ' WITH PASSWORD = ''altere_a_senha'', CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF'
PRINT @create_login
EXEC sys.sp_executesql @stmt = @create_login
EXEC sp_change_users_login 'update_one', @username, @username;
PRINT 'login ' + @username + ' criado e atualizado.'
END CATCH
FETCH NEXT FROM fixusers INTO @username
END
CLOSE fixusers
DEALLOCATE fixusers
GO