zoukankan      html  css  js  c++  java
  • 批处理(bat)实现SQLServer数据库备份与还原

    --1.数据库备份脚本sqlserverbackup.sql

     

    -- SQLServer2000数据库系统清理用户数据库日志和备份用户数据库数据脚本

     

    declare @dumpfile varchar(50)

    declare @msg varchar(70)

       select @dumpfile = 'd:\backup\north' + datename(dw,getdate())+'.bak'

       select @msg=convert(char(26),getdate(),9)+'-----正在清理日志......'

       print @msg

    backup tran northwind with truncate_only

    if (@@ERROR <> 0 )

    begin

       select @msg=convert(char(26),getdate(),9)+'-----清理日志失败或出现异常......'

       print @msg

    end

    else

    begin

       select @msg=convert(char(26),getdate(),9)+'-----清理日志完毕......'

       print @msg

    end

       select @msg=convert(char(26),getdate(),9)+'-----开始备份northwind数据库.....'

       print @msg

    backup database northwind to disk=@dumpfile

    if (@@ERROR <> 0 )

    begin

       select @msg=convert(char(26),getdate(),9)+'-----备份数据失败或出现异常'

       print @msg

    end

    else

    begin

       select @msg=convert(char(26),getdate(),9)+'-----数据库备份完毕'

       print @msg

    end

     

     

    --2.数据库还原脚本sqlserverrestore.sql

     

    declare @dumpfile varchar(50)

    declare @msg varchar(70)

       select @dumpfile = 'd:\backup\north' + datename(dw,getdate())+'.bak'

       select @msg=convert(char(26),getdate(),9)

       print @msg

     

    restore database northwind from disk=@dumpfile

    if (@@ERROR <> 0 )

    begin

       select @msg=convert(char(26),getdate(),9)+'-----还原数据失败或出现异常'

       print @msg

    end

    else

    begin

       select @msg=convert(char(26),getdate(),9)+'-----数据库还原完毕'

       print @msg

    end

     

    --将下面的脚本保存为备份数据库.bat

     

    @net start "mssqlserver"

    @isql -Usa -Padmin -i d:\backup\sqlserverbackup.sql -o d:\backup\sqlserverbackup.out

    @echo ……正在备份中……

    @pause

     

    --将下面的脚本保存为:还原数据库.bat

     

    @echo ******开始还原数据库*****

    @net start "mssqlserver"

    @isql -Usa -Psa -i d:\backup\sqlserverrestore.sql -o d:\backup\sqlserverrestore.out

    @pause


    http://juge001.blog.163.com/blog/static/63617809201011645733472/

  • 相关阅读:
    Python_Day3
    Python_Day2
    动漫推荐3.0 杂谈
    动漫推荐2.0 杂谈
    动漫推荐1.0 剧情向
    西湖十大特产
    一到春天 杭州西湖就美成了一幅画
    机械键盘十大品牌排行榜
    键盘的日常维护及清理
    无线键盘
  • 原文地址:https://www.cnblogs.com/y0umer/p/3839140.html
Copyright © 2011-2022 走看看