Home > Link, Microsoft SQL Server, MSBI, Optimization, Query, Script, SQL Mentalist, SQL PraRup, SQL Query, SQL Server, Technology,, TSQL, Vishal Pawar > BI SQL # 232 : SQL Server DBA Scripts : Backup Old And Restore new Database through Script

BI SQL # 232 : SQL Server DBA Scripts : Backup Old And Restore new Database through Script

Hi Folks,

In this article we are going to cover How to Backup Old And Restore new Database through Script.

In this post we are going to discuss following points:

  • Problem Statement of SQL Script:
  • Description of SQL Script:
  • Input Parameter of SQL Script
  • SQL Script Code
  • User Level to execute

Problem Statement of SQL Script:

How to take backup and restore new database through script ?

Description of SQL Script:

This script will backup and restore new database through script.

Input Parameter of SQL Script

Need to Update file Path in Script

SQL Script Code

DECLARE @RestoreStatement NVARCHAR(max)
    ,@BackupFile NVARCHAR(max)
    ,@logical_data NVARCHAR(max)
    ,@logical_log NVARCHAR(max)
    ,@DbName VARCHAR(Max)
    ,@OldDbName VARCHAR(Max)
    ,@var NVARCHAR(max)
    ,@path VARCHAR(Max)
    ,@LogFileName VARCHAR(250)
    ,@MdfFileName VARCHAR(250)

SET @DbName = 'BenEngine18062012' --Input Database Name(Input)
SET @OldDbName = @DbName + (
        SELECT REPLACE(CONVERT(VARCHAR(20), GETDATE(), 103), '/'
                , '') + '_' + CONVERT(VARCHAR(10), DATEPART(hour, 
                    GETDATE())) + 'h' + CONVERT(VARCHAR(10), 
                DATEPART(minute, GETDATE())) + 'm' + CONVERT(
                VARCHAR(10), DATEPART(second, GETDATE())) + 's'
        )
SET @BackupFile = 
    'D:\BenEnginePhase2\Backup\BenEngine02072012.bak'
--DB Backup File Path (Input)
SET @path = 'D:\BenEnginePhase2\Backup\'
--LDF and MDF File Storage (Input)
SET @RestoreStatement = N'RESTORE FILELISTONLY FROM DISK=N''' + 
    @BackupFile + ''''

DECLARE @FileList TABLE (
    LogicalName NVARCHAR(128) NOT NULL
    ,PhysicalName NVARCHAR(260) NOT NULL
    ,Type CHAR(1) NOT NULL
    ,FileGroupName NVARCHAR(120) NULL
    ,Size NUMERIC(20, 0) NOT NULL
    ,MaxSize NUMERIC(20, 0) NOT NULL
    ,FileID BIGINT NULL
    ,CreateLSN NUMERIC(25, 0) NULL
    ,DropLSN NUMERIC(25, 0) NULL
    ,UniqueID UNIQUEIDENTIFIER NULL
    ,ReadOnlyLSN NUMERIC(25, 0) NULL
    ,ReadWriteLSN NUMERIC(25, 0) NULL
    ,BackupSizeInBytes BIGINT NULL
    ,SourceBlockSize INT NULL
    ,FileGroupID INT NULL
    ,LogGroupGUID UNIQUEIDENTIFIER NULL
    ,DifferentialBaseLSN NUMERIC(25, 0) NULL
    ,DifferentialBaseGUID UNIQUEIDENTIFIER NULL
    ,IsReadOnly BIT NULL
    ,IsPresent BIT NULL
    ,TDEThumbprint VARBINARY(32) NULL
    );

INSERT INTO @FileList
EXEC (@RestoreStatement);

SET @logical_data = (
        SELECT LogicalName
        FROM @FileList
        WHERE Type = 'D'
            AND FileID = 1
        )
SET @logical_log = (
        SELECT LogicalName
        FROM @FileList
        WHERE Type = 'L'
            AND FileID = 2
        )

EXEC sp_renamedb @DbName
    ,@OldDbName

SET @LogFileName = @path + (
        SELECT REPLACE(CONVERT(VARCHAR(20), GETDATE(), 103), '/'
                , '') + '_' + CONVERT(VARCHAR(10), DATEPART(hour, 
                    GETDATE())) + 'h' + CONVERT(VARCHAR(10), 
                DATEPART(minute, GETDATE())) + 'm' + CONVERT(
                VARCHAR(10), DATEPART(second, GETDATE())) + 's' + 
            '.ldf'
        )
SET @MdfFileName = @path + (
        SELECT REPLACE(CONVERT(VARCHAR(20), GETDATE(), 103), '/'
                , '') + '_' + CONVERT(VARCHAR(10), DATEPART(hour, 
                    GETDATE())) + 'h' + CONVERT(VARCHAR(10), 
                DATEPART(minute, GETDATE())) + 'm' + CONVERT(
                VARCHAR(10), DATEPART(second, GETDATE())) + 's' + 
            '.mdf'
        )
SET @var = 'RESTORE DATABASE @DbName FROM
DISK=''' + @BackupFile 
    + '''
WITH FILE = 1,
MOVE N''' + @logical_data + 
    '''
TO N''' + @MdfFileName + ''',
MOVE N''' + @logical_log + 
    '''
TO N''' + @LogFileName + ''',
REPLACE, STATS = 10'

--declare @var
EXECUTE sp_executesql @var
    ,
    N'@LogFileName varchar(MAX),@logical_log varchar(MAX),
@MdfFileName varchar(MAX),@logical_data varchar(MAX),
@DbName Varchar(Max)'
    ,@LogFileName
    ,@logical_log
    ,@MdfFileName
    ,@logical_data
    ,@DbName

User Level to execute

400

    Hope you will like How to Backup Old And Restore new Database through Script.

    If you really like reading my blog and understood at least few thing then please don’t forget to subscribe my blog.

If you want daily link and analysis or interesting link go to following website which will give @ your inbox please subscribe our following link resource blog :

Link Resource Website

For More information related to BI World visit my Mentalist Blog

SQL Server Mentalist >> SQL Learning Blog

Business Intelligence Mentalist >> BI World

Infographic Mentalist >> Image worth explaining thousand Words

Microsoft Mentalist >> MVC,ASP.NET, WCF & LinQ

DBA Mentalist >>Advance SQL Server Blog

Microsoft BI Mentalist >> MS BI Development Update

Connect With me on

| FaceBook |Twitter | linkedIn| Google+ | WordPress | RSS |

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: