Connect Tech Support

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Monday, 17 September 2012

Back up all MS SQL databases at once

Posted on 21:12 by Unknown
This article will discuss how to backup all MS SQL databases with one script. A separate file will be created for each database.
  1. Log into your server through Remote Desktop Connection.
  2. Open SQL Server Management Studio and select the server name.
  3. Click the New Query button and enter in the following data:
  4. DECLARE @name VARCHAR(50) -- database name 

    DECLARE @path VARCHAR(256) -- path for backup files

    DECLARE @fileName VARCHAR(256) -- filename for backup

    DECLARE @fileDate VARCHAR(20) -- used for file name

    SET @path = 'C:\Backup\'

    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')

    OPEN db_cursor

    FETCH NEXT FROM db_cursor INTO @name

    WHILE @@FETCH_STATUS = 0

    BEGIN

    SET @fileName = @path + @name + '_' + @fileDate + '.BAK'

    BACKUP DATABASE @name TO DISK = @fileName

    FETCH NEXT FROM db_cursor INTO @name

    END

    CLOSE db_cursor

    DEALLOCATE db_cursor
  5. Make sure that the directory in the SET @path line exists. If the directory (in this case C:\Backup) does not exist, create the directory else the script will fail.
  6. Click the Execute! button and the script will execute.
  7. Once finished, a dialog box will appear stating such. Now all databases are backed up in C:\Backup with the database name as the file name.

    To take the full backup of all the databases in MS SQL server :

    1. Log into your server through Remote Desktop Connection.
    2. Open SQL Server Management Studio and select the server name.
    3. Click the New Query button and enter in the following data:
      -----------------------------------------------------------------
      DECLARE @BackupFile varchar(255), @DB varchar(30), @Description varchar(255), @LogFile varchar(50)
      DECLARE @Name varchar(30), @MediaName varchar(30), @BackupDirectory nvarchar(200)
      SET @BackupDirectory = 'C:\Backup\'
      --Add a list of all databases you don't want to backup to this.
      DECLARE Database_CURSOR CURSOR FOR SELECT name FROM sysdatabases WHERE name <> 'tempdb' AND name <> 'model' AND name <> 'master'
      OPEN Database_Cursor
      FETCH next FROM Database_CURSOR INTO @DB
      WHILE @@fetch_status = 0

          BEGIN
              SET @Name = @DB + '( Daily BACKUP )'
              SET @MediaName = @DB + '_Dump' + CONVERT(varchar, CURRENT_TIMESTAMP , 112)
              SET @BackupFile = @BackupDirectory + + @DB + '_' + 'Full' + '_' +
                  CONVERT(varchar, CURRENT_TIMESTAMP , 112) + '.bak'
              SET @Description = 'Normal' + ' BACKUP at ' + CONVERT(varchar, CURRENT_TIMESTAMP) + '.'

              IF (SELECT COUNT(*) FROM msdb.dbo.backupset WHERE database_name = @DB) > 0 OR @DB = 'master'
                  BEGIN
                      SET @BackupFile = @BackupDirectory + @DB + '_' + 'Full' + '_' +
                          CONVERT(varchar, CURRENT_TIMESTAMP , 112) + '.bak'
                      --SET some more pretty stuff for sql server.
                      SET @Description = 'Full' + ' BACKUP at ' + CONVERT(varchar, CURRENT_TIMESTAMP) + '.'
                  END   
              ELSE
                  BEGIN
                      SET @BackupFile = @BackupDirectory + @DB + '_' + 'Full' + '_' +
                          CONVERT(varchar, CURRENT_TIMESTAMP , 112) + '.bak'
                      --SET some more pretty stuff for sql server.
                      SET @Description = 'Full' + ' BACKUP at ' + CONVERT(varchar, CURRENT_TIMESTAMP) + '.'
                  END
                  BACKUP DATABASE @DB TO DISK = @BackupFile
                  WITH NAME = @Name, DESCRIPTION = @Description ,
                  MEDIANAME = @MediaName, MEDIADESCRIPTION = @Description ,
                  STATS = 10
              FETCH next FROM Database_CURSOR INTO @DB
      END
      CLOSE Database_Cursor
      DEALLOCATE Database_Cursor
      -----------------------------------------------------------------
    4. Make sure that the directory in the SET @path line exists. If the directory (in this case C:\Backup) does not exist, create the directory else the script will fail.
    5. Click the Execute! button and the script will execute.
    6. Once finished, a dialog box will appear stating such. Now all databases are backed up in C:\Backup with the database name as the file name.
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • How to schedule a PHP script in task scheduler
    Quiet often there is a need to execute/run  php  script on some time interval at server side. And that php scripts should run automatically ...
  • How to resolve Windows Login Error: "An unauthorized change was made to Windows"
    After connecting to windows server via RDC, you receive error “ An unauthorized change was made to Windows ” Resolution: 1) Click the opt...
  • HTTP Error 403.19 – Forbidden The configured user for this application pool does not have sufficient privileges to run CGI applications.
    If you get the error “HTTP Error 403.19 – Forbidden The configured user for this application pool does not have sufficient privileges to...
  • Disallowed Parent Path
    If you are unable to access the website and facing the below error: Active Server Pages error 'ASP 0131' Disallowed Parent Path /adm...
  • How to upgrade Zen Cart 1.3.9 to Zen Cart 1.5
    To upgrade your Zen Cart 1.3.9 to Zen Cart 1.5, follow the following steps. (For this tutorial, I assume you are using cpanel web hosting) 1...
  • Roles and Features showing an error HRESULT: 0x800F0818 in Server Manager of windows server 2008 R2
    When you open Server Manager both Roles and Features display Error and you are unable to add any role or features. When you select the det...
  • Reset Mysql root Password using my.ini
    Follow the below given steps to reset the Mysql root password : 1.Browse to your MySQL installation directory. 2. In there, go to "Data...
  • Block IP from accessing website using .htaccess
    Block IP from accessing website using .htaccess To block certain ip address from accessing your website, just create a file with name .hta...
  • Enable Canonical URL in IIS7 for SEO
    What is URL Canonicalization?  – Its just nothing but you are making sure that your users and the search engines are accessing your websites...
  • An ASP.NET setting has been detected that does not apply in Integrated managed pipeline mode.
    If you are facing the following error while accessing the website: Error Summary HTTP Error 500.24 - Internal Server Error An ASP.NET settin...

