备份与恢复
1. 备份策略
2. 操作系统级备份:
让数据库脱机
USE master
GO
ALTER DATABASE [MySQLServer]
SET OFFLINE
GO
让数据库联机
USE master
GO
ALTER DATABASE [MySQLServer]
SET ONLINE
GO
3. 数据库全备份
BACKUP DATABASE [MySQLServer]
TODISK = N'c:Program FilesMicrosoft SQL ServerMSSQL.1MSSQLBackupMySQLServer.bak'
WITHDESCRIPTION = 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
FROMDISK = N'c:Program FilesMicrosoft SQL ServerMSSQL.1MSSQLBackupMySQLServer.bak'
WITHFILE = @backupSetId,NOUNLOAD,NOREWIND
GO
4. 数据库增量备份
BACKUP DATABASE [MySQLServer]
TODISK = N'c:Program FilesMicrosoft SQL ServerMSSQL.1MSSQLBackupMySQLServer.bak'
WITHDIFFERENTIAL , 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 FROMDISK = N'c:Program FilesMicrosoft SQL ServerMSSQL.1MSSQLBackupMySQLServer.bak' WITHFILE = @backupSetId,NOUNLOAD,NOREWIND
GO
5. 事务日志备份
BACKUP LOG [MySQLServer]
TODISK = N'c:Program FilesMicrosoft SQL ServerMSSQL.1MSSQLBackupMySQLServer.bak'
WITH NOFORMAT, NOINIT,NAME = N'MySQLServer-Transaction LogBackup',
SKIP, NOREWIND, NOUNLOAD,STATS = 10
GO
6. 数据库恢复:
RESTORE DATABASE [MySQLServer]
FROMDISK = N'c:Program FilesMicrosoft SQL ServerMSSQL.1MSSQLBackupMySQLServer.bak'
WITHFILE = 6,NOUNLOAD,STATS = 10
GO
RESTORE DATABASE [MySQLServer]
FROMDISK = N'c:Program FilesMicrosoft SQL ServerMSSQL.1MSSQLBackupMySQLServer.bak'
WITHRESTRICTED_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
MS SQL Server:备份与恢复实例(命令行)
最新推荐文章于 2023-09-11 20:02:43 发布