数据库备份和恢复

                            这个备份问题我以前也碰到过,也尝试了好久和好多方法,包括大家写的相类似的存储过

程,不过我到后来发觉还是自己的最快最简洁。对于SQL数据库来说,一定要独占方式下才

能恢复,哪怕你用相同的用户在其他客户机上同时登录也不行。你可以用系统存储过程检

查谁已登录或KILL他们。但是,对于备份本身这个动作,一定要独占方式,而且是use 

master 而不是use mydb,明白这一点就会发现就是这么简单。



create  proc  killspid  (@dbname  varchar(20))  
as  
begin  
declare  @sql  nvarchar(500)  
declare  @spid  int  
set  @sql='declare  getspid  cursor  for    
select  spid  from  sysprocesses  where  dbid=db_id('''+@dbname+''')'  
exec  (@sql)  
open  getspid  
fetch  next  from  getspid  into  @spid  
while  @@fetch_status  <  >-1  
begin  
exec('kill  '+@spid)  
fetch  next  from  getspid  into  @spid  
end  
close  getspid  
deallocate  getspid  
end  

--用法  
use  master  
exec  killspid  '数据库名'




解压
 VCLUnZip1.ZipName := FileName;//文件名
 VCLUnZip1.DestDir := FApplicationPath;//解压路径
 //vclUNZip1.Password := aPassword;
 VCLUnZip1.RecreateDirs := True;//是否创建子目录
 VCLUnZip1.DoAll := True;
 VCLUnZip1.OverwriteMode := always; //是否覆盖
 try
   VCLUnZip1.UnZip;


来自:不知道_0909, 时间:2003-9-9 16:44:00, ID:2167101 
压缩
with FVCLZip do
 begin
   Password := aZipPass;
   FilesList.Add(aSourceFile); //带有绝对路径和通配符的字符串
   RelativePaths := True; // 保存相对路径信息
   Recurse := True;    // 遍历子目录
   ZipName := aDestFile;  // 目标文件
   Result := Zip;      // 返回多少个文件被压缩
 end



数据库备份(完全)
BACKUP DATABASE [GrBookF] TO  DISK = N'C:\test' WITH  INIT ,  NOUNLOAD ,  NAME 

= N'GrBookF 备份',  NOSKIP ,  STATS = 10,  NOFORMAT 
数据库校验备份
RESTORE VERIFYONLY FROM  DISK = N'C:\test' WITH  FILE = 1 ,  NOUNLOAD 
数据库还原模型
use master
ALTER DATABASE [GrBookF] SET RECOVERY SIMPLE ;
ALTER DATABASE [GrBookF] SET RECOVERY FULL ;
数据事务日志备份
BACKUP LOG [GrBookF] TO  DISK = N'C:\test' WITH  NOINIT ,  NOUNLOAD ,  NAME = 

N'GrBookF 备份',  NOSKIP ,  STATS = 10,  NOFORMAT ,  NO_TRUNCATE 
BACKUP LOG [GrBookF] TO  DISK = N'C:\test' WITH  NOINIT ,  NOUNLOAD ,  NAME = 

N'GrBookF 备份',  NOSKIP ,  STATS = 10,  NOFORMAT 

数据库的恢复
RESTORE DATABASE [GrBookF] FROM  DISK = N'C:\test' WITH  FILE = 1,  NOUNLOAD ,  

STATS = 10,  NORECOVERY 

RESTORE LOG [GrBookF] FROM  DISK = N'C:\test' WITH  FILE = 2,  NOUNLOAD ,  

STATS = 10,  NORECOVERY 

RESTORE LOG [GrBookF] FROM  DISK = N'C:\test' WITH  FILE = 3,  NOUNLOAD ,  

STATS = 10,  NORECOVERY 




select * from msdb..backupset where backup_set_id = 4 可以得到备份记录数等等。 


                                

查看回复