-
Notifications
You must be signed in to change notification settings - Fork 47
Update PMDB.Create_Database_Backup.sql #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
128 changes: 74 additions & 54 deletions
PMDB.Create_Database_Backup.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,98 @@ | ||
/*-------------------------------------------------------------------------------+ | ||
| Purpose: Create a backup of a database | ||
| Example: EXEC admin.Create_Database_Backup 'PMDB1_TEST' | ||
+--------------------------------------------------------------------------------*/ | ||
/*=======================================================================+ | ||
| Purpose : Create a backup of the specified database | ||
| Usage : EXEC admin.Create_Database_Backup @DatabaseName = 'PMDB1_TEST' | ||
| Author : Amit Patel (SQL DBA) | ||
| Date : [Insert Date] | ||
| Notes : Ensure the SQL Server Agent service account has access to | ||
| the backup folder path. Customize the path logic as needed. | ||
+========================================================================*/ | ||
|
||
-- SQLCMD mode variables (used if running from SSMS in SQLCMD mode) | ||
:setvar _server "Server1" | ||
:setvar _user "***username***" | ||
:setvar _password "***password***" | ||
:setvar _database "master" | ||
|
||
-- Connect to the instance | ||
:connect $(_server) -U $(_user) -P $(_password) | ||
|
||
USE [$(_database)]; | ||
GO | ||
|
||
CREATE PROCEDURE [admin].[Create_Database_Backup] | ||
IF NOT EXISTS ( | ||
SELECT 1 | ||
FROM sys.schemas | ||
WHERE name = 'admin' | ||
) | ||
BEGIN | ||
EXEC('CREATE SCHEMA admin'); | ||
END | ||
GO | ||
|
||
CREATE OR ALTER PROCEDURE [admin].[Create_Database_Backup] | ||
( | ||
@DatabaseName VARCHAR(50) | ||
@DatabaseName SYSNAME | ||
) | ||
AS | ||
AS | ||
BEGIN | ||
SET NOCOUNT ON; | ||
|
||
PRINT '=====================================================================' | ||
PRINT 'set the name of the database...' | ||
PRINT '=====================================================================' | ||
DECLARE @SourceDB VARCHAR(50) | ||
SET @SourceDB = @DatabaseName --DB_NAME() | ||
-- Initialize variables | ||
DECLARE | ||
@SourceDB SYSNAME = @DatabaseName, | ||
@BackupUser NVARCHAR(255), | ||
@DateStamp CHAR(20), | ||
@TargetPath NVARCHAR(500), | ||
@BackupFile NVARCHAR(500), | ||
@SQL NVARCHAR(MAX); | ||
|
||
PRINT '=====================================================================' | ||
PRINT 'get user name...' | ||
PRINT '=====================================================================' | ||
DECLARE @BackupUser VARCHAR(255) | ||
SET @BackupUser = (substring(suser_sname(),charindex('\',suser_sname())+(1),len(suser_sname())-charindex('\',suser_sname()))) | ||
PRINT '=====================================================================' | ||
PRINT 'Creating database backup for: ' + @SourceDB | ||
PRINT '=====================================================================' | ||
|
||
PRINT '=====================================================================' | ||
PRINT 'get current date and time...' | ||
PRINT '=====================================================================' | ||
DECLARE @DateStamp VARCHAR(20) | ||
SET @DateStamp = '_' + CONVERT(VARCHAR(20),GetDate(),112) + '_' + REPLACE(CONVERT(VARCHAR(20),GetDate(),108),':','') | ||
-- Extract backup operator's username | ||
SET @BackupUser = PARSENAME(REPLACE(SUSER_SNAME(), '\', '.'), 1); | ||
|
||
PRINT '=====================================================================' | ||
PRINT 'set database backup path...' | ||
PRINT '=====================================================================' | ||
DECLARE @TargetPath VARCHAR(255) | ||
-- TO DO: Standardize the backup folder location for all servers | ||
IF @@SERVERNAME = 'Server1' SET @TargetPath = 'C:\Temp\' | ||
-- Generate timestamp | ||
SET @DateStamp = FORMAT(GETDATE(), 'yyyyMMdd_HHmmss'); | ||
|
||
PRINT '=====================================================================' | ||
PRINT 'set the backup file name...' | ||
PRINT '=====================================================================' | ||
SET @TargetPath = @TargetPath + @SourceDB + @DateStamp + '_' + @BackupUser + '.bak''' | ||
PRINT @TargetPath | ||
-- Set default backup path based on server name | ||
-- TODO: Standardize across environments | ||
IF @@SERVERNAME = 'Server1' | ||
SET @TargetPath = 'C:\Temp\'; | ||
ELSE | ||
SET @TargetPath = 'C:\Backups\'; -- fallback/default | ||
|
||
PRINT '=====================================================================' | ||
PRINT 'backup the database...' | ||
PRINT '=====================================================================' | ||
IF EXISTS(SELECT NAME FROM sys.databases where name = @SourceDB) | ||
BEGIN | ||
DECLARE @BACKUP_SQL VARCHAR(MAX) | ||
SET @BACKUP_SQL = | ||
'BACKUP DATABASE ' + @SourceDB + ' | ||
TO DISK = ''' + @TargetPath + ' | ||
WITH FORMAT, | ||
MEDIANAME = ''' + @BackupUser + ''', | ||
NAME = ''' + @SourceDB + @DateStamp + '''' | ||
-- Compose full backup file path | ||
SET @BackupFile = CONCAT(@TargetPath, @SourceDB, '_', @DateStamp, '_', @BackupUser, '.bak'); | ||
|
||
PRINT @BACKUP_SQL | ||
EXEC (@BACKUP_SQL) | ||
END | ||
PRINT '=====================================================================' | ||
PRINT 'Finished!' | ||
PRINT '=====================================================================' | ||
PRINT 'Backup file path: ' + @BackupFile; | ||
|
||
END | ||
-- Verify database existence | ||
IF EXISTS (SELECT 1 FROM sys.databases WHERE name = @SourceDB) | ||
BEGIN | ||
SET @SQL = ' | ||
BACKUP DATABASE [' + @SourceDB + '] | ||
TO DISK = N''' + @BackupFile + ''' | ||
WITH FORMAT, | ||
INIT, | ||
NAME = N''' + @SourceDB + '_Full_Backup_' + @DateStamp + ''', | ||
MEDIANAME = N''' + @BackupUser + ''', | ||
STATS = 10; | ||
'; | ||
|
||
PRINT 'Executing backup command...'; | ||
PRINT @SQL; | ||
EXEC (@SQL); | ||
PRINT 'Backup completed successfully.'; | ||
END | ||
ELSE | ||
BEGIN | ||
RAISERROR('Database "%s" does not exist.', 16, 1, @SourceDB); | ||
END | ||
|
||
PRINT '=====================================================================' | ||
PRINT 'Backup procedure finished.' | ||
PRINT '=====================================================================' | ||
END | ||
GO | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.