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
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_backupdb]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)drop procedure [dbo].[p_backupdb]GO/*--备份数据库的通用存储过程--邹建 2003.10(引用时请保留此信息)--*//*--调用示例--备份当前数据库exec p_backupdb @bkpath='c:\',@bkfname='\DBNAME\_\DATE\_db.bak'--差异备份当前数据库exec p_backupdb @bkpath='c:\',@bkfname='db_\DATE\_df.bak',@bktype='DF'--备份当前数据库日志exec p_backupdb @bkpath='c:\',@bkfname='db_\DATE\_log.bak',@bktype='LOG'--*/create proc p_backupdb@dbname sysname='', --要备份的数据库名称,不指定则备份当前数据库@bkpath nvarchar(260)='', --备份文件的存放目录,不指定则使用SQL默认的备份目录@bkfname nvarchar(260)='', --备份文件名,文件名中可以用\DBNAME\代表数据库名,\DATE\代表日期,\TIME\代表时间@bktype nvarchar(10)='DB', --备份类型:'DB'备份数据库,'DF' 差异备份,'LOG' 日志备份@appendfile bit=1, --追加/覆盖备份文件@password nvarchar(20)='' --为备份文件设置的密码(仅sql2000支持),设置后,恢复时必须提供此密码as declare @sql varchar(8000) if isnull(@dbname,'')='' set @dbname=db_name() if isnull(@bkpath,'')='' begin select @bkpath=rtrim(reverse(filename)) from master..sysfiles where name='master' select @bkpath=substring(@bkpath,charindex('\',@bkpath)+1,4000) ,@bkpath=reverse(substring(@bkpath,charindex('\',@bkpath),4000))+'BACKUP\' end if isnull(@bkfname,'')='' set @bkfname='\DBNAME\_\DATE\_\TIME\.BAK' set @bkfname=replace(replace(replace(@bkfname,'\DBNAME\',@dbname) ,'\DATE\',convert(varchar,getdate(),112)) ,'\TIME\',replace(convert(varchar,getdate(),108),':','')) set @sql='backup '+case @bktype when 'LOG' then 'log ' else 'database ' end +@dbname +' to disk='''+@bkpath+@bkfname +''' with '+case @bktype when 'DF' then 'DIFFERENTIAL,' else '' end +case @appendfile when 1 then 'NOINIT' else 'INIT' end +case isnull(@password,'') when '' then '' else ',PASSWORD='''+@password+'''' end exec(@sql)go
[解决办法]
是原创吗,呵呵 不过看到一个问题