Вы находитесь на странице: 1из 27

| - P |

|
MSDN Library
SQL Server
SQL Server 2008 R2

SQL Server 2008 R2
Database Engine

C Transact-SQL
BACKUP RESTORE (Transact-SQL)
BACKUP (Transact-SQL)
BACKUP CERTIFICATE (Transact-SQL)
BACKUP MASTER KEY (Transact-SQL)
BACKUP SERVICE MASTER KEY (Transact-SQL)

RESTORE ,
RESTORE MASTER KEY (Transact-SQL)
RESTORE SERVICE MASTER KEY (Transact-SQL)

.
...
BACKUP (Transact-SQL)
SQL Server 2008 R2
2 2
,
(BACKUP DATABASE).

(BACKUP LOG).

SQL Server .
(SQL Server).
Transact-SQL

Transact-SQL
Backing Up a Whole Database
BACKUP DATABASE
{ database_name | @database_name_var }
TO <backup_device>
[ ,...n ]
[ <MIRROR TO claus
e>][ next-mirror-to ]
[ WITH { DIFFERENT
IAL | <general_WITH_options> [ ,...n ] }]
[;]
Backing Up Speci
fic Files or Filegroups
BACKUP DATABASE
{ database_name | @database_name_var }
<file_or_filegrou
p> [ ,...n ]
TO <backup_device>
[ ,...n ]
[ <MIRROR TO claus
e>][ next-mirror-to ]
[ WITH { DIFFERENT
IAL | <general_WITH_options> [ ,...n ] }]
[;]
Creating a Parti
al Backup
BACKUP DATABASE
{ database_name | @database_name_var }
READ_WRITE_FILEGR
OUPS [ , <read_only_filegroup> [ ,...n ] ]
TO <backup_device>
[ ,...n ]
[ <MIRROR TO claus
e>][ next-mirror-to ]
[ WITH { DIFFERENT
IAL | <general_WITH_options> [ ,...n ] } ]
[;]
Backing Up the T
ransaction Log (full and bulk-logged recovery models)
BACKUP LOG { dat
abase_name | @database_name_var }
TO <backup_device>
[ ,...n ]
[ <MIRROR TO claus
e>][ next-mirror-to ]
[ WITH { <general_
WITH_options> | <log-specific_optionspec> } [ ,...n ]]
[;]
<backup_device>:
:=
{
{ logical_device_na
me | @logical_device_name_var }
|{ DISK | TAPE } =

physical_device_na

me | @physical_device_name_var }
}
<MIRROR TO claus
e>::=
MIRROR TO <backup
_device> [ ,...n ]
<file_or_filegro
up>::=
{
FILE = { logical_fi
le_name | @logical_file_name_var }
|FILEGROUP = { log
ical_filegroup_name | @logical_filegroup_name_var }
}
<read_only_fileg
roup>::=
FILEGROUP = { lo
gical_filegroup_name | @logical_filegroup_name_var }
<general_WITH_op
tions> [ ,...n ]::=--Backup Set Options
COPY_ONLY
|{ COMPRESSION | N
O_COMPRESSION }
|DESCRIPTION = {
text | @text_variable }
|NAME = { backup_s
et_name | @backup_set_name_var }
|PASSWORD = { pass
word | @password_variable }
|{ EXPIREDATE = {
date | @date_var }

| RETAINDAYS = { days
@days_var } }
--Media Set Opti
ons
{ NOINIT | INIT }
|{ NOSKIP | SKIP }
|{ NOFORMAT | FORM
AT }
|MEDIADESCRIPTION
= { text | @text_variable }
|MEDIANAME = { med
ia_name | @media_name_variable }
|MEDIAPASSWORD = {
mediapassword | @mediapassword_variable }
|BLOCKSIZE = { blo
cksize | @blocksize_variable }
--Data Transfer
Options
BUFFERCOUNT = { buf
fercount | @buffercount_variable }
|MAXTRANSFERSIZE =

