How to take SQL server database backup without data?
Version: Sql server 2012
There are different method available to achieve this goal. Such as..Script out the source database and then run the script against an empty target database to create all database objects that are in the source database
Right click on the database -> select "tasks" -> "Generate scripts"-> Next -> Select script entire database and all database objects -> Save the sql file in location ->Next-> Next-> Finish.
Now If you want to restore the database just execute the content of sql file and this will create a new database with only data structure .
This is the content of sql file.
USE [master]
GO
/****** Object: Database [newdb] Script Date: 2/3/2017 10:20:17 AM ******/
CREATE DATABASE [newdb]
CONTAINMENT = NONE
ON PRIMARY
( NAME = N'sourcedb', FILENAME = N'E:\MSSQLSERVER\MSSQL11.MSSQLSERVER\MSSQL\DATA\newdb.mdf' , SIZE = 3136KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
LOG ON
( NAME = N'sourcedb_log', FILENAME = N'E:\MSSQLSERVER\MSSQL11.MSSQLSERVER\MSSQL\DATA\newdb_log.ldf' , SIZE = 768KB , MAXSIZE = UNLIMITED, FILEGROWTH = 10%)
GO
ALTER DATABASE [newdb] SET COMPATIBILITY_LEVEL = 110
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [newdb].[dbo].[sp_fulltext_database] @action = 'enable'
end
GO
ALTER DATABASE [newdb] SET ANSI_NULL_DEFAULT OFF
GO
ALTER DATABASE [newdb] SET ANSI_NULLS OFF
GO
ALTER DATABASE [newdb] SET ANSI_PADDING OFF
GO
ALTER DATABASE [newdb] SET ANSI_WARNINGS OFF
GO
ALTER DATABASE [newdb] SET ARITHABORT OFF
GO
ALTER DATABASE [newdb] SET AUTO_CLOSE OFF
GO
ALTER DATABASE [newdb] SET AUTO_CREATE_STATISTICS ON
GO
ALTER DATABASE [newdb] SET AUTO_SHRINK OFF
GO
ALTER DATABASE [newdb] SET AUTO_UPDATE_STATISTICS ON
GO
ALTER DATABASE [newdb] SET CURSOR_CLOSE_ON_COMMIT OFF
GO
ALTER DATABASE [newdb] SET CURSOR_DEFAULT GLOBAL
GO
ALTER DATABASE [newdb] SET CONCAT_NULL_YIELDS_NULL OFF
GO
ALTER DATABASE [newdb] SET NUMERIC_ROUNDABORT OFF
GO
ALTER DATABASE [newdb] SET QUOTED_IDENTIFIER OFF
GO
ALTER DATABASE [newdb] SET RECURSIVE_TRIGGERS OFF
GO
ALTER DATABASE [newdb] SET DISABLE_BROKER
GO
ALTER DATABASE [newdb] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
GO
ALTER DATABASE [newdb] SET DATE_CORRELATION_OPTIMIZATION OFF
GO
ALTER DATABASE [newdb] SET TRUSTWORTHY OFF
GO
ALTER DATABASE [newdb] SET ALLOW_SNAPSHOT_ISOLATION OFF
GO
ALTER DATABASE [newdb] SET PARAMETERIZATION SIMPLE
GO
ALTER DATABASE [newdb] SET READ_COMMITTED_SNAPSHOT OFF
GO
ALTER DATABASE [newdb] SET HONOR_BROKER_PRIORITY OFF
GO
ALTER DATABASE [newdb] SET RECOVERY FULL
GO
ALTER DATABASE [newdb] SET MULTI_USER
GO
ALTER DATABASE [newdb] SET PAGE_VERIFY CHECKSUM
GO
ALTER DATABASE [newdb] SET DB_CHAINING OFF
GO
ALTER DATABASE [newdb] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF )
GO
ALTER DATABASE [newdb] SET TARGET_RECOVERY_TIME = 0 SECONDS
GO
EXEC sys.sp_db_vardecimal_storage_format N'newdb', N'ON'
GO
USE [newdb]
GO
/****** Object: User [test1] Script Date: 2/3/2017 10:20:17 AM ******/
CREATE USER [test1] FOR LOGIN [test1] WITH DEFAULT_SCHEMA=[dbo]
GO
/****** Object: User [som] Script Date: 2/3/2017 10:20:17 AM ******/
CREATE USER [som] FOR LOGIN [som] WITH DEFAULT_SCHEMA=[dbo]
GO
/****** Object: User [readonly] Script Date: 2/3/2017 10:20:17 AM ******/
CREATE USER [readonly] FOR LOGIN [readonly] WITH DEFAULT_SCHEMA=[db_datareader]
GO
ALTER ROLE [db_owner] ADD MEMBER [test1]
GO
ALTER ROLE [db_owner] ADD MEMBER [som]
GO
ALTER ROLE [db_datareader] ADD MEMBER [readonly]
GO
/****** Object: Table [dbo].[Employee] Script Date: 2/3/2017 10:20:17 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Employee](
[ID] [int] NULL,
[Value] [varchar](10) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[t1] Script Date: 2/3/2017 10:20:17 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[t1](
[id] [varchar](255) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[t2] Script Date: 2/3/2017 10:20:17 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[t2](
[id] [varchar](255) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
USE [master]
GO
ALTER DATABASE [newdb] SET READ_WRITE
GO
Incase if you want to restore the database with different name then we would require to modify the sql file with the new database name .
Another method is: Backup the source database and restore to the destination database and then delete all table data.
Now we will show you another method which also serve our purpose.
Backup the database without data.
In the SSMS Object Explorer Window, right click on the "newdb" database and choose "Tasks" > "Extract Data-tier Application..."
The [Extract Data-tier Application] wizard will start.
Provide the DAC package file location
click "Next"
click "Next"
Now we have newdb.dacpac file generated.
Restore a SQL Server Database from a DAC package
The DAC package can be restored to a target SQL Server instance whose version is equal to or higher than that of the source SQL Server instance.
SSMS Window, right click [Databases] , and choose "Deploy Data-tier Application...", as shown below
The [Deploy Data-tier Application] wizard will start, Click next in the first [Introduction] screen, and in the [Select Package] screen, click the Browse button to find the DAC package file location
The [Deploy Data-tier Application] wizard will start, Click next and Browse button to find the DAC package file location.
Click Next, and in the [Update Configuration] screen, input the required destination database name or leave it as if you don’t want to change the db name.
That’s it. We have successfully restored the database with only data structure.
Nenhum comentário:
Postar um comentário