Backup Recovery Script Deployment

Embed Size (px)

Citation preview

  • 8/13/2019 Backup Recovery Script Deployment

    1/15

    BACKUP & RECOVERY OF DATABASE

    Backup of Database

    As system is an electronic device, at any point of time it may come across

    with a problem in hardware or at os or at db, due to which data available in db

    may be lost.

    In order to have security for data and database objects, it is essential that

    at the end of a day to go for a backup. It creates a backup of database into

    flatfiles. Backup data later can be transfered into other external devices for

    security reasons.

    Backup can be created in different ways;

    1. Full backup

    2. Differential backup

    3. Files And Filegroups

    4. Transaction Log

    Syntax: (FULL BACKUP)

    BACKUP DATABASE

    TO DISK='DRIVE:\FILENAME.BAK'

    File name should be provided with an extension as .bak

    Creating a backup thru wizard

    Connect to Database Engine,

    Object Explorer

    Databases

  • 8/13/2019 Backup Recovery Script Deployment

    2/15

    Select the database (Batch33)

    Right click, Tasks, click on BACKUP

    EXAMPLES:

    FULL BACKUP:

    BACKUP DATABASE BATCH33 TO DISK='C:\MYDATA\B33DB.BAK'

    DIFFERENTIAL BACKUP:

    BACKUP DATABASE Batch33 TO DISK='C:\MYDATA\B33DB.BAK'

    WITH DIFFERENTIAL

    File and File groups:

    Restore of Database:

    Database can be restored in a same system or it can be on different system.

    Syntax:

    RESTORE DATABASE db_name FROM DISK='DRIVE:\FNAME.BAK'

    EXAMPLE:

    USE MASTERRESTORE DATABASE Batch33 FROM DISK='C:\MYDATA\B33DB.BAK'

  • 8/13/2019 Backup Recovery Script Deployment

    3/15

    Extended Stored Procedures:

    A stored procedure contains T-SQL statements and C/C++ code compiled

    into .dll file is called extended stored procedure.

    Every extended stored procedure starts with XP_

    All extended stored procedures are stored in MASTER database.

    Extended procedures are used to perform operations in OS and Mail Server.

    XP_DIRTREE PATH

    XP_FIXEDDRIVES

    XP_MSVER

    XP_FILEEXIST

    XP_CMDSHELL 'os command' ---- It is used to communicate with operatingsystem to execute commands.

    NOTE: To work with this extended procedure it should be enabled at

    configuration tools:

    Start--> Programs--> Microsoft SQL Server 3305--> Configuration Tools--> SQL

    Server Surface Area Configuration --> Surface area configuration for features -->

    select cmd shell and check the option cmd shell.

    (OR)

    SP_CONFIGURE 'CMDSHELL',1

    RECONFIGURE

  • 8/13/2019 Backup Recovery Script Deployment

    4/15

    Example:

    USE BATCH33

    XP_CMDSHELL 'MD C:\SQLS'

    EXEC XP_SENDMAIL |

    EXEC XP_READMAIL | to interact with mail server.

    DEPLOYMENT OF DATABASE:

    The process of copying database objects structure with data from one

    server to another server is called Deployment of database.

    Method 1:

    Copy backup file (.bak) to destination system.

    Restore it on destination system

    Method 2:

    Copy data files and log files to destination system

    1. Detach the database on Source System

    sp_detach_db database_name

    (OR)

    object explorer --> Databases --> Select the Database (Batch33) --> Right

    click ---> tasks---> detach

  • 8/13/2019 Backup Recovery Script Deployment

    5/15

    2. Copy the database files to same location on destination System

    3. Attach these files on destination system.

    Syntax:

    CREATE DATABASE database_name

    ON

    (FILENAME='path of a file')

    FOR ATTACH

    (OR)

    Object Explorer--->Databases-->Right click --> Attach

    Click Add

    Select the mdf from the list of files

    After selecting click ok, click ok

    Method 3:

    Creating a Batch and Script Files:

    Batch is nothing but set of T-SQL statements executed under single

    execution plan. Every batch is followed by GO (which makes a separate execution

    plan).

    Creation of any object, setting any property should be presented in

    different batches.

    Script is nothing but collection of batches saved in a file with .sql extension

  • 8/13/2019 Backup Recovery Script Deployment

    6/15

    Advantages:

    1. Script files are reusable

    2. Script files are used to copy database objects from one system to another

    system.

    Generating T-SQL scripts for existing database:

    SQL Server provides Script wizard to generate Scripts for existing database

    and other objects.

    Object Explorer ----> Connect to Database Engine ----> Expand databases ---->

    Select the database(batch33)----> right click Tasks---->Generate Scripts

    STEP 1:

    Click next

    STEP 2:

    Select the database(Batch33)Check script all objects in Selected Database

    Click next

    STEP 3:

    Provide Scripting options

    Append to File = false / true

    If true ----- Select existing file

    If false ---- new file is created

    Script database create = True / False

    If true ---- Create db statement generated

    If false --- Create db statement will not be generated

  • 8/13/2019 Backup Recovery Script Deployment

    7/15

    Script for server version ---- SQL Server 3305

    STEP 4:

    Script to File = d:\TEST.sql

    STEP 5:

    Click Finish

    Example: (Creating User Defined Script File)

    CREATE DATABASE TESTING

    GO

    USE TESTING

    CREATE TABLE EMPT

    (EMPNO INT PRIMARY KEY,EN VARCHAR(12))

    GO

    CREATE VIEW V1

    AS

    SELECT * FROM EMPTGO

    FROM file menu click on save and specify the file name with .SQL extension.

    DATA TRANSFORMATION SERVICES (DTS)

    OR

    SQL SERVER INTEGRATION SERVICES (SSIS)

    In SQL Server 2000 we have DTS packages, which are based on COM technology

    (Component Object Model)

  • 8/13/2019 Backup Recovery Script Deployment

    8/15

    In SQL Server 2005 it is introduced with SSIS packages, which is based on .net

    technology.

    To Import or Export database objects and data from one data source to another

    data source, we can use DTS or SSIS packages.

    SSIS package will support to import or export the database and data between the

    following softwares;

    SQL Server MS-Access

    SQL Server Oracle

    SQL Server SQL Server

    SQL Server Text File/XML Files /Excel Files..... etc

    DTS or SSIS communicates with any data source with the help of ODBC or OLEDB

    or .net data providers.

    _______________ ODBC_____________ Data Src

    |

    DTS/SSIS ______ OLEDB_________ Data Source|

    ______ .net data providers______Data Source

    Data source can be Oracle, SQL Server, .......... etc

    SQL NATIVE CLIENT interface can be used to communicate with only SQL Server

    ODBC --- Open database Connectivity

    OLEDB --- Object Linking Embedding Databases

    Working Procedure:

    Open Object Explorer

  • 8/13/2019 Backup Recovery Script Deployment

    9/15

    |

    Connect to Database Engine

    |

    Select the Server

    |

    Select any database (Batch33)

    |

    Right Click and select Import or Export Data.

    Example 1: Exporting data from SQL Server to Excel Sheet

    SELECT database (Batch33), right click, tasks, export data;

    STEP 1: click Next

    STEP 2: Specify Source Data

    Data Source : SQL Native Client

    Server Name : test

    Database : Batch33

    Click Next

    STEP 3: Specify Destination

    Destination : Microsoft Excel

    Excel File Path : C:\MYDATA\B33DB.XLS

    Check First row has column names

    Click Next

    STEP 4: Select Copy data from one or more tables or views

  • 8/13/2019 Backup Recovery Script Deployment

    10/15

  • 8/13/2019 Backup Recovery Script Deployment

    11/15

    Click Next

    STEP 3 : Specify Destination

    Destination : SQL Native Client Server Name : test

    Database : Batch33

    Click Next

    STEP 4 : Select copy data from one or more tables or views

    Click Next

    STEP 5 : Select tables and views to be copied

    Click Next

    STEP 6 : Check Execute Immediately

    Click Next

    STEP 7 : Click Finish

    NOTE:

    SSIS packages will be stored in MSDB database.

    Browsing SSIS packages:

    Object Explorer

    |

    Connect to Integration Services

  • 8/13/2019 Backup Recovery Script Deployment

    12/15

    |

    Select Stored Packages

    |

    MSDB (System Database)

    |

    Select package & Right click Run Package

    SQL SERVER AGENT:

    In SQL Server2005 SQL Server Agent is a service.

    It supports to create Jobs, Schedules, Alerts And Operators.

    It provides automatic execution of jobs and tasks associated with different

    steps automatically based on system date, day and time.

    To work with SQL Server Agent it is essential that services should get start.

    To start the service the following steps should be used.

    start--->programs--->Microsoft SQL Server 3305---> Configuration Tools--->SQL

    Server Configuration Manager---> Select SQL Server Agent (double click on the

    services and click on start)

    Creating a table:

    CREATE TABLE EMPJ

    (ENO INT IDENTITY,DOJ SMALLDATETIME)

    To work with SQL Server Agent;

  • 8/13/2019 Backup Recovery Script Deployment

    13/15

    Object Explorer

    |

    Expand SQL Server Agent

    Creating a job, Steps and Schedules:

    STEP 1:

    Select the jobs, right click and new job

    Name : ins_rec_job

    Owner : sa

    Category: [Uncategorized [lOCAL]]

    STEP 2:

    select "steps" from "select a page window"

    Click on New

    Step Name : ins_rec

    Type : Transact-SQL script (T-SQL)

    Database : Batch33

    Command : INSERT EMPJ VALUES(getdate())

  • 8/13/2019 Backup Recovery Script Deployment

    14/15

    Click Ok

    STEP 3:

    Select "Schedules" from "Select a page window"

    Click New

    Name : sch_ins_rec

    Schedule Type : Recurring

    Frequency

    Occurs : Daily

    Recurrs Every : 1 Day

    Daily Frequency

    Occurs Every : 1 Min

    Click Ok

    Click Ok

    NOTE:

    To execute the created procedure automatically based on a created schedule.

    CREATE PROCEDURE insrow

  • 8/13/2019 Backup Recovery Script Deployment

    15/15

    AS

    BEGIN

    INSERT EMPJ VALUES(getdate())

    END

    Under steps window, create a new step and provide the command used to

    execute the procedure;

    EXEC insrow