{ maxtransfersize | @maxtransfersize_variable }
--Error Manageme
nt Options
{ NO_CHECKSUM | CHE
CKSUM }
|{ STOP_ON_ERROR |
CONTINUE_AFTER_ERROR }
--Compatibility
Options
RESTART
--Monitoring Opt
ions
STATS [ =percentage
]
--Tape Options
{ REWIND | NOREWIND
}
|{ UNLOAD | NOUNLO
AD }
--Log-specific O
ptions
{ NORECOVERY | STAN
DBY =undo_file_name }
|NO_TRUNCATE

DATABASE ( )
, .
, .
SQL Server
,
.
, BACKUP
DATABASE ( ), .

.

master
.
LOG
.
,
.
.

, WITH STOPAT, STOPATMARK
STOPBEFOREMARK RESTORE LOG.



, WITH
NO_TRUNCATE COPY_ONLY. ,
.

, , - .
. .
{ database_name| @database_name_var }
, ,
.
(@database_name_var),
(@database_name_var=database name)
, ntext text.


.
<___> [ ,...n ]
BACKUP DATABASE.
, ,
, ,
.
FILE ={ logical_file_name| @logical_file_name_var }
,
, .
FILEGROUP = { logical_filegroup_name| @logical_filegroup_name_var }
,
, .

, .

,

.
n
, ,
. .
.
( Transact-SQL).
READ_WRITE_FILEGROUPS [ , FILEGROUP = { logical_filegroup_name| @logical_filegro
up_name_var
} [ ,...n ] ]
.
, :

, ,
, .
READ_WRITE_FILEGROUPS
, , ,
.
, READ_WRITE_FILEGROUPS .


FILEGROUP READ_WRITE_FILEGROUPS .
FILEGROUP = { logical_filegroup_name| @logical_filegroup_name_var }
, ,
, ,
. .
<___>.
n
, ,
, .
.
.
TO <__> [ ,...n ]
,

(
MIRROR TO).
<__>
,
.
{ logical_device_name | @logical_device_name_var }
,
.
. (@logical_device_name_var),
(@logical_device_name_var=
____),
, ntext text.
{DISK | TAPE } = {

physical_device_name | @physical_device_name_var }

.

BACKUP. , BACKUP
INIT, .
. .

TAPE SQL Server.



, .
n
, , 
.
MIRROR TO <__> [ ,...n ]
,
, TO.
MIRROR TO
, TO.
MIRROR TO .
SQL Server 200 Enterprise Edition
.

MIRROR TO = DISK BACKUP


. . BLOCKSIZE
.
<__>
. <__> .
n
, , 
. MIRROR TO
TO.
.
.
[ next-mirror-to ]
, , BACKUP
TO MIRROR TO.
WITH Options
,
.
DIFFERENTIAL ()
BACKUP DATABASE. ,

,
. ,
. ,
,
.

BACKUP DATABASE .

.
.

,
.

,
FILE =<___>.
.
RESTORE (Transact-SQL).
COPY_ONLY
, ,
.
,
.
.
, , SQL
Server 200 ,
,
.
, .
BACKUP DATABASE COPY_ONLY,
,
.
, ,
.

.

DIFFERENTIAL COPY_ONLY , COPY_ONLY
.
BACKUP LOG COPY_ONLY
, .
, ,
. ,
.
. .
{ COMPRESSION | NO_COMPRESSION }
, .
SQL Server 2008 Enterprise.
SQL Server 2008 R2, SQL Server
2008 R2 Standard Edition .
.

.
. ( SQL Server Management

Studio).
COMPRESSION
.


.
NO_COMPRESSION
.
DESCRIPTION = { text | @text_variable }
.
2 .
NAME = { backup_set_name| @backup_set_var }
. 28
. NAME , .
PASSWORD = { password | @password_variable }
. PASSWORD
.

Microsoft SQL Server .

, .
,
SQL Server .

.
( MEDIAPASSWORD,
). ( .
.)

.
SQL Server
.

.

, (ACL).
ACL ,
.
{ EXPIREDATE =

date | RETAINDAYS = days }

,
. ,
RETAINDAYS EXPIREDATE.
,

mediaretention. .
.

SQL Server .
,
.
. SKIP FORMAT .
EXPIREDATE = {

date | @date_var }

,
.
(@date_var),
datetime
:
(@date_var = );
( ntext text);
smalldatetime ;
datetime.
:
Dec , 2020 : PM

202
datetime .
.

