[SQL Server]使用T-SQL來備份與還原資料庫

使用T-SQL來備份與還原資料庫

--建立一個名叫"puma"的DB  

USE [master]
GO
CREATE DATABASE [puma] ON  PRIMARY 
( NAME = N'puma', FILENAME = N'D:\DB\puma.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
 LOG ON 
( NAME = N'puma_log', FILENAME = N'D:\DB\puma_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO

 --備份"puma"DB 

BACKUP DATABASE puma TO DISK = 'D:\DB\puma.bak'
GO

--還原"puma"DB到新的DB"puma2" 

RESTORE DATABASE puma2
   FROM DISK = 'D:\DB\puma.bak'
   WITH 
   MOVE 'puma' TO 'D:\DB\puma2.mdf',
   MOVE 'puma_log' TO 'D:\DB\puma2_log.ldf',
  REPLACE;
GO