星空网 > 软件开发 > 数据库

backup1:开始数据库备份

数据库备份分为数据文件备份和日志文件备份,数据文件的备份分为:完整备份和差异备份。在SQL Server 2012中,能够将数据分布式备份到不同的存储设备上,一般情况,只将数据备份到一个备份文件(.bak)中,只有在备份超大的数据库时,才需要分布式备份。

数据库备份的策略一般是:一周一次完整备份,一天一次差异备份,一小时一次事务日志备份,根据数据容灾的要求,适当增减备份的时间间隔。

为了便于管理数据备份文件,推荐的做法是:

  • 数据/日志的每次备份都使用一个单独的备份文件,数据备份的扩展名是 .bak,日志备份的扩展名是.trn;
  • 合理命名每个备份文件,建议使用:database_name+date+time+(.bak/.trn),该命名方式,很容易识别备份的数据库和开始备份的时间;
  • 创建schedule,定时清理备份文件,避免备份文件耗尽磁盘空间;

一,创建数据库的完整备份和差异备份

使用backup database命令创建数据库的数据文件的备份,backup database 命令语法(简化):

BACKUP DATABASE database_name TO DISK = 'physical_device_name'[ WITH { DIFFERENTIAL| COPY_ONLY | { COMPRESSION | NO_COMPRESSION } | { NOINIT | INIT } | { NOSKIP | SKIP } | { NOFORMAT | FORMAT } | STATS [ = percentage ] }]

1,完整备份和差异备份

差异备份由DIFFERENTIAL 关键字指定,只备份从上一次完整备份之后发生更新的数据,而不是备份整个数据库,通常情况下,差异备份比完整备份占用的空间更少。差异备份的参考基准是上一次完整备份,而,事务日志,只备份是从上一次差异备份之后产生的事务日志。因此,备份是有顺序的,如果存在以下备份序列:

  1. FullBackup1.bak
  2. DifferentialBackup2.bak
  3. LogBackup3.trn
  4. DifferentialBackup4.bak
  5. LogBackup5.trn
  6. 出现错误

还原的策略是:备份尾日志,使数据库处于Restoring状态,依次还原FullBackup1.bak,DifferentialBackup4.bak,LogBackup5.trn,尾日志,就能将数据库还原到一个合适的有效时间点。

在执行完整备份和差异备份时,SQL Server会备份足够的事务日志,用于将数据库还原到一致性的状态。对于master数据库,只能执行完整备份。

  • During a full or differential database backup, SQL Server backs up enough of the transaction log to produce a consistent database when the backup is restored.
  • Only a full database backup can be performed on the master database.

2,只复制(COPY_ONLY )备份

备份是有顺序的,使用COPY_ONLY选项不会影响备份的正常顺序,仅仅创建一个数据库的副本。

差异备份的基准是上一次完整备份,即差异是指从上一次full backup之后,对数据文件执行的更新操作。如果执行一次Copy-Only的完整数据库备份,不会影响差异备份的base(基准),该base是上一次full backup,而非本次 Copy-only full backup。

3,压缩数据{ COMPRESSION | NO_COMPRESSION }

在备份时,将数据压缩,由于压缩的备份较小,能够减少Disk Sapce和Disk IO消耗,提高数据备份的速度,但是,备份文件的压缩和解压缩十分消耗CPU资源。

4,建议:每一次数据备份,都存储在单个备份文件上

由于硬盘空间有限,不可能保留过多的备份文件,将数据的每一次备份都存储在单个文件上,便于对备份文件进行管理(删除或归档)。

每次备份都存储在新的备份上,搭配选项 Init、Skip、Format,将数据备份存储在新的备份文件上,这三个选项的含义是:

  • Format 选项:将备份文件格式化,默认选项是 NoFormat;
  • Init 选项:初始化备份文件,Init选项不会初始化Media Header,只将backup set初始化,默认选项是NoInit,将备份存储到备份文件的末尾;
  • SKIP 选项:不做任何检查,不会检查Media Header是否有效,也不会检查backup set的有效期,默认选项是NoSkip;

5,备份进度(stats)

使用stats选项,每当备份进行到一定的百分比时,SQL Server显式进度消息,默认值是10,即,每完成10%,SQL Server显式完成的进度消息,例如,设置stats=10,当备份进程完成30%时,SQL Server会打印消息:30 percent processed.

The STATS option reports the percentage complete as of the threshold for reporting the next interval. This is at approximately the specified percentage; for example, with STATS=10, if the amount completed is 40 percent, the option might display 43 percent. For large backup sets, this is not a problem, because the percentage complete moves very slowly between completed I/O calls.

二,数据备份操作

建议:每一次数据备份,都存储在单个备份文件上

1,数据库完整备份,没有指定Differential选项

backup database [TestSite]to disk = 'D:\TestDBBackupFolder\Sitedb_bak1.bak' --specify new backup filewithcompression,format,init,skip,stats=5

2,数据库差异备份,指定Differential选项

backup database [TestSite]to disk = 'D:\TestDBBackupFolder\Sitedb_bak2.bak' --specify new backup filewith differential,compression, format, init, skip, stats=5

三,事务日志备份

使用backup log命令对事务日志进行备份,跟backup database命令的差异是,不能使用differential选项,多了NoRecovery 和 NO_Truncate选项;

BACKUP LOG database_name TO DISK = 'physical_device_name'[ WITH {  COPY_ONLY| { COMPRESSION | NO_COMPRESSION } | { NOINIT | INIT } | { NOSKIP | SKIP } | { NOFORMAT | FORMAT } | STATS [ = percentage ] | { NORECOVERY | STANDBY = undo_file_name }| NO_TRUNCATE }]

1,尾日志备份

NORECOVERY 选项,指定备份事务日志的尾部,并使数据库处于RESTORING状态

Backs up the tail of the log and leaves the database in the RESTORING state. NORECOVERY is useful when failing over to a secondary database or when saving the tail of the log before a RESTORE operation. To perform a best-effort log backup that skips log truncation and then take the database into the RESTORING state atomically, use the NO_TRUNCATE and NORECOVERY options together.

2,日志截断

正常情况下,数据库处于Online状态,在进行事务日志备份时,如果不指定 NO_TRUNCATE 选项,那么数据库将已备份的事务日志文件截断,避免事务日志过大,耗尽disk空间;如果指定 NO_TRUNCATE 选项,表示日志备份不会将事务日志文件截断,该选项一般在数据库处于异常状态时使用。

Specifies that the log not be truncated and causes the Database Engine to attempt the backup regardless of the state of the database. Consequently, a backup taken with NO_TRUNCATE might have incomplete metadata. This option allows backing up the log in situations where the database is damaged.
The NO_TRUNCATE option of BACKUP LOG is equivalent to specifying both COPY_ONLY and CONTINUE_AFTER_ERROR.
Without the NO_TRUNCATE option, the database must be in the ONLINE state. If the database is in the SUSPENDED state, you might be able to create a backup by specifying NO_TRUNCATE. But if the database is in the OFFLINE or EMERGENCY state, BACKUP is not allowed even with NO_TRUNCATE. 

四,事务日志备份

1,正常情况下的事务日志备份

backup log [TestSite]to disk = 'D:\TestDBBackupFolder\Sitedb_bak3.trn'withcompression,format,init,skip,stats=5

2,备份尾日志,进而还原数据库

backup log [TestSite]to disk = 'D:\TestDBBackupFolder\Sitedb_bak4.trn'withcompression,format,init,skip,stats=5,norecovery

 

参考doc:

BACKUP (Transact-SQL)




原标题:backup1:开始数据库备份

关键词:数据库

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流