Categories

  • booting Process
  • linux
  • redhat

Blog Archive

  • ►  2013 (68)
    • ►  July (1)
    • ►  May (2)
    • ►  April (11)
    • ►  March (54)
  • ▼  2012 (44)
    • ▼  September (20)
      • How to check Malware injection in Code
      • How to improve internet speed on your Windows XP D...
      • How to Change RDP Port
      • Could not load type ‘System.ServiceModel.Activatio...
      • Windows Login Error: An unauthorized change was ma...
      • How to Configure SmarterMail 9 site in IIS7
      • Back up all MS SQL databases at once
      • How to Install Smartermail 9
      • Back up Windows Server 2003 Registry
      • Map Local Drives through Remote Desktop
      • Stopping Unneeded Services In Windows Server 2008
      • Create a scheduled task in Windows Server 2008
      • Adjust Page File / Virtual Memory on Server 2008
      • Checking Available Diskspace For Windows 2008
      • Force Visitor Browser to use SSL
      • Enable multiple RDC sessions from one user in Wind...
      • Allow Passive FTP Access Through Windows 2008 Fire...
      • Set up an IP Security Policy Rule for Windows 2008...
      • Install PHPMyAdmin on IIS7 and Server 2008
      • Install MySQL on a Windows Server
    • ►  August (1)
    • ►  July (4)
    • ►  June (12)
    • ►  May (2)
    • ►  March (4)
    • ►  February (1)
  • ►  2011 (1)
    • ►  February (1)
  • ►  2009 (9)
    • ►  September (3)
    • ►  August (2)
    • ►  June (1)
    • ►  May (2)
    • ►  March (1)
Powered by Blogger.

About Me

Unknown
View my complete profile