Sql Server备份与还原

1、完全备份与还原

create table dept
(
	dept_id int identity(1,1) primary key,
	dept_name nvarchar(256)
	
);

insert into dept(dept_name) values('张三001');
insert into dept(dept_name) values('张三002');
insert into dept(dept_name) values('张三003');
insert into dept(dept_name) values('张三004');

select * from dept;

backup database test to  disk='D:\test001.bak';

truncate table dept;

use master;

restore database test from disk='D:\test001.bak' with replace;

use test;

select * from dept;

完全备份简单,恢复也很简单,但是如果数据库太大,可能会花很长时间,针对这个问题吗,需要制定好备份计划,

比如说:

周日:全备份

周一:日志备份

周二:差异备份

周三:日志备份

周四:日志备份

周五:差异备份

周六:日志备份

2、差异备份与还原

if exists(select * from sys.tables where name='dept')
	drop table dept
GO

create table dept
(
	dept_id int identity(1,1) primary key,
	dept_name nvarchar(256)
	
)
GO

insert into dept(dept_name) values('张三001');
insert into dept(dept_name) values('张三002');
insert into dept(dept_name) values('张三003');
insert into dept(dept_name) values('张三004');

select * from dept;

backup database test to  disk='D:\test001.bak';

truncate table dept;

insert into dept(dept_name) values('李四001');
insert into dept(dept_name) values('李四002');
insert into dept(dept_name) values('李四003');
insert into dept(dept_name) values('李四004');

backup database test to  disk='D:\test001_001.diff' with differential;

use master;

restore database test from disk='D:\test001.bak' with norecovery,replace;
restore database test from disk='D:\test001_001.diff' with recovery;

use test;

select * from dept;

差异备份的还原需要建立在一次全备份基础之上,并且中间不能有间隔,由于数据比较重要建议使用完整恢复模式

alter database test set  recovery full;

3、日志备份与还原

同样道理,日至还原也是需要建立在一次全备份基础之上,中间不能有间隔,最好使用完全恢复模式

if exists(select * from sys.tables where name='dept')
	drop table dept
GO

create table dept
(
	dept_id int identity(1,1) primary key,
	dept_name nvarchar(256)
	
)
GO

insert into dept(dept_name) values('张三001');
insert into dept(dept_name) values('张三002');
insert into dept(dept_name) values('张三003');
insert into dept(dept_name) values('张三004');

select * from dept;

backup database test to  disk='D:\test001.bak';

delete from dept where dept_id=1;

insert into dept(dept_name) values('李四001');
insert into dept(dept_name) values('李四002');
insert into dept(dept_name) values('李四003');
insert into dept(dept_name) values('李四004');

backup log test to  disk='D:\test001_001.trn';

delete from dept where dept_id=5;

insert into dept(dept_name) values('王五001');
insert into dept(dept_name) values('王五002');
insert into dept(dept_name) values('王五003');
insert into dept(dept_name) values('王五004');

backup log test to  disk='D:\test001_002.trn';
use master;

restore database test from disk='D:\test001.bak' with norecovery,replace;
restore log test from disk='D:\test001_001.trn' with norecovery;
restore log test from disk='D:\test001_002.trn' with recovery;

use test;

select * from dept;

日志还原还可以还原到时间点:

restore database test from disk='D:\test001.bak' with norecovery,replace;
restore log test from disk='D:\test001_001.trn' with norecovery;
restore log test from disk='D:\test001_002.trn' with norecovery,stopat='2013-07-10 16:18:50';
restore database test with recovery

注:针对差异备份与日志备份的还原,必须建议在一次全备份基础之上,并且中间的备份不能出现间隔

例如:2013-07-09 16:20:13  全备份

2013-07-10 16:20:13 差异(日志)备份

2013-07-11 16:20:13 差异(日志)备份

2013-07-12 16:20:13 差异(日志)备份

然后我想还原到2013-07-11 09:50:10这个时间点,但是发现2013-07-10 16:20:13这个备份丢失了,

这样肯定就不行了

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

关于备份与恢复还有许多具体的参数,可以参考微软提供的文档

全备份:
BACKUP DATABASE { database_name | @database_name_var } 
  TO <backup_device> [ ,...n ] 
  [ <MIRROR TO clause> ] [ next-mirror-to ]
  [ WITH { DIFFERENTIAL | <general_WITH_options> [ ,...n ] } ]