, SKIP.
RETAINDAYS = { days| @days_var }
, ,
.
(@days_var), .

.
{ NOINIT | INIT }
,
, ,
.
(NOINIT).

{ NOINIT | INIT } {
NOSKIP | SKIP } . .
NOINIT

,
.
, . NOINIT
.
.
.
INIT
,
. INIT ,
, .
BACKUP ,
, :
- .
. EXPIREDATE RETAINDAYS.
, BACKUP ( ),
. .
NAME, .
SKIP.

, SQL Server
, .
SKIP. , ,
, .
. MEDIAPASSWORD,
.
. FORMAT, .
. .
{ NOSKIP | SKIP }
,
.

{ NOINIT | INIT } {
NOSKIP | SKIP } . .
NOSKIP
BACKUP,
, .
.
SKIP
,
BACKUP .
{ INIT | NOINIT } {
NOSKIP | SKIP } . .
,
expiration_date backupset.

{ NOFORMAT | FORMAT }
, ,
,
.
NOFORMAT
,
,
. .
FORMAT
, .
FORMAT
,
. ,

.

FORMAT .
. ,
,
, .
FORMAT SKIP,
SKIP .
MEDIADESCRIPTION = { text | @text_variable }
,
2 .
MEDIANAME = { media_name | @media_name_variable}
.
28 . MEDIANAME,
,
. SKIP,
.
MEDIAPASSWORD = { mediapassword | @mediapassword_variable }
. MEDIAPASSWORD
.

Microsoft SQL Server .

, .
,
. ,

.
. .
FORMAT. ( . .)


.
SQL Server
.

.

, (ACL).
ACL ,
.
BLOCKSIZE = { blocksize | @blocksize_variable }
. 2,  02,
2 08,  0, 8 2,  8, 2 8   ( ).
 2 .
, BACKUP
, .
.
, -
, BLOCKSIZE=208.


.

BUFFERCOUNT = { buffercount | @buffercount_variable }
-,
.
, -
Sqlservr.exe.
:
buffercount
maxtransfersize.
MAXTRANSFERSIZE = { maxtransfersize | @maxtransfersize_variable }
SQL Server
. ,   (
),   0 ( ).

,

.
{ NO_CHECKSUM | CHECKSUM }
, .
NO_CHECKSUM
(
). ,

.
CHECKSUM
, BACKUP
.

.
BACKUP
( ) ,
.
2.
, ,
BACKUP
.

. ,
.
.


.
.
{ STOP_ON_ERROR | CONTINUE_AFTER_ERROR }
,
.
STOP_ON_ERROR
, BACKUP ,
.
.
CONTINUE_AFTER_ERROR
, BACKUP ,
, .

, NO_TRUNCATE,
, CONTINUE_AFTER_ERROR NO_TRUNCATE.

RESTART
. SQL
Server .

STATS [ =percentage ]

,
. percentage , SQL
Server 0 .
STATS
. ;
STATS=0, 0,
 .
,
-.

.
.
{ REWIND | NOREWIND }
REWIND
, SQL Server . REWIND
.
NOREWIND
, SQL Server
.
.
NOREWIND NOUNLOAD,
BACKUP.

NOREWIND SQL Server


, BACKUP
RESTORE, , REWIND UNLOAD
.
, .
.
.
{ UNLOAD | NOUNLOAD }

UNLOADNOUNLOAD ,
.
UNLOAD
,
. UNLOAD
.
NOUNLOAD
, BACKUP
.

BLOCKSIZE .

.
,
BACKUP LOG.

,
. .
.
{ NORECOVERY | STANDBY =undo_file_name }
NORECOVERY

RESTORING. NORECOVERY -
RESTORE.
,

RESTORING, NO_TRUNCATE NORECOVERY.
STANDBY =standby_file_name

STANDBY. STANDBY
( , ).
STANDBY BACKUP LOG WITH NORECOVERY,
RESTORE WITH STANDBY.
,
standby_file_name, .
, Database Engine ;
, Database Engine .
.
,
, RESTORE LOG.
,
,
.
NO_TRUNCATE
, , Database Engine
.
, , NO_TRUNCATE,
.
, .
NO_TRUNCATE BACKUP LOG
COPY_ONLY CONTINUE_AFTER_ERROR.
NO_TRUNCATE ONLINE.
SUSPENDED,
, NO_TRUNCATE.
OFFLINE EMERGENCY, BACKUP NO_TRUNCATE.

