Vous êtes sur la page 1sur 2

1 --script to set up stored procedure sp_dumpdb in database saptools

2 --version 1.0.2
3 --date 04 Oct 2012
4 --changes:
5 --1.0.1 fix issue that cause sp to not run in jobscheduler
6 --1.0.2 no change
7
8 use saptools
9 go
10 if not exists (select name from sysobjects where name = 'dump_history' and type = 'U')
11 begin
12 exec('create table dump_history (DBName VARCHAR(30), Type VARCHAR(10), Command
VARCHAR(512), StartTime DATETIME, EndTime DATETIME, RC INTEGER, Status CHAR(1))')
13 end
14 go
15 if exists (select name from sysobjects where name = 'sp_dumpdb' and type = 'P')
16 begin
17 drop procedure sp_dumpdb
18 end
19 go
20 create procedure sp_dumpdb (@sapdb_name varchar (256))
21 as
22 begin
23 declare @sqlstatus smallint
24 declare @sqlerror int
25 declare @curr_logdev varchar(1024)
26 declare @current_ts char(19)
27 declare @starttime datetime
28 declare @dumpcmd varchar(1024)
29 declare @sjobid int
30 declare @sec_past int
31 declare @backup_active int
32 declare @dbid smallint
33
34 commit
35 set chained on
36 set quoted_identifier on
37
38 set @sqlerror = 0
39 set @sqlstatus = 0
40
41 -- is it a valid DB name?
42 select @dbid = db_id(@sapdb_name)
43 if @dbid IS NULL
44 begin
45 print '%1! is not a valid database name', @sapdb_name
46 select @sqlerror=-100
47 commit
48 goto exit_sp
49 end
50
51 -- work-around JS timing issue
52 -- 1. determine own sched_job_id
53 SELECT @sjobid = jsh_sjobid FROM sybmgmtdb..js_history WHERE jsh_spid = @@spid AND
jsh_jobend IS NULL AND jsh_state='R2' AND jsh_exit_code = 0
54 -- 2. get seconds since last execution
55 SELECT @sec_past = datediff(ss,MAX(jsh_jobstart),current_bigdatetime()) FROM
sybmgmtdb..js_history WHERE jsh_sjobid = @sjobid AND jsh_jobend IS NOT NULL
56 -- 3. skip dump trans if last execution time is within the past 120sec
57 if @sjobid IS NULL or @sjobid < 1 or @sec_past > 120 or @sec_past IS NULL
58 begin
59 -- ensure no dump database is running
60 select @backup_active = BackupInProgress from master..monOpenDatabases where
DBName = @sapdb_name
61 if @backup_active = 0
62 begin
63 -- execute dump db
64 select @starttime = getdate()
65 select @current_ts = CONVERT(varchar,@starttime,23)
66 select @current_ts = str_replace(@current_ts,':','_')
67 select @curr_logdev = '/sybase/'+ @@servername +
'/backups/'+@sapdb_name+'_'+@current_ts+'.dmp'
68 select @dumpcmd = 'dump database ' || @sapdb_name || ' to "' || @curr_logdev
|| '" with compression = 101'
69 insert into
saptools..dump_history(DBName,Type,Command,StartTime,EndTime,RC,Status) VALUES
(@sapdb_name,'DB',@dumpcmd,@starttime,NULL,NULL,'R')
70 commit
71 exec(@dumpcmd)
72 select @sqlstatus = @@sqlstatus, @sqlerror = @@error
73 if @sqlerror <> 0
74 begin
75 update saptools..dump_history set EndTime = getdate(), Status = 'E', RC =
@sqlerror where StartTime = @starttime
76 end
77 else
78 begin
79 update saptools..dump_history set EndTime = getdate(), Status = 'F', RC = 0
where StartTime = @starttime
80 end
81 commit
82 end
83 else
84 begin
85 insert into
saptools..dump_history(DBName,Type,Command,StartTime,EndTime,RC,Status) VALUES
(@sapdb_name,'DB','',getdate(),getdate(),0,'S')
86 print 'skipping database dump due to an active database backup'
87 select @sqlerror=-100
88 commit
89 end
90 end
91
92 exit_sp:
93 if (@sqlerror<>0)
94 return @sqlerror
95 else
96 return 0
97 end
98
99 go
100

Vous aimerez peut-être aussi