[;]

文件、文件组备份:
BACKUP DATABASE { database_name | @database_name_var } 
 <file_or_filegroup> [ ,...n ] 
  TO <backup_device> [ ,...n ] 
  [ <MIRROR TO clause> ] [ next-mirror-to ]
  [ WITH { DIFFERENTIAL | <general_WITH_options> [ ,...n ] } ]
[;]

部分备份:
BACKUP DATABASE { database_name | @database_name_var } 
 READ_WRITE_FILEGROUPS [ , <read_only_filegroup> [ ,...n ] ]
  TO <backup_device> [ ,...n ] 
  [ <MIRROR TO clause> ] [ next-mirror-to ]
  [ WITH { DIFFERENTIAL | <general_WITH_options> [ ,...n ] } ]
[;]
日志备份 (full and bulk-logged recovery models)
BACKUP LOG { database_name | @database_name_var } 
  TO <backup_device> [ ,...n ] 
  [ <MIRROR TO clause> ] [ next-mirror-to ]
  [ WITH { <general_WITH_options> | <log-specific_optionspec> } [ ,...n ] ]
[;]

<backup_device>::= 
 {
   { logical_device_name | @logical_device_name_var } 
 | { DISK | TAPE } = 
     { 'physical_device_name' | @physical_device_name_var }
 } 

<MIRROR TO clause>::=
 MIRROR TO <backup_device> [ ,...n ]

<file_or_filegroup>::=
 {
   FILE = { logical_file_name | @logical_file_name_var } 
 | FILEGROUP = { logical_filegroup_name | @logical_filegroup_name_var }
 } 

<read_only_filegroup>::=
FILEGROUP = { logical_filegroup_name | @logical_filegroup_name_var }

<general_WITH_options> [ ,...n ]::= 
--Backup Set Options
   COPY_ONLY 
 | { COMPRESSION | NO_COMPRESSION } 
 | DESCRIPTION = { 'text' | @text_variable } 
 | NAME = { backup_set_name | @backup_set_name_var } 
 | PASSWORD = { password | @password_variable } 
 | { EXPIREDATE = { 'date' | @date_var } 
        | RETAINDAYS = { days | @days_var } } 

--Media Set Options
   { NOINIT | INIT } 
 | { NOSKIP | SKIP } 
 | { NOFORMAT | FORMAT } 
 | MEDIADESCRIPTION = { 'text' | @text_variable } 
 | MEDIANAME = { media_name | @media_name_variable } 
 | MEDIAPASSWORD = { mediapassword | @mediapassword_variable } 
 | BLOCKSIZE = { blocksize | @blocksize_variable } 

--Data Transfer Options
   BUFFERCOUNT = { buffercount | @buffercount_variable } 
 | MAXTRANSFERSIZE = { maxtransfersize | @maxtransfersize_variable }

--Error Management Options
   { NO_CHECKSUM | CHECKSUM }
 | { STOP_ON_ERROR | CONTINUE_AFTER_ERROR }

--Compatibility Options
   RESTART 

--Monitoring Options
   STATS [ = percentage ] 

--Tape Options
   { REWIND | NOREWIND } 
 | { UNLOAD | NOUNLOAD } 

--Log-specific Options
   { NORECOVERY | STANDBY = undo_file_name }
 | NO_TRUNCATE
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
RESTORE DATABASE { database_name | @database_name_var } 
 [ FROM <backup_device> [ ,...n ] ]
 [ WITH 
   {
    [ RECOVERY | NORECOVERY | STANDBY = 
        {standby_file_name | @standby_file_name_var } 
       ]
   | ,  <general_WITH_options> [ ,...n ]
   | , <replication_WITH_option>
   | , <change_data_capture_WITH_option>
   | , <service_broker_WITH options> 
   | , <point_in_time_WITH_options—RESTORE_DATABASE> 
   } [ ,...n ]
 ]
[;]

--To perform the first step of the initial restore sequence
-- of a piecemeal restore:
RESTORE DATABASE { database_name | @database_name_var } 
   <files_or_filegroups> [ ,...n ]
 [ FROM <backup_device> [ ,...n ] ] 
   WITH 
      PARTIAL, NORECOVERY 
      [  , <general_WITH_options> [ ,...n ] 
       | , <point_in_time_WITH_options—RESTORE_DATABASE> 
      ] [ ,...n ] 
[;]

