MS SQL Backup and Restore Procedures
Continuing on my SQL search script posted yesterday, I am posting two stored procedures for backing up and restoring Microsoft SQL databases easily.
I use the backup script frequently when publishing or making large changes to databases. Obviously, this script should really be used in addition to a scheduled maintenance and backup plan and is not intended to replace that.
Another thing to keep in mind is that these two procedures just abstract calling the built in backup and restore commands. I think this syntax is easier to remember and the hard coded options are right for what I tend to do. Feel free to tweak them as needed. I tend to create these procedures in my master database.
Here’s the SQL for BackupDatabase.
CREATE PROCEDURE BackupDatabase @database varchar(100), @backupDir varchar(500) AS IF RIGHT(@backupDir, 1) = '' SET @backupDir = @backupDir + @database + '.bak' ELSE SET @backupDir = @backupDir + '' + @database + '.bak' BACKUP DATABASE @database TO DISK = @backupDir WITH STATS = 10
The BackupDatabase procedure accepts to parameters, the name of the database to backup and the path to the directory where the backup file will be written. The following is an example of how to call the procedure.
master..BackupDatabase 'Northwind', 'd:temp'
This example will backup the Northwind database to the d:temp directory, creating a file named Northwind.bak. One thing I might suggest doing is altering the procedure to append the current date to the end of the file name.
The RestoreDatabase procedure depends on a helper procedure which returns the files which are contained within a backup file. You can call this procedure directly, but there’s really not much point to this. Execute the following SQL to create the procedure.
CREATE PROCEDURE GetBackupFileList @backupFile varchar(500) AS RESTORE FILELISTONLY FROM DISK = @backupFile
Once you have created the GetBackupFileList procedure the RestoreDatabase procedure can be created by executing this SQL code:
CREATE PROCEDURE RestoreDatabase @backupFile varchar(500), @newDatabaseName varchar(100) AS -- // create a temp table to hold the file list for the db CREATE TABLE #fileList ( LogicalName nvarchar(128), PhysicalName nvarchar(260), Type char(1), FileGroupName nvarchar(128), Size numeric(20,0), MaxSize numeric(20,0) ) -- // Declare a variable to hold the restore statement DECLARE @RestoreStmt varchar(500) -- // start the restore stmnt SET @RestoreStmt = ' RESTORE DATABASE ' + @newDatabaseName + ' FROM DISK = '' + @backupFile + '' WITH ' INSERT #fileList EXECUTE GetBackupFileList @backupFile --- // declare some vars to hold the filenames and types DECLARE @LogicalName nvarchar(128) DECLARE @PhysicalName nvarchar(260) DECLARE @Type char(1) DECLARE @LastSlash int DECLARE fileListCsr CURSOR FOR SELECT LogicalName, PhysicalName, Type FROM #fileList OPEN fileListCsr FETCH NEXT FROM fileListCsr INTO @LogicalName, @PhysicalName, @Type WHILE @@FETCH_STATUS = 0 BEGIN -- // get the path from @PhysicalName SET @LastSlash = CHARINDEX('', REVERSE(@PhysicalName)) SET @PhysicalName = LEFT(@PhysicalName, LEN(@PhysicalName) - @LastSlash) + '' -- // check the type of this file IF @Type = 'D' SET @PhysicalName = @PhysicalName + @newDatabaseName + '_data.mdf' ELSE SET @PhysicalName = @PhysicalName + @newDatabaseName + '_log.ldf' -- // update the file paths SET @RestoreStmt = @RestoreStmt + ' MOVE '' + @LogicalName + '' TO '' + @PhysicalName + '', ' FETCH NEXT FROM fileListCsr INTO @LogicalName, @PhysicalName, @Type END -- // add stats SET @RestoreStmt = @RestoreStmt + ' STATS = 10' PRINT @RestoreStmt CLOSE fileListCsr DROP TABLE #fileList -- // execute the restore stmnt EXEC( @RestoreStmt )
This RestoreDatabase procedure accepts two parameters, the path to the backup file and the name of the database to restore. The following is an example which restores the backup file we created earlier to a database named NewNorthwind.
master..RestoreDatabase 'd:tempNorthwind.bak', 'NewNorthwind'
I’ve found these two procedures to be very useful over the years, I hope you do too.
Please let me know if you have any feedback.