SQL server 数据库备份大全(SQL语句)

首先简单的介绍一下Sql server 备份的类型有:1:完整备份(所有的数据文件和部分的事务日志文件)2:差异备份(最后一次完成备份后数据库改变的部分)3:文件和文件组备份(对指定的文件和文件组备份)4:事物日志备份(所有数据库的变更)5:尾日期备份(日志的活动部分,指上一次为备份的日志部分)6:部分备份(主文件组、每个可读可写文件组和指定的只读文件组)7:仅复制备份(数据库或者日志的备份,不影响整体备份)

--------------------完整备份默认追加到现有的文件---------------
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'
 
-----事物日志备份,会自动截断日志(默认会阶段日志)
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


--从远程备份至本地
USE HL7
EXEC sp_configure 'show advanced options', 1RECONFIGURE WITH OVERRIDE
EXEC sp_configure 'xp_cmdshell', 1RECONFIGURE WITH OVERRIDE
EXEC sp_configure   'show advanced options', 0RECONFIGURE WITH OVERRIDE
--2、使用变量进行备份
declare 
        @db sysname
declare 
        @dbpath varchar(120)
        set @db='HL7'
--文件名根据数据库名称+日期YYYYMMDD+扩展名
        set @dbpath='\\10.0.0.6\Users\RJYX\Documents\文件共享\'+@db+convert(varchar(10),getdate(),112)+'.bak'

--3、在sql中建个映射
exec master..xp_cmdshell 'net use Y: \\10.0.0.6\Users\RJYX\Documents\文件共享\ "123987" /user:10.0.0.6\guohe'
--exec master..xp_cmdshell 'net use Y: \\本机ip\databasebak "本机登陆密码" /user:本机ip\本机登陆用户名'
/* 说明: 
Y:是映射网络路径对应本机的盘符 
请修改【本机ip】为实际ip 
databasebak是我本机共享的文件夹 
【本机登陆用户名】是登陆的用户名  
"本机登陆密码" 是【本机登陆用户名】用户的密码,注意引号是英文半角*/

--4、备份(@db是要备份的数据库名称)
Backup Database @db To disk=@dbpath
--5、COPY
declare 
@copypath varchar(200)set 
@copypath = 'copy '+@dbpath+' Y:'
select @copypath
exec master..xp_cmdshell @copypath 
--6、删除(这句可以去掉)
declare 
@deletepath varchar(200)
set @deletepath = 'del '+@dbpath
select @deletepath
exec master..xp_cmdshell @deletepath 
--7、完成后删除映射
exec master..xp_cmdshell 'net use Y: /delete' 
--8、关闭xp_cmdshell
USE HL7 
EXEC sp_configure 'show advanced options', 1 
RECONFIGURE WITH OVERRIDE 
EXEC sp_configure 'xp_cmdshell', 0 
RECONFIGURE WITH OVERRIDE 
EXEC sp_configure   'show advanced options', 0
RECONFIGURE WITH OVERRIDE
    原文作者:Asa_Guo
    原文地址: https://www.jianshu.com/p/b4149913d243
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