SQLServer備份指令碼

來源:互聯網
上載者:User

標籤:disk   group   data   t-sql   tac   upd   分離   trunc   pdb   

企業管理器
--管理
--SQL Server代理
--右鍵作業
--新增作業
--"常規"項中輸入作業名稱
--"步驟"項
--建立
--"步驟名"中輸入步驟名
--"類型"中選擇"Transact-SQL 指令碼(TSQL)"
--"資料庫"選擇執行命令的資料庫
--"命令"中輸入要執行的語句:
                      
--確定
--"調度"項
--建立調度
--"名稱"中輸入調度名稱
--"調度類型"中選擇你的作業執行安排
--如果選擇"反覆出現"
--點"更改"來設定你的時間安排為一天一次


然後將SQL Agent服務啟動,並設定為自動啟動,否則你的作業不會被執行

設定方法:
我的電腦--控制台--管理工具--服務--右鍵 SQLSERVERAGENT--屬性--啟動類型--選擇"自動啟動"--確定.

 

/*******************完整備份作業*******************/
--完整備份,每周一次
USE Master
GO
declare @str varchar(100)
set @str=‘D:\DBtext\jgj\DBABak\FullBak‘+replace(replace(replace(convert(varchar,getdate(),20),‘-‘,‘‘),‘ ‘,‘‘),‘:‘,‘‘)+‘.bak‘
BACKUP DATABASE [demo] TO [email protected]
WITH RETAINDAYS=15,NOFORMAT,NOINIT,
NAME=N‘Demo完整備份‘,SKIP,NOREWIND,
NOUNLOAD,STATS=10
GO

 


/*******************差異備份作業*******************/
--截斷日誌
USE Master
GO
BACKUP LOG Demo  WITH NO_LOG
GO
--收縮記錄檔
USE Demo
GO
DBCC SHRINKFILE (N‘Demo_log‘,0,TRUNCATEONLY)
GO
--差異備份,每天一次
USE Master
GO
declare @str varchar(100)
set @str=‘D:\DBtext\jgj\DBABak\DiffBak‘+replace(replace(replace(convert(varchar,getdate(),20),‘-‘,‘‘),‘ ‘,‘‘),‘:‘,‘‘)+‘.diff‘
BACKUP DATABASE [Demo] TO [email protected]
WITH DIFFERENTIAL,RETAINDAYS=8,NOFORMAT,NOINIT,
NAME=N‘Demo差異備份‘,SKIP,NOREWIND,
NOUNLOAD,STATS=10
GO

 


/******************記錄備份作業*******************/
--記錄備份,每小時一次
USE Demo
GO
declare @str varchar(100)
set @str=‘D:\DBtext\jgj\DBABak\logbak‘+replace(replace(replace(convert(varchar,getdate(),20),‘-‘,‘‘),‘ ‘,‘‘),‘:‘,‘‘)+‘.trn‘
BACKUP LOG [Demo] TO [email protected]
WITH RETAINDAYS=3,NOFORMAT,NOINIT,
NAME=N‘Demo記錄備份‘,SKIP,NOREWIND,
NOUNLOAD,STATS=10
GO

 


--刪除到期的備份檔案,每天兩次
declare @str varchar(100),@dir varchar(100),@fileName varchar(30)
set @dir=‘del D:\DBtext\jgj\DBABak\‘
set @filename=left(replace(replace(replace(convert(varchar,getdate()-15,20),‘-‘,‘‘),‘ ‘,‘‘),‘:‘,‘‘),8)
set @[email protected]+‘fullbak‘[email protected]+‘*.bak‘
exec xp_cmdshell @str
set @filename=left(replace(replace(replace(convert(varchar,getdate()-8,20),‘-‘,‘‘),‘ ‘,‘‘),‘:‘,‘‘),8)
set @[email protected]+‘diffbak‘[email protected]+‘*.diff‘
exec xp_cmdshell @str
set @filename=left(replace(replace(replace(convert(varchar,getdate()-8,20),‘-‘,‘‘),‘ ‘,‘‘),‘:‘,‘‘),8)
set @[email protected]+‘logbak‘[email protected]+‘*.trn‘
exec xp_cmdshell @str

 

