SQL語句備份和還原數據庫

1,使用SQL最簡單備份,還原數據庫

 

  1. /* 備份 */
  2. backup database Test to disk='D:/Test.bak'
  3. /* 還原 */
  4. restore database Test from disk='D:/Test.bak'

2,爲了方便以後的使用,開始對語句進行簡單的封裝->存儲過程

    1,備份

  1. /*******************************************************
  2.     備份數據庫
  3. *******************************************************/
  4. if exists(select 1 from sys.procedures where name='sp_BackupDB')
  5.     drop procedure sp_BackupDB
  6. go
  7. create procedure sp_BackupDB
  8.     @savePath nvarchar(4000) -- 備份數據庫保存位置(目錄)   
  9.     ,@dbName nvarchar(4000) -- 需要進行備份的數據庫
  10.     ,@bakName nvarchar(4000) -- 備份文件的名稱(不含擴展名)
  11. as begin
  12.     declare @sql nvarchar(4000)
  13.     /* 驗證路徑 */
  14.     if(charindex('/',reverse(@savePath))!=1) begin
  15.         set @savePath=@savePath+'/'
  16.     end
  17.     /* 拼SQL並執行 */
  18.     set @sql='backup database '+@dbName+' to disk='''+@savePath+@bakName+'.bak'''
  19.     exec sp_executesql @sql
  20.     
  21.     /* 返回執行結果(1=成功,0=失敗) */
  22.     if(@@error=0) begin
  23.         return 1
  24.     end
  25.     return 0
  26. end

    2,還原

  1. /*******************************************************
  2.     還原數據庫
  3. *******************************************************/
  4. if exists(select 1 from sys.procedures where name='sp_RestoreDB')
  5.     drop procedure sp_RestoreDB
  6. go
  7. create procedure sp_RestoreDB
  8.     /* 數據庫還原後的保存位置(目錄)(使用系統默認保存位置:-1) */
  9.     @savePath nvarchar(4000)
  10.     ,@backFile nvarchar(4000) -- 需要還原的數據庫備份文件
  11.     ,@defaultName nvarchar(4000) -- 數據庫原始名稱(備份的原數據庫名稱)不包含擴展名
  12.     /* 爲數據庫重命名(使用數據庫默認名稱:-1)不包含擴展名
  13.         如果目錄已存在該名稱的數據庫,將會被覆蓋 */
  14.     ,@dbName nvarchar(4000)
  15. as begin
  16.     declare @newName nvarchar(4000),@sql nvarchar(4000)
  17.     /* 獲取數據庫名稱 */
  18.     if(@dbName='-1') begin
  19.         set @newName=@defaultName
  20.     end else begin
  21.         set @newName=@dbName
  22.     end
  23.     /* 結束所有對當前數據庫的連接 */
  24.     if exists(select 1 from sys.sysprocesses where dbid=db_id(@defaultName)) begin
  25.         declare #cs_spid cursor -- 聲明遊標
  26.         for
  27.         select #cs_spid=convert(varchar,spid) from sys.sysprocesses where dbid=db_id(@defaultName)
  28.         open #cs_spid
  29.             declare @spid varchar(20)
  30.             fetch next from #cs_spid into @spid -- 賦值並前進到下一條
  31.             while(@@fetch_status=0) begin -- 在fetch失敗前執行
  32.                 exec ('kill '+@spid) -- 結束對操作庫的連接(exec執行SQL語句1)
  33.                 fetch next from #cs_spid into @spid
  34.             end
  35.         close #cs_spid
  36.         deallocate #cs_spid -- 釋放遊標
  37.     end
  38.     /* 創建執行語句   */
  39.     set @sql='restore database '+@newName+' from disk='''+@backFile+''' with replace'
  40.     if(@savePath!='-1') begin
  41.         -- 驗證路徑
  42.         if(charindex('/',reverse(@savePath))!=1) begin
  43.             set @savePath=@savePath+'/'
  44.         end
  45.         set @sql=@sql+', move '''+@defaultName+''' to '''+@savePath+@newName+'.mdf'''
  46.         set @sql=@sql+', move '''+@defaultName+'_log'' to '''+@savePath+@newName+'_log.ldf'''
  47.     end
  48.     /* 執行操作 */
  49.     exec sp_executesql @sql -- (exec執行SQL語句2)
  50.     /* 返回執行結果(1=成功,0=失敗) */
  51.     if(@@error=0) begin
  52.         return 1
  53.     end
  54.     return 0
  55. end

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章