.
.


,
.
BACKUP .

,
.
,
.
SQL Server.


SQL Server .
,
.
, .
, 22.
. (Transact-SQL).


.
(
).
.
.
.
, .

BACKUP LOG WITH NO_LOG WITH TRUNCATE_ONLY .



,
,
. .

.
.
.

SQL Server


,
.
. ,
INSERT, UPDATE DELETE.

, ALTER DATABASE
ADD FILE, REMOVE FILE.
.
.

, . ,
,
(
).
, .
,
.

BACKUP
:
FORMAT;
;
.
. .


.

.



.
,
.

,
, ,
.
,
.

.
, .

,
.
,
.



( ).
,
, ,
.

,
.

,
.

.


.
.
.

, .
.
COPY_ONLY BACKUP.
. .
SKIP, NOSKIP, INIT NOINIT
{ NOINIT | INIT } {
NOSKIP | SKIP }.


,
.
, ,
MTF, .
NOINIT
INIT
NOSKIP
,
, ,
MEDIANAME ( ). ,

.
, .
, .
.2
MEDIANAME, ,
.

,
.
, .
,
. .
,
MEDIANAME, MEDIAPASSWORD MEDIADESCRIPTION,
.
SKIP
,

.
,
,
.
, ,
MEDIANAME, MEDIAPASSWORD MEDIADESCRIPTION, .

 MTF .
,
.
2

.

SQL Server ,
:
backupfile (Transact-SQL)
backupfilegroup (Transact-SQL)
backupmediafamily (Transact-SQL)
backupmediaset (Transact-SQL)
backupset (Transact-SQL)
,
msdb,
.


, SQL Server,
SQL Server.
BACKUP RESTART
SQL Server. RESTART
SQL Server 200 .
(

)
,
.
, ,
( , FORMAT).
AdventureWorks2008R2
,
.
BACKUP DATABASE AdventureWorks2008R2
TO DISK= X: SQLServerBackups AdventureWorks2008R2_.bak ,
DISK= Y: SQLServerBackups AdventureWorks2008R2_2.bak ,
DISK= Z: SQLServerBackups AdventureWorks2008R2_.bak
WITH FORMAT,
MEDIANAME = AdventureWorks2008R2StripedSet0 ,
MEDIADESCRIPTION = Striped media set for AdventureWorks2008R2 database;
GO


, FORMAT. :
, ,
,
FORMAT.
FORMAT.
MEDIANAME,
MEDIADESCRIPTION, ,
, .

,
BACKUP TO.
.

.
.

,
.

.
, TO
MIRROR TO .
MIRROR TO
, TO.
, ,
.
.
BACKUP DATABASE AdventureWorks2008R2
TO DISK= X: SQLServerBackups AdventureWorks2008R2_a.bak ,
DISK= Y: SQLServerBackups AdventureWorks2008R2_2a.bak ,
DISK= Z: SQLServerBackups AdventureWorks2008R2_a.bak
MIRROR TO DISK= X: SQLServerBackups AdventureWorks2008R2_b.bak ,

DISK= Y: SQLServerBackups AdventureWorks2008R2_2b.bak ,


DISK= Z: SQLServerBackups AdventureWorks2008R2_b.bak ;
GO

,
. ,
,
,
.

, TO
BACKUP, . , TO
, BACKUP
.
.
.
,
, .
,
. , ,
.


2

0
Z: AdventureWorks2008R2_a.bak
Z: AdventureWorks2008R2_2a.bak
Z: AdventureWorks2008R2_a.bak

Z: AdventureWorks2008R2_b.bak
Z: AdventureWorks2008R2_2b.bak
Z: AdventureWorks2008R2_b.bak

, . ,
,
,
.
.
.
.
, .

BACKUP DATABASE BACKUP LOG


sysadmin db_owner
db_backupoperator.
, ,
, . ,
.
, ,

.

BACKUP.
.
SQL Server
.
.
FORMAT.
. .
.
,
SQL Server,
.
,