--To Restore Specific Files or Filegroups: 
RESTORE DATABASE { database_name | @database_name_var } 
   <file_or_filegroup> [ ,...n ]
 [ FROM <backup_device> [ ,...n ] ] 
   WITH 
   {
      [ RECOVERY | NORECOVERY ]
      [ , <general_WITH_options> [ ,...n ] ]
   } [ ,...n ] 
[;]

--To Restore Specific Pages: 
RESTORE DATABASE { database_name | @database_name_var } 
   PAGE = 'file:page [ ,...n ]' 
  [ , <file_or_filegroups> ] [ ,...n ]
 [ FROM <backup_device> [ ,...n ] ] 
   WITH 
       NORECOVERY   
      [ , <general_WITH_options> [ ,...n ] ]
[;]

--To Restore a Transaction Log:
RESTORE LOG { database_name | @database_name_var } 
 [ <file_or_filegroup_or_pages> [ ,...n ] ]
 [ FROM <backup_device> [ ,...n ] ] 
 [ WITH 
   {
     [ RECOVERY | NORECOVERY | STANDBY = 
        {standby_file_name | @standby_file_name_var } 
       ]
    | ,  <general_WITH_options> [ ,...n ]
    | , <replication_WITH_option>
    | , <point_in_time_WITH_options—RESTORE_LOG> 
   } [ ,...n ]
 ] 
[;]

--To Revert a Database to a Database Snapshot:   
RESTORE DATABASE { database_name | @database_name_var } 
FROM DATABASE_SNAPSHOT = database_snapshot_name 

<backup_device>::=
{ 
   { logical_backup_device_name |
            @logical_backup_device_name_var }
 | { DISK | TAPE } = { 'physical_backup_device_name' |
            @physical_backup_device_name_var } 
} 

<files_or_filegroups>::= 
{ 
   FILE = { logical_file_name_in_backup | @logical_file_name_in_backup_var } 
 | FILEGROUP = { logical_filegroup_name | @logical_filegroup_name_var } 
 | READ_WRITE_FILEGROUPS
} 

<general_WITH_options> [ ,...n ]::= 
--Restore Operation Options
   MOVE 'logical_file_name_in_backup' TO 'operating_system_file_name' 
          [ ,...n ] 
 | REPLACE 
 | RESTART 
 | RESTRICTED_USER 

--Backup Set Options
 | FILE = { backup_set_file_number | @backup_set_file_number } 
 | PASSWORD = { password | @password_variable } 

--Media Set Options
 | MEDIANAME = { media_name | @media_name_variable } 
 | MEDIAPASSWORD = { mediapassword | @mediapassword_variable } 
 | BLOCKSIZE = { blocksize | @blocksize_variable } 

--Data Transfer Options
 | BUFFERCOUNT = { buffercount | @buffercount_variable } 
 | MAXTRANSFERSIZE = { maxtransfersize | @maxtransfersize_variable }

--Error Management Options
 | { CHECKSUM | NO_CHECKSUM } 
 | { STOP_ON_ERROR | CONTINUE_AFTER_ERROR } 

--Monitoring Options
 | STATS [ = percentage ] 

--Tape Options
 | { REWIND | NOREWIND } 
 | { UNLOAD | NOUNLOAD } 

<replication_WITH_option>::=
 | KEEP_REPLICATION 

<change_data_capture_WITH_option>::=
 | KEEP_CDC

<service_broker_WITH_options>::= 
 | ENABLE_BROKER 
 | ERROR_BROKER_CONVERSATIONS 
 | NEW_BROKER 


<point_in_time_WITH_options—RESTORE_DATABASE>::= 
 | {
   STOPAT = { 'datetime' | @datetime_var } 
 | STOPATMARK = { 'lsn:lsn_number' }
                 [ AFTER 'datetime' ] 
 | STOPBEFOREMARK = {  'lsn:lsn_number' }
                 [ AFTER 'datetime' ] 
   } 

<point_in_time_WITH_options—RESTORE_LOG>::= 
 | {
   STOPAT = { 'datetime' | @datetime_var } 
 | STOPATMARK = { 'mark_name' | 'lsn:lsn_number' }
                 [ AFTER 'datetime' ] 
 | STOPBEFOREMARK = { 'mark_name' | 'lsn:lsn_number' }
                 [ AFTER 'datetime' ] 
   } 



你可能感兴趣的:(Sql Server备份与还原)