74 lines
3.4 KiB
MySQL
74 lines
3.4 KiB
MySQL
|
USE msdb;
|
|||
|
GO
|
|||
|
|
|||
|
CREATE PROCEDURE dbo.P_BACKUP
|
|||
|
@MODE CHAR(1), -- L : LOG, C : COMPLETE, D : DIFFERENTIAL
|
|||
|
@PATH NVARCHAR(256),
|
|||
|
@DATETIME BIT = 1,
|
|||
|
@RETAINSDAYS TINYINT
|
|||
|
AS
|
|||
|
/******************************************************************************
|
|||
|
* NATURE : PROCEDURE *
|
|||
|
* OBJECT : msdb.dbo.P_BACKUP *
|
|||
|
* CREATE : 2020-06-26 *
|
|||
|
* VERSION : 1 *
|
|||
|
* SYSTEM :NON *
|
|||
|
*******************************************************************************
|
|||
|
* Fr<EFBFBD>d<EFBFBD>ric BROUARD - alias SQLpro - SARL SQL SPOT - SQLpro@sqlspot.com *
|
|||
|
* Architecte de donn<EFBFBD>es : expertise, audit, conseil, formation, mod<EFBFBD>lisation *
|
|||
|
* tuning, sur les SGBD Relationnels, le langage SQL, MS SQL Server/PostGreSQL *
|
|||
|
* blog: http://blog.developpez.com/sqlpro site: http://sqlpro.developpez.com *
|
|||
|
*******************************************************************************
|
|||
|
* PURPOSE : sauvegarde de toutes les bases d'une instance *
|
|||
|
*******************************************************************************
|
|||
|
* INPUTS : *
|
|||
|
* @MODE CHAR(1), L : LOG, C : COMPLETE, D : DIFFERENTIAL *
|
|||
|
* @PATH NVARCHAR(256), chemin vers le r<EFBFBD>pertoire de sauvegarde *
|
|||
|
* @DATETIME BIT = 1, ajoute ou non la date heure dans le nom du fichier *
|
|||
|
* de sauvegarde *
|
|||
|
* @RETAINSDAYS TINYINT si pas de dateheure dans le fichier de sauvegarde *
|
|||
|
* alors cumule les sauvegarde mais ne retient que les *
|
|||
|
* n dernier jours *
|
|||
|
*******************************************************************************
|
|||
|
* EXEMPLE : *
|
|||
|
* EXEC dbo.P_BACKUP 'C', 'C:\DATABASE\BACKUP', 1, NULL; *
|
|||
|
******************************************************************************/
|
|||
|
SET NOCOUNT ON;
|
|||
|
SET @MODE = UPPER(@MODE);
|
|||
|
IF RIGHT(@PATH, 1) <> '\'
|
|||
|
SET @PATH = @PATH + '\';
|
|||
|
DECLARE @SQL NVARCHAR(max) = N'';
|
|||
|
SELECT @SQL = @SQL + N'BACKUP ' +
|
|||
|
CASE @MODE
|
|||
|
WHEN N'L' THEN N'LOG '
|
|||
|
ELSE N'DATABASE '
|
|||
|
END + N'[' + name + '] TO DISK = ''' + @PATH + name +
|
|||
|
CASE @DATETIME
|
|||
|
WHEN 1 THEN N'_' + CONVERT(NVARCHAR(32), SYSDATETIME(), 121)
|
|||
|
ELSE ''
|
|||
|
END +
|
|||
|
CASE @MODE
|
|||
|
WHEN N'L' THEN N'.TRN'
|
|||
|
ELSE N'.BAK'
|
|||
|
END + '''' +
|
|||
|
CASE WHEN @RETAINSDAYS IS NOT NULL AND @DATETIME = 0
|
|||
|
THEN N' WITH RETAINDAYS = ' + CAST(@RETAINSDAYS AS VARCHAR(16))
|
|||
|
ELSE N''
|
|||
|
END + N';'
|
|||
|
FROM sys.databases
|
|||
|
WHERE state = 0
|
|||
|
AND source_database_id IS NULL
|
|||
|
AND name NOT in ('model', 'tempdb')
|
|||
|
AND 1 = CASE WHEN @MODE = 'L' AND recovery_model = 3 THEN 0
|
|||
|
ELSE 1 END;
|
|||
|
GO
|
|||
|
|
|||
|
--> planif de la sauvegarde FULL tous les jours
|
|||
|
EXEC dbo.P_BACKUP 'C', 'C:\DATABASE\BACKUP', 1, NULL;
|
|||
|
|
|||
|
|
|||
|
--> planif de la sauvegarde TRAN toutes les 30 minutes
|
|||
|
EXEC dbo.P_BACKUP 'L', 'C:\DATABASE\BACKUP', 1, NULL;
|
|||
|
|
|||
|
|