************************ Backup *****************************************
- backup Database to Disk
backup database master to disk = 'path'
- Backup Databse to Backup device
Create backup device
sp_addumpdevice @physicalType , @logicalname , 'Store Path'
Backup Database to device
backup database master to @devicename
************************ Restore *****************************************
1. Install the new SQL Server Instance
2. Start SQL Server Instance
3. Install Service Pack and hotfix
4. Stop SQL Agent
5. Start the SQL Server in single-user mode
6. Use SQLCMD log on to SQL Server instance with system administrator account
7. Restore the master database with excute command
Restore database [Master] from disk = 'path'
วันพฤหัสบดีที่ 29 เมษายน พ.ศ. 2553
Database Miroring with T-SQL
This example shows all the stages required to create a database mirroring session with a witness using Windows Authentication. The examples in this topic use Transact-SQL. Note that as an alternative to using Transact-SQL steps, you can use the Configure Database Mirroring Security Wizard for database mirroring setup. For more information, see Managing Database Mirroring (SQL Server Management Studio).
Prerequisite
________________________________________
The example uses the AdventureWorks sample database, which uses the simple recovery model by default. To use database mirroring with this database, you must alter it to use the full recovery model. To do this in Transact-SQL, use the ALTER DATABASE statement, as follows:
USE master;
GO
ALTER DATABASE AdventureWorks
SET RECOVERY FULL;
GO
For information on changing the recovery model in SQL Server Management Studio, see How to: View or Change the Recovery Model of a Database (SQL Server Management Studio).
Example
________________________________________
In this example, the two partners and the witness are the default server instances on three computer systems. The three server instances run the same Windows domain, but the user account is different for the example's witness server instance.
The following table summarizes the values used in this example.
Initial mirroring role Host system Domain user account
Principal PARTNERHOST1\
Mirror PARTNERHOST5\
Witness WITNESSHOST4\
1. Create an endpoint on the principal server instance (default instance on PARTNERHOST1).
CREATE ENDPOINT Endpoint_Mirroring
STATE=STARTED
AS TCP (LISTENER_PORT=7022)
FOR DATABASE_MIRRORING (ROLE=PARTNER)
GO
--Partners under same domain user; login already exists in master.
--Create a login for the witness server instance,
--which is running as Somedomain\witnessuser:
USE master ;
GO
CREATE LOGIN [Somedomain\witnessuser] FROM WINDOWS ;
GO
-- Grant connect permissions on endpoint to login account of witness.
GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [Somedomain\witnessuser];
GO
2. Create an endpoint on the mirror server instance (default instance on PARTNERHOST5).
CREATE ENDPOINT Endpoint_Mirroring
STATE=STARTED
AS TCP (LISTENER_PORT=7022)
FOR DATABASE_MIRRORING (ROLE=ALL)
GO
--Partners under same domain user; login already exists in master.
--Create a login for the witness server instance,
--which is running as Somedomain\witnessuser:
USE master ;
GO
CREATE LOGIN [Somedomain\witnessuser] FROM WINDOWS ;
GO
--Grant connect permissions on endpoint to login account of witness.
GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [Somedomain\witnessuser];
GO
3. Create an endpoint on the witness server instance (default instance on WITNESSHOST4).
CREATE ENDPOINT Endpoint_Mirroring
STATE=STARTED
AS TCP (LISTENER_PORT=7022)
FOR DATABASE_MIRRORING (ROLE=WITNESS)
GO
--Create a login for the partner server instances,
--which are both running as Mydomain\dbousername:
USE master ;
GO
CREATE LOGIN [Mydomain\dbousername] FROM WINDOWS ;
GO
--Grant connect permissions on endpoint to login account of partners.
GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [Mydomain\dbousername];
GO
4. Create the mirror database. For more information, see How to: Prepare a Mirror Database for Mirroring (Transact-SQL).
5. On the mirror server instance on PARTNERHOST5, set the server instance on PARTNERHOST1 as the partner (making it the initial principal server instance).
ALTER DATABASE AdventureWorks
SET PARTNER =
'TCP://PARTNERHOST1.COM:7022'
GO
6. On the principal server instance on PARTNERHOST1, set the server instance on PARTNERHOST5 as the partner (making it the initial mirror server instance).
ALTER DATABASE AdventureWorks
SET PARTNER = 'TCP://PARTNERHOST5.COM:7022'
GO
7. On the principal server, set the witness (which is on WITNESSHOST4).
ALTER DATABASE AdventureWorks
SET WITNESS =
'TCP://WITNESSHOST4.COM:7022'
GO
reference : http://msdn.microsft.com/
Prerequisite
________________________________________
The example uses the AdventureWorks sample database, which uses the simple recovery model by default. To use database mirroring with this database, you must alter it to use the full recovery model. To do this in Transact-SQL, use the ALTER DATABASE statement, as follows:
USE master;
GO
ALTER DATABASE AdventureWorks
SET RECOVERY FULL;
GO
For information on changing the recovery model in SQL Server Management Studio, see How to: View or Change the Recovery Model of a Database (SQL Server Management Studio).
Example
________________________________________
In this example, the two partners and the witness are the default server instances on three computer systems. The three server instances run the same Windows domain, but the user account is different for the example's witness server instance.
The following table summarizes the values used in this example.
Initial mirroring role Host system Domain user account
Principal PARTNERHOST1
Mirror PARTNERHOST5
Witness WITNESSHOST4
1. Create an endpoint on the principal server instance (default instance on PARTNERHOST1).
CREATE ENDPOINT Endpoint_Mirroring
STATE=STARTED
AS TCP (LISTENER_PORT=7022)
FOR DATABASE_MIRRORING (ROLE=PARTNER)
GO
--Partners under same domain user; login already exists in master.
--Create a login for the witness server instance,
--which is running as Somedomain\witnessuser:
USE master ;
GO
CREATE LOGIN [Somedomain\witnessuser] FROM WINDOWS ;
GO
-- Grant connect permissions on endpoint to login account of witness.
GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [Somedomain\witnessuser];
GO
2. Create an endpoint on the mirror server instance (default instance on PARTNERHOST5).
CREATE ENDPOINT Endpoint_Mirroring
STATE=STARTED
AS TCP (LISTENER_PORT=7022)
FOR DATABASE_MIRRORING (ROLE=ALL)
GO
--Partners under same domain user; login already exists in master.
--Create a login for the witness server instance,
--which is running as Somedomain\witnessuser:
USE master ;
GO
CREATE LOGIN [Somedomain\witnessuser] FROM WINDOWS ;
GO
--Grant connect permissions on endpoint to login account of witness.
GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [Somedomain\witnessuser];
GO
3. Create an endpoint on the witness server instance (default instance on WITNESSHOST4).
CREATE ENDPOINT Endpoint_Mirroring
STATE=STARTED
AS TCP (LISTENER_PORT=7022)
FOR DATABASE_MIRRORING (ROLE=WITNESS)
GO
--Create a login for the partner server instances,
--which are both running as Mydomain\dbousername:
USE master ;
GO
CREATE LOGIN [Mydomain\dbousername] FROM WINDOWS ;
GO
--Grant connect permissions on endpoint to login account of partners.
GRANT CONNECT ON ENDPOINT::Endpoint_Mirroring TO [Mydomain\dbousername];
GO
4. Create the mirror database. For more information, see How to: Prepare a Mirror Database for Mirroring (Transact-SQL).
5. On the mirror server instance on PARTNERHOST5, set the server instance on PARTNERHOST1 as the partner (making it the initial principal server instance).
ALTER DATABASE AdventureWorks
SET PARTNER =
'TCP://PARTNERHOST1.COM:7022'
GO
6. On the principal server instance on PARTNERHOST1, set the server instance on PARTNERHOST5 as the partner (making it the initial mirror server instance).
ALTER DATABASE AdventureWorks
SET PARTNER = 'TCP://PARTNERHOST5.COM:7022'
GO
7. On the principal server, set the witness (which is on WITNESSHOST4).
ALTER DATABASE AdventureWorks
SET WITNESS =
'TCP://WITNESSHOST4.COM:7022'
GO
reference : http://msdn.microsft.com/
Switch Recovery Model with T-SQL
Full recovery model
alter database [databasename] set recovery full
Bulk log recovery model
alter database [databasename] set recovery bulk_logged
Simple recovery model
alter database [databasename] set recovery simple
alter database [databasename] set recovery full
Bulk log recovery model
alter database [databasename] set recovery bulk_logged
Simple recovery model
alter database [databasename] set recovery simple
Backup Database with T-SQL to Backup device
Step by Step
1. Create Backup Device
exec sp_addumpdevice 'disk','adventureworks2008backup','d:\backupdb\aw2008.bak'
2. verify backup device
select * from sysdevices
3. Remove Backup device
sp_dropdevice @logicalname = 'adventureworks2008bakup'
4. backup database to device
backup database adventureworks to adventureworks2008backup with init ,stats = 10
5. Verify Backup
restore verifyonly from adventureworks2008backup
1. Create Backup Device
exec sp_addumpdevice 'disk','adventureworks2008backup','d:\backupdb\aw2008.bak'
2. verify backup device
select * from sysdevices
3. Remove Backup device
sp_dropdevice @logicalname = 'adventureworks2008bakup'
4. backup database to device
backup database adventureworks to adventureworks2008backup with init ,stats = 10
5. Verify Backup
restore verifyonly from adventureworks2008backup
วันพุธที่ 21 เมษายน พ.ศ. 2553
Add user with T-SQL
Syntax
sp_adduser [ @loginame = ] 'login'
[ , [ @name_in_db = ] 'user' ]
[ , [ @grpname = ] 'role' ]
Sample
EXEC sp_adduser 'Vidur', 'Vidur', 'Recruiting'
sp_adduser [ @loginame = ] 'login'
[ , [ @name_in_db = ] 'user' ]
[ , [ @grpname = ] 'role' ]
Sample
EXEC sp_adduser 'Vidur', 'Vidur', 'Recruiting'
Basic backup and Restore Database with T-SQL
******* Backup Database *******
Backup To Disk
backup Database Databasename to Disk= 'File Destination path'
Backup To Device
sp_adddumpdevice @devicename
Backup Database Databasename to device
******* Restore Database ******
Restore From Disk
Restore database Databasename from disk= 'File Destination path'
Restore From Device
Restore database Databasename from device = Devicename
Backup To Disk
backup Database Databasename to Disk= 'File Destination path'
Backup To Device
sp_adddumpdevice @devicename
Backup Database Databasename to device
******* Restore Database ******
Restore From Disk
Restore database Databasename from disk= 'File Destination path'
Restore From Device
Restore database Databasename from device = Devicename
Database Snapshot (Transact-SQL)
******* Create Database Snapshot (T-SQL)*******
CREATE DATABASE AdventureWorks_dbss1800 ON
( NAME = AdventureWorks_Data, FILENAME =
'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\Data\AdventureWorks_data_1800.ss' )
AS SNAPSHOT OF AdventureWorks;
GO
******* View Database Snapshot (T-SQL) *******
slect * from sys.databases
******* Revert Database Snapshot (T-SQL) *******
USE master;
-- Reverting AdventureWorks to AdventureWorks_dbss1800
RESTORE DATABASE AdventureWorks from
DATABASE_SNAPSHOT = 'AdventureWorks_dbss1800';
GO
******* Drop Database Snapshot (T-SQL) *******
DROP DATABASE SalesSnapshot0600
CREATE DATABASE AdventureWorks_dbss1800 ON
( NAME = AdventureWorks_Data, FILENAME =
'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\Data\AdventureWorks_data_1800.ss' )
AS SNAPSHOT OF AdventureWorks;
GO
******* View Database Snapshot (T-SQL) *******
slect * from sys.databases
******* Revert Database Snapshot (T-SQL) *******
USE master;
-- Reverting AdventureWorks to AdventureWorks_dbss1800
RESTORE DATABASE AdventureWorks from
DATABASE_SNAPSHOT = 'AdventureWorks_dbss1800';
GO
******* Drop Database Snapshot (T-SQL) *******
DROP DATABASE SalesSnapshot0600
สมัครสมาชิก:
บทความ (Atom)