But what if we have a lot of databases on our SQL Server, and want to take backup all the database to our local drive. i.e., save all the .bak file of our databases in our local C drive or D drive respectively.
In this case, using SQL Server Management Studio and taking database backup manually one by one is not a good option. So to solve this, we make a simple SQL Script .i.e( StoredProcedure Created ) which do the excellent job for us, in short, it takes backup of all the database in SQL SERVER.
Steps to Taking Backup of all Databases in MQ Sqlserver.
- Create a Backup folder (where all database backup .bak file would be saved ).
- Create a STORED PROCEDURE (which takes backup of databases)
#1 Create a Backup folder i.e.(directory)
First, we create a folder in our Local C drive and name it as MyBackup. The newly created folder MyBackup is used to save all our databases .bak.
#2 Create STORED PROCEDURE which takes backup of databases.
After creating the folder, now we make a stored procedure which does the job. Let go in depth of the stored procedure. With the below-written query, we get the list all databases from our SQL server.
SELECT name FROM master.dbo.sysdatabases
You might have noticed the above query returns all the database including default database. If you want to exclude default databases or specific database while taking the backup, then using where condition we make our custom selection of database. Our query looks like this
SELECT name FROM master.dbo.sysdatabases WHERE name NOT IN ('master','model','msdb','tempdb') -- exclude these databases
Right now we are done with the database selection; next, we want to take backup of these databases. By using MS SQL BACKUP DATABASE statement, we can easily take the full backup of the database. Simple example of using backup database statement look like as written below
DECLARE @fileName varchar(250); SET @filePath_Name ='C:\MyBackup\tableName.bak'; BACKUP DATABASE @dbName TO DISK =@filePath_Name;
We are almost complete with taking database backup. As we want to take backup of all the database, we need to make a loop on all the database and take backup of each. Here we need to use CURSOR which loops over each database and take backup of each.
Our Final Stored Procedure looks like as written below.
CREATE PROCEDURE GET_BACKUP( @backupPath VARCHAR(256) -- specify database backup file directory ) AS BEGIN DECLARE @dbName VARCHAR(50) -- database name DECLARE @fileName VARCHAR(256) -- filename for backup DECLARE @fileDate VARCHAR(20) -- used for file name -- specify filename format SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112) DECLARE db_cursor CURSOR FOR SELECT name FROM master.dbo.sysdatabases WHERE name NOT IN ('master','model','msdb','tempdb') -- exclude these databases OPEN db_cursor FETCH NEXT FROM db_cursor INTO @dbName WHILE @@FETCH_STATUS = 0 BEGIN SET @fileName = @backupPath + @dbName + '_' + @fileDate + '.BAK' BACKUP DATABASE @dbName TO DISK = @fileName FETCH NEXT FROM db_cursor INTO @dbName END CLOSE db_cursor DEALLOCATE db_cursor END
The last step is to execute this stored procedure and get all the databases; pls check your backup folder you will be able to see all .bak files are generating one by one.
EXEC GET_BACKUP 'C:\MyBackup\'
This article covers a complete tutorial on how to take a full database backup of all the database or specific databases from SQL SERVER 2008 by using above SQL Script .i.e. ( Stored Procedure). Hope you like this tutorial. If you have any recommendations, please let us know what you think in the comment section below! See you again next time!