一、备份还原的概念
1、 完整备份:完整备份因为需要备份的数据量大,所以需要在空闲时间进行,并且定期进行。
2、 日志备份:日志备份的数据量小,备份时间为上一次备份到本次本分期间的数据,每天都可以进行备份,或者每小时都可以进行备份,据所需备份。
3、 差异备份:只备份修改过的数据,与每小时进行的日志备份配合使用,效率更高。
二、备份设备
1、 在进行备份数据的保存时,需要输入的文件路径很长,并且每次都要输入,难免麻烦费事,所以我们先给文件路径取好别名,即备份设备,以便之后书写代码的方便。
2、 创建备份设备
IF EXISTS (SELECT 1 FROM sys.backup_devices AS BD WHERE BD.name = ‘dp_EduBase_FullBackup‘) EXEC sp_dropdevice ‘dp_EduBase_FullBackup‘; EXEC sp_addumpdevice ‘DISK‘,‘dp_EduBase_FullBackup‘,‘C:\EduBase\Backup\Full\dp_EduBase_FullBackup.bak‘; IF EXISTS (SELECT 1 FROM sys.backup_devices AS BD WHERE BD.name = ‘dp_EduBase_DiffBackup‘) EXEC sp_dropdevice ‘dp_EduBase_DiffBackup‘; EXEC sp_addumpdevice ‘DISK‘,‘dp_EduBase_DiffBackup‘,‘C:\EduBase\Backup\Diff\dp_EduBase_DiffBackup.bak‘; IF EXISTS (SELECT 1 FROM sys.backup_devices AS BD WHERE BD.name = ‘dp_EduBase_LogBackup‘) EXEC sp_dropdevice ‘dp_EduBase_LogBackup‘; EXEC sp_addumpdevice ‘DISK‘,‘dp_EduBase_LogBackup‘,‘C:\EduBase\Backup\Log\dp_EduBase_LogBackup.bak‘;
1、 做完整备份
USE EduBase; DECLARE @FullBkDesc VARCHAR(MAX); //申明备份设备 SELECT @FullBkDesc = ‘Weekly full backup for ‘+DB_NAME()+‘ at ‘+DATENAME(YEAR,GETDATE())+‘,week ‘+DATENAME(WEEK,GETDATE())+‘.(‘+CONVERT(VARCHAR,GETDATE(),120)+‘)‘; BACKUP DATABASE EduBase TO dp_EduBase_FullBackup WITH INIT //INIT操作可以初始化文件,即可以覆盖掉之前的备份,保留本次操作过后的备份 ,Name = ‘EduBase_FullBackup‘ ,DESCRIPTION = @FullBkDesc;
2、 做日志备份
DECLARE @LogBkDesc VARCHAR(MAX); SELECT @LogBkDesc = ‘Hourly log backup for ‘+DB_NAME()+‘ at ‘+DATENAME(YEAR,GETDATE())+‘,week ‘+DATENAME(WEEK,GETDATE())+‘,day ‘+CONVERT(VARCHAR,DATEPART(W,GETDATE())-1)+‘,hour ‘+CONVERT(VARCHAR,DATEPART(HOUR,GETDATE()))+‘.(‘+CONVERT(VARCHAR,GETDATE(),120)+‘)‘; BACKUP LOG EduBase TO dp_EduBase_LogBackup WITH INIT ,Name = ‘EduBase_LogBackup‘ ,DESCRIPTION = @LogBkDesc;
3、做差异备份
DECLARE @DiffBkDesc VARCHAR(MAX); SELECT @DiffBkDesc = ‘Daily Differential backup for ‘+DB_NAME()+‘ at ‘+DATENAME(YEAR,GETDATE())+‘,week ‘+DATENAME(WEEK,GETDATE())+‘,day ‘+CONVERT(VARCHAR,DATEPART(W,GETDATE())-1)+‘.(‘+CONVERT(VARCHAR,GETDATE(),120)+‘)‘; BACKUP DATABASE EduBase TO dp_EduBase_DiffBackup WITH DIFFERENTIAL ,INIT ,Name = ‘EduBase_DiffBackup‘