1,sp脚本
USE [master]
GO
/****** Object: StoredProcedure [dbo].[sp_BackupDatabase] Script Date: 2025/1/8 10:43:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO-- Author: Amadeus-- Create date: 2021-10-20 exec sp_BackupDatabase L-- Description: 备份数据库,备份路径C:\Database_BackUp\ 可自行修改-- Parameter1: 备份类型 F=全部, D=差异, L=日志ALTER PROCEDURE [dbo].[sp_BackupDatabase]
@backupType CHAR(1)
AS
BEGIN
SET NOCOUNT ON;declare @filepath_backup varchar(100)
declare @dateTime varchar(30),@del_time_stamp varchar(50)
DECLARE @sqlCommand NVARCHAR(1000)
DECLARE @sourcePath nvarchar(max)
DECLARE @destionationPath nvarchar(max)
DECLARE @cmdStr nvarchar(max) -- 创建数据库对应文件夹
EXECUTE master.dbo.xp_create_subdir N'D:\Database_BackUp\Full\'
EXECUTE master.dbo.xp_create_subdir N'D:\Database_BackUp\Difference\'
EXECUTE master.dbo.xp_create_subdir N'D:\Database_BackUp\Log_Bak\'IF @backupType = 'F'
set @filepath_backup='D:\Database_BackUp\Full\'
IF @backupType = 'D'
set @filepath_backup='D:\Database_BackUp\Difference\'
IF @backupType = 'L'
set @filepath_backup='D:\Database_BackUp\Log_Bak\'SET ANSI_WARNINGS OFF
SET @dateTime = replace(convert(varchar,current_timestamp, 112)+'_'+convert(varchar,current_timestamp, 108),':','')----删除超过30天的备份文件
DECLARE @delete_time datetime
set @delete_time = getdate() -30EXECUTE master.dbo.xp_delete_file 0,N'D:\Database_BackUp',N'trn',@delete_time,1
EXECUTE master.dbo.xp_delete_file 0,N'D:\Database_BackUp',N'bak',@delete_time,1SELECT @dateTime = replace(convert(varchar,current_timestamp, 112)+'_'+convert(varchar,current_timestamp, 108),':','')declare db_info cursor for
SELECT NAME,recovery_model FROM MASTER.SYS.databases
where state = 0 ---只处理online的数据库
and name in ('Test') ----填写需要备份的数据库declare @databaseName nvarchar(128)
declare @recovery_model int
OPEN db_info
fetch next from db_info into @databaseName,@recovery_model
while @@fetch_status=0
Begin ---recovery_model 1 : FULL 2 : BULK_LOGGED 3:SIMPLEIF @backupType = 'F'
beginSET @sqlCommand = 'BACKUP DATABASE '+ @databaseName +' TO DISK = '''+ @filepath_backup + ''+ @databaseName +'_Full_'+@dateTime+'.BAK'' with STATS = 10, INIT, COMPRESSION, CHECKSUM 'set @sourcePath = @filepath_backup + ''+ @databaseName +'_Full_'+@dateTime+'.BAK'
end
IF @backupType = 'D' and @databaseName not in ('master','msdb','model')
beginSET @sqlCommand = 'BACKUP DATABASE '+ @databaseName +' TO DISK = '''+ @filepath_backup + ''+ @databaseName + '_Diff_' + @dateTime + '.BAK '' WITH DIFFERENTIAL, STATS = 10, INIT, COMPRESSION'set @sourcePath = @filepath_backup + ''+ @databaseName +'_Diff_'+@dateTime+'.BAK'
end
IF @backupType = 'L' and @recovery_model <> 3 and @databaseName not in ('master','msdb','model')
beginSET @sqlCommand = 'BACKUP LOG '+ @databaseName +' TO DISK = '''+ @filepath_backup + '' + @databaseName +'_Log_' + @dateTime + '.TRN'' with STATS = 10, INIT, COMPRESSION'set @sourcePath = @filepath_backup + ''+ @databaseName +'_Log_'+@dateTime+'.TRN'
end
print @sqlCommandEXECUTE sp_executesql @sqlCommand fetch next from db_info into @databaseName,@recovery_model
End close db_info
deallocate db_info PRINT '-- Backup completed successfully at '+convert(varchar, getdate(), 120) SET ANSI_WARNINGS ON
END
2,SQL agent任务指令:
[dbo].[sp_BackupDatabase] 'F'
3,别忘了每周agent任务,跑一下数据库日志的收缩,例:test数据库
USE test;
GO
ALTER DATABASE test
SET RECOVERY SIMPLE; --设置简单恢复模式
GODBCC SHRINKFILE (test_log, 1);
GOALTER DATABASE test
SET RECOVERY FULL; --恢复为原模式
GO