【Vegas原创】SQL Server数据库备份、差异备份、日志备份脚本

2年前 (2022) 程序员胖胖胖虎阿
252 0 0

1,sp脚本

USE [master]
GO
/****** Object:  StoredProcedure [dbo].[sp_BackupDatabase]    Script Date: 2021/10/22 10:04:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- 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'C:\Database_BackUp\Full\'
EXECUTE master.dbo.xp_create_subdir N'C:\Database_BackUp\Difference\'
EXECUTE master.dbo.xp_create_subdir N'C:\Database_BackUp\Log_Bak\'

IF @backupType = 'F'
set @filepath_backup='C:\Database_BackUp\Full\'
IF @backupType = 'D'
set @filepath_backup='C:\Database_BackUp\Difference\'
IF @backupType = 'L'
set @filepath_backup='C:\Database_BackUp\Log_Bak\'

SET ANSI_WARNINGS OFF
SET @dateTime = replace(convert(varchar,current_timestamp, 112)+'_'+convert(varchar,current_timestamp, 108),':','')

----删除超过14天的备份文件
DECLARE @delete_time datetime
set @delete_time = getdate() -14

EXECUTE master.dbo.xp_delete_file 0,N'C:\Database_BackUp',N'trn',@delete_time,1
EXECUTE master.dbo.xp_delete_file 0,N'C:\Database_BackUp',N'bak',@delete_time,1

SELECT @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 ('OperationSystem')  ----填写需要备份的数据库

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:SIMPLE

IF @backupType = 'F' 
begin
    SET @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')
begin
   SET @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')
begin
    SET @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 @sqlCommand

EXECUTE sp_executesql @sqlCommand 

set @destionationPath = REPLACE(@sourcePath,'C:\Database_BackUp','x:')
/*
exec sp_configure 'show advanced options', 1    --允许配置高级选项
reconfigure    --重新配置
exec sp_configure 'xp_cmdshell', 1    --启用xp_cmdshell
reconfigure    --重新配置
--配置共享路径用户名和密码
exec master..xp_cmdshell 'net use x: \\Server-QC-DB2.zt.com\Database_BackUp P@ssw0rd  /user:dbbackuper'
--exec sp_configure 'xp_cmdshell', 0    --执行完成后出于安全考虑可以将xp_cmdshell关闭
*/
set @cmdStr = 'exec master..xp_cmdshell ''copy '+@sourcePath+' '+@destionationPath+''''
EXEC(@cmdStr)

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任务,跑一下日志的收缩

USE OperationSystem;

GO 

ALTER DATABASE OperationSystem
SET RECOVERY SIMPLE;  --设置简单恢复模式

GO

DBCC SHRINKFILE (OperationSystem_log, 1);

GO

ALTER DATABASE OperationSystem
SET RECOVERY FULL;   --恢复为原模式

GO

相关文章

暂无评论

暂无评论...