2016年11月22日星期二

SQL Server Database Backup - 3 Methods

2016-11-1

Database backup is not the DBA only work. Most of developers and even small business owners also need to perform this task. This article will introduce 3 methods to back up database from SQL Server.

  1. Method 1 – from management studio
    1. Connect to database server
    2. Expend Databases folder



                3.       Right click on the database that you want to backup
                4.       from Context menu click Task
                5.       from sub menu click on Back Up…


               6.       on the popup window click on Add

               7.       on Select Backup Destination window you could enter file location and file name directly or click on … button to specify a backup file.
               8.       Click on OK button to close the window, then click on OK again and wait for the completion.


   2. Method 2 – using T-SQL script

You could either to execute the script on Management Studio or store it as stored procedure to execute.
Here’s the script for backing up all user databases:

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

-- specify database backup directory
SET @path = 'C:\DB\MSSQL11.MSSQLSERVER\MSSQL\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')  -- exclude system db

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


    3. Method 3 – using SSIS Service

In order to use SSIS Service, you need SQL Server Development Tool (SSDT) or SQL Server Business Intelligence Development Studio (BIDS) for SQL 2008 and older to create a package. You can deploy the package to SQL Server and schedule a job to auto-run it.

          1.       Create a SSIS project from Visual Studio (SSDT installed). Note that the project template is under Business Intelligence folder, not SQL Server.

        2.       Create a package and open it, drag and drop “Back Up Database Task” onto the Control Flow work space.
        3.       Double click on the task, “Back Up Database Task” window will popup.



           4.       Click on New… button to specify the database connection.




              5.       On “Back Up Database task” window click on Database(s) drop down button, and select the database option you want to back up.


                  6.       In Destination tab, specify Folder location and click OK.

               7.       Save the package.


               8.       You could execute the task right inside the studio. Right click on the task and select Execute Task from context menu.


                    You will see the green check mark when the execution completed.


              9.       Now you can run the package or debug the package directly in the Visual Studio, or you can deploy the package to SQL Server to run.



没有评论:

发表评论