What you want to see on DotNetFunda.com ?
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 3114 |  Welcome, Guest!   Register  Login
Home > Articles > Sql Server > How can we take all the SQL SERVER Database Backup at once

How can we take all the SQL SERVER Database Backup at once

Article posted by Rn2786 on 7/19/2011 | Views: 2874 | Category: Sql Server | Level: Intermediate | Points: 250 red flag


Here I am going to share how can we take the database backup of all the SQL Server databases at once instead of taking it individually.

Download


 Download source code for How can we take all the SQL SERVER Database Backup at once


Hello Friends,


Here I am going to share how can we take the database backup of all the SQL Server databases at once instead of taking it individually.

First we need to create a folder in any drive to store the backup of database which we are going to take.

Here I have created a folder named "dbBackup" in my drive "C:\"


Copy the below code & paste it in your SQL Server Query Executer page:


DECLARE @DBName varchar(255)
DECLARE @DATABASES_Fetch int
DECLARE DATABASES_CURSOR CURSOR FOR
    select
        DATABASE_NAME   = db_name(s_mf.database_id)
    from
        sys.master_files s_mf
    where
       -- ONLINE
        s_mf.state = 0 

       -- Only look at databases to which we have access
    and has_dbaccess(db_name(s_mf.database_id)) = 1 

        -- Not master, tempdb or model
    and db_name(s_mf.database_id) not in ('Master','tempdb','model')
    group by s_mf.database_id
    order by 1

OPEN DATABASES_CURSOR

FETCH NEXT FROM DATABASES_CURSOR INTO @DBName

WHILE @@FETCH_STATUS = 0
BEGIN
    declare @DBFileName varchar(256)    
    set @DBFileName = datename(dw, getdate()) + ' - ' + 
                       replace(replace(@DBName,':','_'),'\','_')

    exec ('BACKUP DATABASE [' + @DBName + '] TO  DISK = N''c:\dbBackup\' + 
        @DBFileName + '.bak' + ''' WITH NOFORMAT, INIT,  NAME = N''' + 
        @DBName + '-Full Database Backup'', SKIP, NOREWIND, NOUNLOAD,  STATS = 100')

    FETCH NEXT FROM DATABASES_CURSOR INTO @DBName
END

CLOSE DATABASES_CURSOR
DEALLOCATE DATABASES_CURSOR


Now execute the page & you are done....!!!


Check your Folder "dbBackup" in Drive "C:/" . You will find all the databases backup in it.


Enjoy...!! :)

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Experience:5 year(s)
Home page:www.creativecoder.in
Member since:Tuesday, February 22, 2011
Level:Starter
Status: [Member]
Biography:System Admin, Software-developer & web designer.
 Responses
Posted by: PandianS | Posted on: 20 Jul 2011 11:48:29 AM | Points: 25

Hi

Nice sample!

I would like to say something, The script is used for RESTORE! not for BACKUP. Am I correct ?

The explanation is given for BACKUP but the script is given for RESTORE!

Kindly check and change accordingly!

Posted by: Akiii | Posted on: 21 Jul 2011 12:37:04 AM | Points: 25

Excellent stuff....it is quite helpful

Thanks for sharing.....

Regards
Akiii

>> Write Response - Respond to this post and get points
Related Posts

In this article, we will look into the Try_Convert function of SQL Server 2012 (Denali).

This is part 36 of the series of article on SSIS. In this article we are going to see on how to use an Aggregate (Maximum) data flow transformation control in SSIS packaging.

This is part 36 of the series of article on SSIS. In this article we are going to see on how to use an Aggregate (Minimum) data flow transformation control in SSIS packaging.

This article (Part I) explores one of the feature (of SQL server 2008 and higher) of providing the auditing abilities i.e. CDC – Change Data Capture. We will discuss the intrinsic of CDC in Part II.

This article describes how to update Remote SQL Server Databases with a Single Click.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/26/2013 1:17:49 AM