• MS SQL Server备份与恢复实例


    备份与恢复
    1. 备份策略
    2. 操作系统级备份:
    让数据库脱机
    USE master
    GO
    ALTER DATABASE [MySQLServer]
        SET OFFLINE
    GO

    让数据库联机
    USE master
    GO
    ALTER DATABASE [MySQLServer]
        SET ONLINE
    GO

    3. 数据库全备份
    BACKUP DATABASE [MySQLServer]
      TO  DISK = N'c:Program FilesMicrosoft SQL ServerMSSQL.1MSSQLBackupMySQLServer.bak'
      WITH  DESCRIPTION = N'2008-03-11',
      NOFORMAT, INIT,  
      NAME = N'MySQLServer-Full Database Backup',
      SKIP, NOREWIND, NOUNLOAD,  STATS = 10
    GO
    declare @backupSetId as int
    select @backupSetId = position
      from msdb..backupset
      where database_name=N'MySQLServer'
        and backup_set_id=
          (select max(backup_set_id) from msdb..backupset where database_name=N'MySQLServer' )

    if @backupSetId is null
    begin
      raiserror(N'Verify failed. Backup information for database ''MySQLServer'' not found.', 16, 1)
    end

    RESTORE VERIFYONLY
      FROM  DISK = N'c:Program FilesMicrosoft SQL ServerMSSQL.1MSSQLBackupMySQLServer.bak'
      WITH  FILE = @backupSetId,  NOUNLOAD,  NOREWIND
    GO

    4. 数据库增量备份

    BACKUP DATABASE [MySQLServer]
      TO  DISK = N'c:Program FilesMicrosoft SQL ServerMSSQL.1MSSQLBackupMySQLServer.bak'
      WITH  DIFFERENTIAL , NOFORMAT, NOINIT,  
      NAME = N'MySQLServer-Differential Database Backup',
      SKIP, NOREWIND, NOUNLOAD,  STATS = 10
    GO

    declare @backupSetId as int
    select @backupSetId = position from msdb..backupset where database_name=N'MySQLServer' and backup_set_id=(select max(backup_set_id) from msdb..backupset where database_name=N'MySQLServer' )
    if @backupSetId is null begin raiserror(N'Verify failed. Backup information for database ''MySQLServer'' not found.', 16, 1) end
    RESTORE VERIFYONLY FROM  DISK = N'c:Program FilesMicrosoft SQL ServerMSSQL.1MSSQLBackupMySQLServer.bak' WITH  FILE = @backupSetId,  NOUNLOAD,  NOREWIND
    GO

    5. 事务日志备份

    BACKUP LOG [MySQLServer]
      TO  DISK = N'c:Program FilesMicrosoft SQL ServerMSSQL.1MSSQLBackupMySQLServer.bak'
      WITH NOFORMAT, NOINIT,  NAME = N'MySQLServer-Transaction Log  Backup',
      SKIP, NOREWIND, NOUNLOAD,  STATS = 10
    GO

    6. 数据库恢复:
    RESTORE DATABASE [MySQLServer]
      FROM  DISK = N'c:Program FilesMicrosoft SQL ServerMSSQL.1MSSQLBackupMySQLServer.bak'
      WITH  FILE = 6,  NOUNLOAD,  STATS = 10
    GO

    RESTORE DATABASE [MySQLServer]
      FROM  DISK = N'c:Program FilesMicrosoft SQL ServerMSSQL.1MSSQLBackupMySQLServer.bak'
      WITH  RESTRICTED_USER,  FILE = 6,  
        MOVE N'MySQLServer_PRI01' TO N'E:MySQL_PRI01.mdf',  
        MOVE N'MySQLServer_log' TO N'E:MySQL2_Log.ldf',  
        MOVE N'MySQLServer_SEC01' TO N'E:MySQL2_SEC01.ndf',  
        MOVE N'MySQLServer_SEC02' TO N'E:MySQL2_SEC02.ndf',  
      KEEP_REPLICATION,  NOUNLOAD,  REPLACE,  STATS = 10
    GO

    7. 分离与附加
    例:数据库分离:
    USE [master]
    GO
    EXEC master.dbo.sp_detach_db @dbname = N'MySQLServer', @keepfulltextindexfile=N'true'
    GO

    例:数据库附加:
    CREATE DATABASE MySQLServer2
    ON ( FILENAME = 'C:Program FilesMicrosoft SQL ServerMSSQLDataMySQLServer2.MDF' )
    FOR ATTACH
    GO

    见:http://space.itpub.net/7705285/viewspace-417559

    作者:行走的贝壳

    来源贝壳笔记 (http://ronli.cnblogs.com)

  • 相关阅读:
    代码面试最常用的10大算法
    ant google compiler 压缩
    美工资源
    面试题
    validate表单验证插件
    laypage分页
    layer弹出框小结
    Thymeleaf
    webApp开发
    grunt自动化构建工具
  • 原文地址:https://www.cnblogs.com/ronli/p/SqlServerBackup.html
Copyright © 2020-2023  润新知