备份与恢复
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