SQL Server 备份方案
参考
目的
在发生意外 (人为删除, 磁盘坏掉) 之后, 让数据可还原到指定时间点上.
Backup 的种类
备份分 3 种 Level
Full Backup (所有数据 copy)
Differential Backup (只 copy 从上一次 full backup 到目前的数据变化, 在还原过程中, differential backup 必须配合 full backup 才能工作)
Log Backup (只 copy 从上一次 differential backup 到目前的数据变化, 在还原过程中, log backup 必须配合 full backup 和 differential backup 才能工作)
执行流程
通常是:
每天 1 个 full backup, 半天 1 个 differential backup, 每小时 1 个 log backup
full backup -> log backup -> log backup -> differential backup -> log backup -> log backup -> full backup 大概是这样的
当需要还原的时候, 先还原到某一天的 full backup 然后依据时间继续还原 log backup 和 differential backup.
log backup 是可以指定时间的 (一小时内任意一个时间点上都可以还原到), 而 full 和 differential 只是记入数据所以没有时间的概念.
最糟糕的情况下可能会丢失最后 1 小时的数据 (Azure 的最小时间是每15 分钟 1 个 log backup, 一直 backup 是会影响性能的哦, 也是要衡量一下)
代码
USE [master] SELECT bs.database_name AS 'Database Name', bs.backup_start_date AS 'Backup Start', bs.backup_finish_date AS 'Backup Finished', DATEDIFF(MINUTE, bs.backup_start_date, bs.backup_finish_date) AS 'Duration (min)', bmf.physical_device_name AS 'Backup File', CASE WHEN bs.[type] = 'D' THEN 'Full Backup' WHEN bs.[type] = 'I' THEN 'Differential Database' WHEN bs.[type] = 'L' THEN 'Log' WHEN bs.[type] = 'F' THEN 'File/Filegroup' WHEN bs.[type] = 'G' THEN 'Differential File' WHEN bs.[type] = 'P' THEN 'Partial' WHEN bs.[type] = 'Q' THEN 'Differential partial' END AS 'Backup Type' FROM msdb.dbo.backupmediafamily bmf WITH(NOLOCK) INNER JOIN msdb..backupset bs WITH(NOLOCK) ON bmf.media_set_id = bs.media_set_id WHERE bs.database_name = 'test-recovery' ORDER BY bs.backup_start_date ASC; -- note for 下次看 -- backup 有 3 种, full, log, differential -- 通常是 full -> log -> log -> differential -> log -> log -> full -- 比如 1 天 1 个 full, 半天一个 differential, 每小时一个 log -- 备份的关键是, 1.恢复的时间(尽可能短) 2. 恢复的层度 (尽可能完整) -- express, web edition 不支持 backup compression -- norecovery 意思是还有下一个 restore 要跑, 最后一个 restore 才 recovery -- refer http://mysql.taobao.org/monthly/2017/11/03/ -- refer http://mysql.taobao.org/monthly/2017/12/05/ -- stats 是显示 progress % stats 10 = 完成 10% 就显示一下 -- init and replace 用于 full 的情况, 另外 2 个 用 noint 和不需要放 replace -- STOPAT 是只恢复到某事时间点停止 use [master]; use [test-recovery]; alter database [test-recovery] set recovery full with no_wait declare @now nvarchar(50) = REPLACE(convert(nvarchar(20),GetDate(),120),':','-'); set @now = '2018-09-02 18-16-27'; declare @type nvarchar(50) = 'full'; declare @path nvarchar(500) = 'C:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS\MSSQL\Backup\test-recovery_'+ @now +'_' + @type +'.bak'; --backup log [test-recovery] to disk = @path with noinit,stats=10; --backup database [test-recovery] to disk = @path with differential,init,stats=10; --backup database [test-recovery] to disk = @path with init,stats=10; restore database [test-recovery] from disk = @path with norecovery, replace; INSERT INTO Products(name) VALUES ('dada'); INSERT INTO Products(name) VALUES ('yyyy'); INSERT INTO Products(name) VALUES ('zz'); INSERT INTO Products(name) VALUES ('gg'); RESTORE DATABASE [test-recovery] FROM DISK =N'C:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS\MSSQL\Backup\test-recovery_2018_09_02-16_17_differential.bak' WITH NORECOVERY; RESTORE LOG [test-recovery] FROM DISK =N'C:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS\MSSQL\Backup\test-recovery_2018_09_02_16_18_log.bak' WITH RECOVERY; RESTORE LOG [test-recovery] FROM DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS\MSSQL\Backup\test-recovery_2018_09_02_16_18_log.bak' WITH STOPAT = '2017-12-17 23:04:46.130', RECOVERY
工具
上面的代码只是执行 backup 和 restore, 但正正要运行起来还需要一个 server task 每天/小时运行. 所以还是推荐上层工具.
虽然 SQL in Azure 也有 SQL Backup 方案, 但有点贵, 就先不碰了. 这款是免费的.
具体操作流程大概是:
1. select SQL Server and login > select database
2. Add destination 存放路径
3. set schedule
搞定
有一个需要注意的是, 默认配置下, full backup 以后 differential backup 就会被自动删除, differential backup 之后 log backup 就会自动删除
这是为了节省磁盘空间, 代价就是没办法还原到任意时间点 (这个代价也太大了吧). 通过 setting 我们可以修改掉这个默认设置.
Shrink log file
SQL server 会把所有操作记入在 database_log.ldf 里头, 这个 file 会越来越大. 而当我们做了上述的 backup 以后, 这个 file 就没啥用了. 我们可以定期去 shrink 它.
一般上是用 SQL Server Management Studio 来处理的.
参考:
How do you clear the SQL Server transaction log?
SQL Server Transaction Log Backup, Truncate and Shrink Operations