码迷,mamicode.com
首页 > 数据库 > 详细

数据库备份

时间:2016-05-13 07:30:20      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:

 

首先简单的介绍一下Sql server 备份的类型有:

1:完整备份(所有的数据文件和部分的事务日志文件)

2:差异备份(最后一次完成备份后数据库改变的部分)

3:文件和文件组备份(对指定的文件和文件组备份)

4:事物日志备份(所有数据库的变更)

5:尾日期备份(日志的活动部分,指上一次为备份的日志部分)

6:部分备份(主文件组、每个可读可写文件组和指定的只读文件组)

7:仅复制备份(数据库或者日志的备份,不影响整体备份)

技术分享
-- NorthWind是数据库名字
--------------------完整备份默认追加到现有的文件---------------
backup database NorthWind
 
To disk=d:\backup\NorthWindCS-Full-2010-11-23.bak
 
--------完整备份,覆盖现有的文件
Backup database NorthWind
To disk=d:\backup\NorthWindCS-Full-2010-11-23.bak
With init---覆盖现有文件代码
 
--------差异备份(上次一完整备份以来改变的数据页)
backup database NorthWind
To Disk=d:\backup\NorthWindCS-Full-2010-11-23.bak
with NAME=[BackupName],DIFFERENTIAL

 
-----事物日志备份,会自动截断日志(默认会阶段日志)
backup log NorthWind
To Disk=d:\backup\NorthWindCS-log-2010-11-23
 
-----事物日志备份,不截断日志(默认会阶段日志)
backup log NorthWind
To Disk=d:\backup\NorthWindCS-log-2010-11-23
With No_Truncate
 
-----不备份直接阶段日志,在SQL SERVER2008中不再支持。
backup log NorthWind With No_Log
backup log NorthWind With Tuancate_only
 
-----SQL SERVER 2008 替代的截断日志方法
alter database NorthWind set Recovery Simple
exec sp_helpdb NorthWInd
use NorthWind 
dbcc shrinkfile(NorthWind_log)
alter database NorthWind set Recovery Full
 
----超大型数据库的文件和文件组备份
Exec sp_helpdb NorthWind
backup database NorthWind File=NorthWind_Current
to disk=h:\backup\NorthwindCS_Full_2010031.bak
backup database NorthWind FileGroup=Current
to disk=h:\backup\NorthwindCS_FG_2010031.bak
 
---仅复制备份,不影响现有的备份序列
backup database NorthWind
To disk=h:\backup\NorthwindCS_Full_2010031.bak
With Copy_only
 
 
--尾部日志备份,备份完成后数据库不再提供访问
use master
go
backup log NorthWind
to disk=h:\backup\Northwind-taillog-20101031.bak
With NoRecovery
 
--回复数据库提供访问
Restore databse NorthWind with Recovery
 
--分割备份到多个目标文件
backup database NorthWind 
to disk=h:\backup\Northwind-part1.bak,
disk=h:\backup\NorthwindCS-part2.bak
 
--镜像备份,需要加入With Format
backup database NorthWind
to disk=h:\backup\NorthwindCS-Mirror1.bak
Mirror to disk=h:\backup\NorthwindCS-Mirror2.bak----Mirror镜像
With Format
 
 
--备份到远程服务器
--使用SQL SERVER 的服务启动账号访问远程共享可写文件夹
backup database Northwind
to disk=\\192.168.3.20\backup\nw-yourname.bak
 
--备份到远程服务器,指定访问远程服务器的账号和密码
Exec sp_configure
Exec Sp_COnfigure show advanced options,1
Reconfigure with Overrid
Exec sp_configure xp_cmdshell,1
Reconfigure with override
 
 
Exec xp_cmdshell
net use \\192.168.10.101 /user:administrator password
 
backup database Northwind 
to disk=\\192.168.10.101\backup\nw-fy.bak
 
Exec sp_configure xp_cmdshell,0
Reconfigure with override
 
 
--------------------------------------
--备份压缩
--------------------------------------
Backup Database AdventureWorks
To disk=h:\backup\adv不压缩备份.bak
--132MB  花费 7.789 秒(16.877 MB/秒)。
 
--备份到NTFS目录
Backup Database AdventureWorks
To disk=H:\backup\test\advNTFS压缩备份.bak
--60MB     花费 11.871 秒(11.073 MB/秒)。
 
Backup Database AdventureWorks
To disk=h:\backup\adv压缩备份.bak
With Compression
--132MB  花费 7.789 秒(16.877 MB/秒)。
--34MB    花费 3.775 秒(34.820 MB/秒)。
 
--启动默认备份压缩
EXEC sp_configure backup compression default, 1
RECONFIGURE WITH OVERRIDE
GO
View Code

以上文章来源于:http://bbs.csdn.net/topics/350183266

 

以下是自己编写自动生成文件名备份数据库:

技术分享
DECLARE @filename VARCHAR(255)
DECLARE @date DATETIME
SELECT @date=GETDATE()
SELECT @filename = D:\BackUp\SQL-+CAST(DATEPART(yyyy,@date) as varchar)
+-+CAST(DATEPART(mm,@date) as varchar)+-+CAST(DATEPART(dd,@date) as varchar)+.bak
BACKUP DATABASE [backupname] TO  DISK = @filename WITH INIT
GO
View Code


以下是删除数据库:

技术分享
DECLARE @filename VARCHAR(255)
DECLARE @date DATETIME
SELECT @date=GETDATE()
SELECT @filename = ‘‘D:\BackUp\SQL-‘‘+CAST(DATEPART(yyyy,@date) as varchar)
+‘‘-‘‘+CAST(DATEPART(mm,@date) as varchar)+‘‘-‘‘+CAST(DATEPART(dd,@date) as varchar)+‘‘.bak‘‘
BACKUP DATABASE 【backupname】TO  DISK = @filename WITH INIT
GO
View Code

 

数据库备份

标签:

原文地址:http://www.cnblogs.com/youchim/p/5484742.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!