====================================================================== 
SQL還原 
====================================================================== 
1、驗證備份 
------------------------------------------------------------ 
restore headeronly from bak3 
restore filelistonly from bak3 with file=1 
restore labelonly from bak3 
restore verifyonly from bak3 
---------------------------------------------------------------------- 
2、從備份中還原 
------------------------------------------------------------------------- 
restore headeronly from bak1 
restore database d1 from bak1 with file=2        --從完全備份中恢複 
---------------------------------------------------------------------- 
restore headeronly from bak2              --從差異備份中恢複 
restore database d2 from bak2 with file=1,norecovery    
restore database d2 from bak2 with file=5,recovery 
---------------------------------------------------------------------- 
restore headeronly from bak3              --從記錄備份中恢複 
restore database d3 from bak3 with file=1,norecovery 
restore log    d3 from bak3 with file=2,norecovery 
restore log    d3 from bak3 with file=3,norecovery 
restore log    d3 from bak3 with file=4,norecovery 
restore log    d3 from bak3 with file=5,recovery 
---------------------------------------------------------------------- 
restore database d3 from bak3 with file=1,norecovery      --恢複到指定時間 
restore log    d3 from bak3 with file=2,norecovery 
restore log    d3 from bak3 with file=3,norecovery 
restore log    d3 from bak3 with file=4,recovery,stopat=‘2003-08-15 11:29:00.000‘ 
---------------------------------------------------------------------- 
restore database d5 filegroup=‘FG2‘ from bak5 with file=4,norecovery --還原檔案組備份 
restore log d5 from bak5 with file=5,norecovery 
restore log d5 from bak5 with file=7,recovery 
---------------------------------------------------------------------- 
restore headeronly from bak6                --還原檔案備份 
restore database d5 file=‘d5_data3‘ from bak6 with file=6,norecovery 
restore log d5 from bak6 with file=7,norecovery 
restore log d5 from bak6 with file=9,recovery 
---------------------------------------------------------------------- 
restore database d5 from bak6 with replace    --刪除現有資料庫,從備份中重建資料庫 
---------------------------------------------------------------------- 
create database d6            --move to將資料庫檔案移動到新位置 
on primary 
(name=d6_data, 
filename=‘E:\Program Files\Microsoft SQL Server\MSSQL\data\d6_Data.MDF‘, 
size=2MB) 
log on 
(name=d6_log, 
filename=‘E:\Program Files\Microsoft SQL Server\MSSQL\data\d6_log.ldf‘, 
size=2MB) 
go 
backupdatabase d6 to bak6 with init 
drop database d6 
restore database d6 from bak6 
with move ‘d6_data‘ to ‘e:\data\d6\d6_data.mdf‘, 
move ‘d6_log‘to ‘e:\data\d6\d6_log.ldf‘ 
sp_helpdb d6 
---------------------------------------------------------------------- 
3、分離與重串連資料庫 
-------------------------------------- 
sp_detach_db ‘d6‘        
sp_attach_db ‘d6‘,‘e:\data\d6\d6_data.mdf‘,‘e:\data\d6\d6_log.ldf‘ 
-------------------------------------- 
sp_detach_db d6 
go 
create database d6 
on primary 
(filename=‘e:\data\d6\d6_data.mdf‘) 
for attach 
go 
---------------------------------------------------------------------- 
4、恢複損壞的系統資料庫 
---------------------------------------------------------------------- 
1)先備份MASTER、MSDB 
2)停止SQL服務,將MASTER資料庫檔案刪除或者重新命名。這樣,SQL服務將不能啟動。 
3)系統資料庫的還原 
----------------------------------------------- 
(1)如果SQL服務還能啟動,則從備份中恢複系統資料庫。 
(2)如果SQL服務不能啟動,則需要重建系統資料庫。 
使用SQL檔案夾TOOLS\BINN目錄下的Rebuildm.exe重建master資料庫。 
(3)建立備份裝置,指向以前的備份裝置。 
(4)以單一使用者模式啟動SQL 
cd programe files\microsoft sql server\mssql\binn 
sqlservr.exe -c -m 
(5)進查詢分析器,從備份中恢複master資料庫。 
restore database master from masterbak 
restore database msdb from disk=‘e:\bak\msdb.bak‘ 
MASTER還原後,SQL中使用者資料庫的資訊也會恢複。 
(6)如果MASTER沒有備份,則需要用sp_attach_db命令將使用者資料庫附加到新的MASTER資料庫中。

  

SQLServer備份指令碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.