/*******************************************打开/关闭数据库日志****************************************/
if @work='log_trun_off'
begin
exec sp_dboption @db_name,'trunc. log on chkpt.','TRUE' --关闭日志
return 0
end
if @work='log_trun_on'
begin
exec sp_dboption @db_name,'trunc. log on chkpt.','FALSE' --打开日志
return 0
end
/*******************************************删除日志文件****************************************/
if @work='drop_log'
begin
if exists (select * from sysobjects where name='tmp_f' )
drop table tmp_f
exec ('select * into tmp_f from '+@db_name+'.dbo.sysfiles')
-----------------------------------------------------------------------------------------
exec master.dbo.sp_dropconn @db_name --断开连接
----------------------------------------------------------------------------------------------
exec sp_detach_db @db_name
declare tmp_f2 cursor for select replace(filename,' ','') from tmp_f order by fileid desc
open tmp_f2
fetch next from tmp_f2 into @logical_file
while @@fetch_status=0
begin
if @logical_file like '%.ldf%' --日志文件,删除
exec ('xp_cmdshell ''del "'+@logical_file+'"''')
else
exec sp_attach_single_file_db @db_name,@logical_file --数据文件,附加
fetch next from tmp_f2 into @logical_file
end
close tmp_f2
deallocate tmp_f2
return 0
end
/*******************************************删除备份**************+'\bak_'+@db_name**************************/
if @work='drop_bak'
begin
set @other=replace(@other,' ','')
if @other='' or isnumeric(@other)<>1
begin
raiserror (577056500,16,1) --没有数据库名,或不是有效的数字
return 0
end
declare tmp_file cursor for select item from #tmp_bak where isfile=1 and item like 'bak%'
open tmp_file
fetch next from tmp_file into @logical_file
while @@fetch_status=0
begin
set @tmp=left(replace(@logical_file, 'bak_'+@db_name,''),8)
if datediff(dd,convert(datetime,@tmp),getdate())>convert(int,@other)
begin
set @execsour='del "'+@file_path+'\'+@logical_file+'"'
set @execsour='del '+replace(@execsour,'\\','\')
exec xp_cmdshell @execsour
end
fetch next from tmp_file into @logical_file
end
close tmp_file
deallocate tmp_file
return 0
end
/*******************************************删除备份完****************************************/
/*******************************************备份数据库日志****************************************/
if @work='log_bak'
begin
if @File_Path =''
begin
RAISERROR (577056501, 16, 1) --没有备份路径
return
end
set @execsour=@file_path
if @other<>'' --指定了文件名
set @execsour='backup log '+@db_name+' to disk =''' +@execsour+'\'+@other+''''
else --自动的文件名
set @execsour='backup log '+@db_name+' to disk =''' +@execsour+'\bak_'+@db_name+@date+'.log.bak'''
/*******************************************建立数据库****************************************/
if @work='make_db'
begin
if @File_Path =''
begin
RAISERROR (577056504, 16, 1) --没有备份路径
return 0
end
SET @EXECSOUR=
'
create database '+@DB_NAME
+' ON '+
'(NAME='+@DB_NAME+
', FILENAME='''+@FILE_PATH+'\'+@DB_NAME+'.MDF'',
size=1,
FILEGROWTH = 10MB
)'
set @execsour=replace(@execsour,'\\','\')
exec (@EXECSOUR)
return 0
end
/*******************************************更改sa密码****************************************/
if @work='pass'
begin
exec sp_password null,@file_path,'sa'
return 0
end
/****************************************备份数据库************************************************/
if @work='backup'
begin
if @File_Path =''
begin
RAISERROR (577056501, 16, 1) --没有备份路径
return 0
end
set @execsour=@file_path
if @other<>'' --指定了文件名
set @execsour='backup database '+@db_name+' to disk =''' +@execsour+'\'+@other+''''
else --自动的文件名
set @execsour='backup database '+@db_name+' to disk =''' +@execsour+'\bak_'+@db_name+@date+'.bak'''
set @execsour=replace(@execsour,'\\','\')
exec (@execsour)
return 0
end
--备份
/*******************************************备份数据库完****************************************/
/*******************************************移动数据库****************************************/
if @work='move'
begin
if exists (select * from sysobjects where name='tmp_f' )
drop table tmp_f
exec ('select * into tmp_f from '+@db_name+'.dbo.sysfiles')
-----------------------------------------------------------------------------------------
exec master.dbo.sp_dropconn @db_name --断开连接
----------------------------------------------------------------------------------------------
exec sp_detach_db @db_name
set @execsour='exec master..sp_attach_db '+''''+@db_name+''''
declare tmp_f cursor for select replace(filename,' ','') from tmp_f
open tmp_f
fetch next from tmp_f into @logical_file
while @@fetch_status=0
begin
exec ('xp_cmdshell ''move "'+@logical_file+'" "' +@file_path+'"''')
--set @logical_file=replace(@logical_file,' ','')
while charindex('\',@logical_file)<>0
begin
set @logical_file=right(@logical_file,len(@logical_file)-charindex('\',@logical_file))
print @logical_file
end
set @logical_file=@file_path+'\'+@logical_file
set @execsour=@execsour+','''+@logical_file+''''
fetch next from tmp_f into @logical_file
end
close tmp_f
deallocate tmp_f
set @execsour=replace(@execsour,'\\','\')
exec (@execsour)
return 0
end
/*******************************************还原数据库****************************************/
if @work='restore'
begin
-----------------------------------------------------------------------------------------
exec master.dbo.sp_dropconn @db_name --断开连接
----------------------------------------------------------------------------------------------
if @file_path=''
begin
RAISERROR (577056502, 16, 1) --没有备份文件路径
return
end
set @execsour='restore database '+@db_name+' from disk ='''+@file_path+''''
if @other <>'' and @other is not null
begin
create table #tmp_file (name varchar(255), PhysicalName varchar(255),type varchar(255),FileGroupName varchar(255),size bigint,maxsize bigint)
insert into #tmp_file
exec ('RESTORE FILELISTONLY FROM DISK = N'''+@file_path+''' WITH FILE = 1 , NOUNLOAD ' ) --取得逻辑文件名
declare tmp_1 cursor for select name from #tmp_file order by type
open tmp_1
fetch next from tmp_1 into @logical_file
set @execsour=@execsour+' with move '+''''+@logical_file+'''' +' to '+ ''''+@other+'\'+@logical_file+'.mdf'''
fetch next from tmp_1 into @logical_file
while @@fetch_status=0
begin
set @execsour=@execsour+ ','+' move '''+@logical_file+'''' +' to '+ ''''+@other+'\'+@logical_file+'.ldf'''
fetch next from tmp_1 into @logical_file
end
close tmp_1
deallocate tmp_1
end
set @execsour=replace(@execsour,'\\','\')
exec (@execsour)
return 0
end
我写了一个存储过程玩。。。。。
-------------------------------use master
go
sp_addmessage 577056500,'16','Sorry,Database NOT Foud','us_english' --未指定数据库名称
go
sp_addmessage 577056501,'16','Sorry,Path NOT found,can''t backup database','us_english' --无路径还原数据库
go
sp_addmessage 577056502,'16','Sorry,Path NOT found,can''t restore database','us_english' --无备份文件
go
sp_addmessage 577056503,'16','I DON''T KNOWN HOW OLD TO DELETE? ','us_english' --无删除范围
go
sp_addmessage 577056504,'16','Sorry,Path NOT found,can''t backup log','us_english' --无备份文件
go
sp_addmessage 577056599,'16','COMMAND NOT FOUND','us_english' --暂不提供本功能
go
if exists (select * from sysobjects where name='sp_dropconn')
drop procedure sp_dropconn
go
create PROCEDURE dbo.sp_dropconn
@db_name varchar(255)
AS
/*
断开特定数据库用户的连接
*/
declare @spid int
, @execsour varchar(8000)
---------------------------------------------------------------------------------------
create table #tmp_login
(spid int,ecid int,status varchar(255),loginname varchar(255),hostname varchar(255),blk varchar(255),dbname varchar(255),cmd varchar(255))
insert into #tmp_login exec master..sp_who
declare tmp_l cursor for select spid from #tmp_login where dbname=@db_name --使用该数据库的用户
open tmp_l
fetch next from tmp_l into @spid
while @@fetch_status=0
begin
set @execsour=convert(varchar(300),@spid)
set @execsour='kill '+@execsour
exec (@execsour ) --断开当前数据库连接
fetch next from tmp_l into @spid
end
close tmp_l
deallocate tmp_l
----------------------------------------------------------------------------------------------
set @logical_file=''
set @date=convert(varchar(20),getdate(),112)+'_'+replace(convert(varchar(20),getdate(),108),':','')
--print @date
if (@db_name='' or @db_name is null)and @work<>'pass'
begin
raiserror (577056500,16,1) --没有数据库名
return 0
end
------------------------------------------------------------
create table #file (isfile int,isdir int,father int)
if @make=1
begin --创建不存在的路径
declare @dir varchar(255) --当前操作路径
set @dir=@file_path
set @logical_file=left(@dir,charindex('\',@dir)-1)
set @dir=right(@dir,len(@dir)-charindex('\',@dir))
while 1>0
begin
if charindex('\',@dir)=0
begin
if len(@dir)=0
break
set @logical_file=@logical_file+'\'+@dir
set @dir=''
end
else
begin
set @logical_file=@logical_file+'\'+left(@dir,charindex('\',@dir)-1)
set @dir=right(@dir,len(@dir)-charindex('\',@dir))
end
insert into #file exec xp_fileexists @logical_file
if (select sum(isdir) from #file)=0 and right(@logical_file,1)<>':'
begin
set @execsour='xp_cmdshell ''md "'+@logical_file +'"'''
exec (@execsour)
end
delete from #file
end
end
----------------------------------------------------------------------
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_RestoreDb]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[p_RestoreDb]
GO
--生成数据恢复语句,将数据库恢复到SQL数据目录下
set @sql='restore database '+@redb
+' from disk='''+@bkfile+''''
+' with move '''+@logfn1+''' to '''
+@phyfn1+@redb+'.mdf'',move '''
+@logfn2+''' to '''+@phyfn1+@redb+'.ldf'''
+case @overexist when 1 then ',replace' else '' end
exec(@sql)
go
--创建备份当前数据库的存储过程
create proc p_backupdb
as
declare @sql varchar(8000)
set @sql='backup database '+db_name()+' to disk=''c:\'+db_name()
+convert(varchar,getdate(),112)+'.bak'''
exec(@sql)
go
DBCC CHECKDB(Northwind)
/***********************************************************
** CREATE BACKUP AND RESTORE DEVICES
************************************************************/
IF @RC=0
BEGIN
exec sp_addumpdevice 'disk', @DevName_log,@log_path
select @rc=@@error
IF @RC<>0
begin
EXEC SP_DropDevice @Devname_data
exec sp_dropdevice @devname_log
SELECT @RC=-1000
return @rc
end
END
IF @kind='backup'
BEGIN
IF @bakequip=0
BEGIN
IF @baktype=0
BEGIN
IF @bakdb=0
BEGIN
BACKUP DATABASE Northwind TO DISK=@Devname_data
WITH INIT
END
IF @baklog=0
BEGIN
BACKUP LOG Northwind WITH NO_LOG
BACKUP LOG Northwind TO DISK=@DevName_log
WITH INIT,NO_TRUNCATE
END
END
ELSE BEGIN
IF @bakdb=0
BEGIN
BACKUP DATABASE Northwind TO DISK=@DevName_data
WITH NOINIT
END
IF @baklog=0
BEGIN
BACKUP LOG Northwind WITH NO_LOG
BACKUP LOG Northwind TO DISK=@DevName_log
WITH NOINIT,NO_TRUNCATE
END
END
END
SELECT @retmsg='数据库备份成功!'
END
IF @kind='restore'
BEGIN
RESTORE DATABASE Northwind FROM DISK= @DevName_data WITH REPLACE
SELECT @retmsg='恢复数据库成功!'
END