一键备份 MS server 主机中所有数据库
USE master;
GO
CREATE OR ALTER PROCEDURE dbo.BackupAllDatabases
@BackupDirectory NVARCHAR(255)
AS
BEGIN
-- 获取当前日期和时间
DECLARE @CurrentDate VARCHAR(8) = CONVERT(VARCHAR(8), GETDATE(), 112);
DECLARE @CurrentDateTime VARCHAR(14) = REPLACE(CONVERT(VARCHAR(14), GETDATE(), 120), ':', '');
-- 创建临时表来存储备份文件的详细信息
CREATE TABLE #BackupFiles
(
DatabaseName NVARCHAR(128),
BackupFilePath NVARCHAR(255)
);
-- 查询所有用户数据库
INSERT INTO #BackupFiles
SELECT name, @BackupDirectory + '\' + name + '_' + @CurrentDateTime + '.bak'
FROM sys.databases
WHERE name NOT IN ('master', 'tempdb', 'model', 'msdb')
AND state = 0; -- 只备份处于在线状态的数据库
-- 执行备份
DECLARE @DatabaseName NVARCHAR(128);
DECLARE @BackupFilePath NVARCHAR(255);
DECLARE @SQL NVARCHAR(MAX);
DECLARE db_cursor CURSOR FOR
SELECT DatabaseName, BackupFilePath
FROM #BackupFiles;
OPEN db_cursor;
FETCH NEXT FROM db_cursor INTO @DatabaseName, @BackupFilePath;
WHILE @@FETCH_STATUS = 0
BEGIN
SET @SQL = 'BACKUP DATABASE ' + QUOTENAME(@DatabaseName) + ' TO DISK = N''' + @BackupFilePath + '''';
EXEC sp_executesql @SQL;
FETCH NEXT FROM db_cursor INTO @DatabaseName, @BackupFilePath;
END;
CLOSE db_cursor;
DEALLOCATE db_cursor;
-- 删除超过30天的备份文件
DECLARE @DateThreshold VARCHAR(8) = CONVERT(VARCHAR(8), DATEADD(DAY, -30, GETDATE()), 112);
DECLARE @DeleteSQL NVARCHAR(MAX) = 'EXEC xp_cmdshell ''DEL "' + @BackupDirectory + '\*.bak" /F /Q /S''';
SET @DeleteSQL = REPLACE(@DeleteSQL, '*', @DateThreshold);
EXEC sp_executesql @DeleteSQL;
-- 删除临时表
DROP TABLE #BackupFiles;
END;
-- EXEC dbo.BackupAllDatabases 'C:\BackupDirectory';
以下备注:
【错误描述】:
SQL Server阻止了对组件‘xp_cmdshell’的过程‘sys.xp_cmdshell’的访问。因为此组件已作为此服务嚣安全配置的一部分而被关闭。系统管理员可以通过使用sp_configure启用‘xp_cmdshell’。有关启用‘xp_cmdshell’的详细信息,请参阅sQL帮助文件。
【原因分析】:
xp_cmdshell可以让系统管理员以操作系统命令行解释器的方式执行给定的命令字符串,并以文本行方式返回任何输出,是一个功能非常强大的扩展存贮过程。一般情况下,xp_cmdshell对管理员来说也是不必要的,xp_cmdshell的消除不会对Server造成任何影响。
【解决办法】:
通过查询分析器,选择Master数据库,然后执行以下SQL内容:
sp_configure 'show advanced options',1
reconfigure
go
sp_configure 'xp_cmdshell',1
reconfigure
go
Oracle 七天一个全量每天一个增量备份
$backupFolder = '/path/to/backup/'
$oneWeekAgo = (Get-Date).AddDays(-7)
# 获取一周前的日期
$oneWeekAgoDate = Get-Date $oneWeekAgo -Format 'yyyy-MM-dd'
# 执行 RMAN 备份命令
$RMANScript = @"
RUN {
ALLOCATE CHANNEL c1 DEVICE TYPE DISK;
SQL 'CREATE TABLESPACE RMAN_TEMP';
SQL 'ALTER SESSION SET "_oracle_script"=true';
SQL 'SELECT name INTO :DBNAME FROM v$database';
# 每周一次的一周增量备份
IF TRUNC(SYSDATE, 'IW') = TRUNC(SYSDATE) THEN
BACKUP INCREMENTAL LEVEL 1 DATABASE FORMAT '$backupFolder/:DBNAME_%d-%m-%Y_%H.bak';
END IF;
# 每天的增量备份
BACKUP INCREMENTAL LEVEL 1 DATABASE FORMAT '$backupFolder/:DBNAME_%d-%m-%Y_%H.bak';
# 删除一周前的备份
DELETE NOPROMPT OBSOLETE RECOVERY WINDOW OF 7 DAYS;
SQL 'DROP TABLESPACE RMAN_TEMP INCLUDING CONTENTS AND DATAFILES';
RELEASE CHANNEL c1;
}
"@
# 执行 RMAN 备份命令
Start-Process -FilePath rman.exe -ArgumentList "-run", $RMANScript
# 获取符合条件的备份文件
$filesToDelete = Get-ChildItem -Path $backupFolder | Where-Object { $_.Name -like "*_$oneWeekAgoDate_*" }
# 循环删除符合条件的备份文件
foreach ($file in $filesToDelete) {
Remove-Item -Path $file.FullName -Force
}
TRANSLATE with x English TRANSLATE with COPY THE URL BELOW Back EMBED THE SNIPPET BELOW IN YOUR SITE Enable collaborative features and customize widget: Bing Webmaster Portal Back 标签:BackUpLogView,cmdshell,--,备份,DECLARE,SQL,xp,数据库 From: https://www.cnblogs.com/dentist/p/17521431.html