.
, ,
.
, ,
.
BACKUP
, PASSWORD. , BACKUP
, MEDIAPASSWORD
. BACKUP ,
,
. BACKUP ,
BACKUP , MEDIAPASSWORD.
SKIP, NOSKIP, INIT
NOINIT . .

. SQL Server
; ,
SQL Server, .
sp_addumpdevice,
, .

,
.

AdventureWorks2008R2 .
AdventureWorks2008R2 , SQL
Server 200. Adventure Works Cycles ,

.. .
AdventureWorks2008R2.
.
.
.

.
.
.

.

.
.

.
.
(Transact-SQL).
.

AdventureWorks2008R2 .
BACKUP DATABASE AdventureWorks2008R2
TO DISK = Z: SQLServerBackups AdvWorksData.bak
WITH FORMAT;
GO
.
AdventureWorks2008R2,
.
AdventureWorks2008R2
.
sp_addumpdevice
, AdvWorksData
, AdvWorksLog
.

AdvWorksData , ,
AdvWorksLog.
-- To permit log backups, before the full database backup, modify the database
-- to use the full recovery model.
USE master;
GO
ALTER DATABASE AdventureWorks2008R2
SET RECOVERY FULL;
GO
-- Create AdvWorksData and AdvWorksLog logical backup devices.
USE master
GO
EXEC sp_addumpdevice disk , AdvWorksData ,
Z: SQLServerBackups AdvWorksData.bak ;
GO
EXEC sp_addumpdevice disk , AdvWorksLog ,

X: SQLServerBackups AdvWorksLog.bak ;
GO
-- Back up the full AdventureWorks2008R2 database.
BACKUP DATABASE AdventureWorks2008R2 TO AdvWorksData;
GO
-- Back up the AdventureWorks2008R2 log.
BACKUP LOG AdventureWorks2008R2
TO AdvWorksLog;
GO

.
,
.
.

.
--Back up the files in SalesGroup:
BACKUP DATABASE Sales
FILEGROUP = SalesGroup ,
FILEGROUP = SalesGroup2
TO DISK = Z: SQLServerBackups SalesFiles.bck
GO
.

.
--Back up the files in SalesGroup:
BACKUP DATABASE Sales
FILEGROUP = SalesGroup ,
FILEGROUP = SalesGroup2
TO DISK = Z: SQLServerBackups SalesFiles.bck
WITH
DIFFERENTIAL
GO
.

(
),
AdventureWorks2008R2.
BACKUP DATABASE AdventureWorks2008R2
TO TAPE = . tape0
MIRROR TO TAPE = . tape
MIRROR TO TAPE = . tape2
MIRROR TO TAPE = . tape
WITH
FORMAT,
MEDIANAME = AdventureWorks2008R2Set0
.

,
. ,
AdventureWorks2008R2
.
BACKUP DATABASE AdventureWorks2008R2
TO TAPE = . tape0 , TAPE = . tape
MIRROR TO TAPE = . tape2 , TAPE = . tape
WITH
FORMAT,
MEDIANAME = AdventureWorks2008R2Set
.

, .
BACKUP LOG AdventureWorks2008R2
TO TAPE = . tape0 , TAPE = . tape
MIRROR TO TAPE = . tape2 , TAPE = . tape
WITH
NOINIT,
MEDIANAME = AdventureWorks2008R2Set

NOINIT, , .
[ ]
.
,

AdventureWorks2008R2.
BACKUP DATABASE AdventureWorks2008R2 TO DISK= Z: SQLServerBackups AdvWorksData.b
ak
WITH
FORMAT,
COMPRESSION
[ ]
.

ALTER DATABASE (Transact-SQL)


DBCC SQLPERF (Transact-SQL)
RESTORE (Transact-SQL)
RESTORE FILELISTONLY (Transact-SQL)
RESTORE HEADERONLY (Transact-SQL)
RESTORE LABELONLY (Transact-SQL)

RESTORE VERIFYONLY (Transact-SQL)


sp_addumpdevice (Transact-SQL)
sp_configure (Transact-SQL)
sp_helpfile (Transact-SQL)
sp_helpfilegroup (Transact-SQL)

,



SQL Server
SQL Server

202 Microsoft. . | |
| | | MSDN |
|