select name, recovery_model_desc from master.sys.databases where name not in ( 'master' ,'model' ,'msdb' ,'ReportServer' ,'ReportServerTempDB' ,'tempdb' ) order by name
C:\SQLMIRROR
ec2-100-100-100-100.compute-1.amazonaws.com
ec2_100_100_100_100_compute_1_amazonaws_com
use master GO /* If there is not already a DATABASE MASTER KEY in the master database, create one. */ IF NOT EXISTS (SELECT * FROM sys.symmetric_keys WHERE symmetric_key_id = 101) CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'MasterKeyPassword'; GO /* Backup the Master Key */ BACKUP MASTER KEY TO FILE = 'C:\sqlmirror\PrincipalServerNormalizedName.DMK' ENCRYPTION BY PASSWORD = 'MasterKeyBackupPassword' GO /* Create Certificate */ CREATE CERTIFICATE PrincipalServerNormalizedName_cert AUTHORIZATION dbo WITH SUBJECT='Certificate for instance on PrincipalServerName' START_DATE='03/09/2013', /* make sure this is a day prior to the current date */ EXPIRY_DATE='12/31/2020'; /* make sure this is set out 10-20 years */ GO /* Create EndPoint */ CREATE ENDPOINT Endpoint_Mirroring STATE = STARTED AS TCP(LISTENER_PORT=8123, LISTENER_IP=ALL) FOR DATABASE_MIRRORING (AUTHENTICATION = CERTIFICATE PrincipalServerNormalizedName_CERT, ENCRYPTION = REQUIRED ALGORITHM AES, ROLE = ALL) GO /* Backup Certificate */ BACKUP CERTIFICATE PrincipalServerNormalizedName_CERT TO FILE = 'C:\sqlmirror\PrincipalServerNormalizedName.cer' GO
use master GO /* If there is not already a DATABASE MASTER KEY in the master database, create one. */ IF NOT EXISTS (SELECT * FROM sys.symmetric_keys WHERE symmetric_key_id = 101) CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'MasterKeyPassword'; GO /* Backup the Master Key */ BACKUP MASTER KEY TO FILE = 'C:\sqlmirror\PrincipalServerNormalizedName.DMK' ENCRYPTION BY PASSWORD = 'MasterKeyBackupPassword' GO /* Create Certificate */ CREATE CERTIFICATE MirrorServerNormalizedName_CERT AUTHORIZATION dbo WITH SUBJECT='Certificate for instance on MirrorServerName' START_DATE='03/09/2013', /* make sure this is a day prior to the current date */ EXPIRY_DATE='12/31/2020'; /* make sure this is set out 10-20 years */ GO /* Create EndPoint */ CREATE ENDPOINT Endpoint_Mirroring STATE = STARTED AS TCP(LISTENER_PORT=8123, LISTENER_IP=ALL) FOR DATABASE_MIRRORING (AUTHENTICATION = CERTIFICATE MirrorServerNormalizedName_CERT, ENCRYPTION = REQUIRED ALGORITHM AES, ROLE = ALL) GO /* Backup Certificate */ BACKUP CERTIFICATE MirrorServerNormalizedName_CERT TO FILE = 'C:\sqlmirror\MirrorServerNormalizedName.cer' GO
use master GO /* If there is not already a DATABASE MASTER KEY in the master database, create one. */ IF NOT EXISTS (SELECT * FROM sys.symmetric_keys WHERE symmetric_key_id = 101) CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'MasterKeyPassword'; GO /* Backup the Master Key */ BACKUP MASTER KEY TO FILE = 'C:\sqlmirror\PrincipalServerNormalizedName.DMK' ENCRYPTION BY PASSWORD = 'MasterKeyBackupPassword' GO /* Create Certificate */ CREATE CERTIFICATE WitnessServerNormalizedName_CERT AUTHORIZATION dbo WITH SUBJECT='Certificate for instance on WitnessServerName' START_DATE='03/09/2013', /* make sure this is a day prior to the current date */ EXPIRY_DATE='12/31/2020'; /* make sure this is set out 10-20 years */ GO /* Create EndPoint */ CREATE ENDPOINT Endpoint_Mirroring STATE = STARTED AS TCP(LISTENER_PORT=8122, LISTENER_IP=ALL) FOR DATABASE_MIRRORING (AUTHENTICATION = CERTIFICATE WitnessServerNormalizedName_CERT, ENCRYPTION = REQUIRED ALGORITHM AES, ROLE = WITNESS) GO /* Backup Certificate */ BACKUP CERTIFICATE WitnessServerNormalizedName_CERT TO FILE = 'C:\sqlmirror\WitnessServerNormalizedName.cer' GO
*.CER
/* MIRROR */ use master GO CREATE LOGIN MirrorServerNormalizedName_LOGIN WITH PASSWORD='Password1!'; GO CREATE USER MirrorServerNormalizedName_USER FOR LOGIN MirrorServerNormalizedName_LOGIN; GO CREATE CERTIFICATE MirrorServerNormalizedName_CERT FROM FILE = 'C:\sqlmirror\MirrorServerNormalizedName.cer'; GO GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [MirrorServerNormalizedName_LOGIN]; GO /* WITNESS */ use master GO CREATE LOGIN WitnessServerNormalizedName_LOGIN WITH PASSWORD='Password1!'; GO CREATE USER WitnessServerNormalizedName_USER FOR LOGIN WitnessServerNormalizedName_LOGIN; GO CREATE CERTIFICATE WitnessServerNormalizedName_CERT FROM FILE = 'C:\sqlmirror\WitnessServerNormalizedName.cer'; GO GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [WitnessServerNormalizedName_LOGIN]; GO
/* PRINCIPAL */ use master GO CREATE LOGIN PrincipalServerNormalizedName_LOGIN WITH PASSWORD='Password1!'; GO CREATE USER PrincipalServerNormalizedName_USER FOR LOGIN PrincipalServerNormalizedName_LOGIN; GO CREATE CERTIFICATE PrincipalServerNormalizedName_CERT FROM FILE = 'C:\sqlmirror\PrincipalServerNormalizedName.cer'; GO GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [PrincipalServerNormalizedName_LOGIN]; GO /* WITNESS */ use master GO CREATE LOGIN WitnessServerNormalizedName_LOGIN WITH PASSWORD='Password1!'; GO CREATE USER WitnessServerNormalizedName_USER FOR LOGIN WitnessServerNormalizedName_LOGIN; GO CREATE CERTIFICATE WitnessServerNormalizedName_CERT FROM FILE = 'C:\sqlmirror\WitnessServerNormalizedName.cer'; GO GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [WitnessServerNormalizedName_LOGIN]; GO
/* PRINCIPAL */ use master GO CREATE LOGIN PrincipalServerNormalizedName_LOGIN WITH PASSWORD='Password1!'; GO CREATE USER PrincipalServerNormalizedName_USER FOR LOGIN PrincipalServerNormalizedName_LOGIN; GO CREATE CERTIFICATE PrincipalServerNormalizedName_CERT FROM FILE = 'C:\sqlmirror\PrincipalServerNormalizedName.cer'; GO GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [PrincipalServerNormalizedName_LOGIN]; GO /* MIRROR */ use master GO CREATE LOGIN MirrorServerNormalizedName_LOGIN WITH PASSWORD='Password1!'; GO CREATE USER MirrorServerNormalizedName_USER FOR LOGIN MirrorServerNormalizedName_LOGIN; GO CREATE CERTIFICATE MirrorServerNormalizedName_CERT FROM FILE = 'C:\sqlmirror\MirrorServerNormalizedName.cer'; GO GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [MirrorServerNormalizedName_LOGIN]; GO
BACKUP DATABASE [DatabaseName] TO DISK = 'C:\SQLMIRROR\DatabaseName-CurrentDate:YYYY-MM-DD.bak
BACKUP LOG [DatabaseName] TO DISK = N'C:\SQLMIRROR\DatabaseName-CurrentDate:YYYY-MM-DD_LOG.bak' WITH NOFORMAT, NOINIT, NAME = N'DatabaseName-Transaction Log Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10
*.BAK
DROP DATABASE [DatabaseName] GO RESTORE DATABASE [DatabaseName] FROM DISK = 'C:\SQLMIRROR\DatabaseName-CurrentDate:YYYY-MM-DD.bak WITH NORECOVERY, NOUNLOAD, STATS = 10 GO
RESTORE LOG [DatabaseName] FROM DISK = N'C:\SQLMIRROR\DatabaseName-CurrentDate:YYYY-MM-DD_LOG.bak' WITH FILE = 1, NORECOVERY, NOUNLOAD, STATS = 10
ALTER DATABASE DatabaseName SET PARTNER OFF
ALTER DATABASE [DatabaseName] SET PARTNER = 'TCP://PrincipalServerName:8123'
ALTER DATABASE DatabaseName SET WITNESS OFF
ALTER DATABASE [DatabaseName] SET PARTNER = 'TCP://MirrorServerName:8123' GO ALTER DATABASE [DatabaseName] SET WITNESS = 'TCP://WitnessServerName:8122' GO