Imported Upstream version 2.4.4p3
[debian/amanda] / contrib / dbbackup.tcl
1 #!/opt/tcl8.3.0/bin/tclsh8.3
2
3 #  .-------------.------------------------------------------------------.
4 #  |  module     |  dbbackup.tcl                                        |
5 #  `-------------^------------------------------------------------------'
6
7 #  .--------------------------------------------------------------------.
8 #  |                            Revisions                               |
9 #  |--------------------------------------------------------------------|
10 #  |  07/25/96 (TMH) eliminated the need for a library file.            |
11 #  |  02/10/97 (TMH) converted to tcl7.6, oratcl 2.4.                   |
12 #  |  12/02/98 (TMH) eliminate all pipes to /bin/sh for final release   | 
13 #  `--------------------------------------------------------------------'
14
15 #  .--------------------------------------------------------------------.
16 #  |            Copyright (c) 1998, Purdue University                   |
17 #  |                     All rights reserved.                           |
18 #  `--------------------------------------------------------------------'
19
20 #  .--------------------------------------------------------------------.
21 #  |  Redistribution and use in source and binary forms are permitted   |
22 #  |  provided that:                                                    |
23 #  |                                                                    |
24 #  | (1) source distributions retain this entire copyright notice and   |
25 #  |     comment, and                                                   |
26 #  | (2) distributions including binaries display the following         |
27 #  |      acknowledgement:                                              |
28 #  |                                                                    |
29 #  |   "This product includes software developed by Purdue University." |
30 #  | in the documentation or other materials provided with the          |
31 #  | distribution and in all advertising materials mentioning features  |
32 #  | or use of this software.                                           |
33 #  |                                                                    |
34 #  |   The name of the University may not be used to endorse or promote |
35 #  | products derived from this software without specific prior written |
36 #  | permission.                                                        |
37 #  |                                                                    |
38 #  | THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR     |
39 #  | IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED     |
40 #  | WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR      |
41 #  | PURPOSE.                                                           |
42 #  `--------------------------------------------------------------------'
43
44 package require -exact Oratcl 2.7
45
46 set uidpswd /
47 set df_dest /opt/oracle/backup
48 set cf_dest [file join $df_dest "ctl$env(ORACLE_SID).ctl"]
49
50 set cur {}
51 set lda {}
52
53
54 #  .--------------------------------------------------------------------.
55 #  |  sysdate                                                           |
56 #  `--------------------------------------------------------------------'
57 proc sysdate {} {
58         return [clock format [clock seconds] -format {%c}]
59 }
60
61
62 #  .-------------.------------------------------------------------------.
63 #  |  procedure  |  ora_Connect                                         |
64 #  |  date       |  (09/02/94)                                          |
65 #  `-------------^------------------------------------------------------'
66 proc ora_Connect {} {
67         global uidpswd
68         global cur
69         global lda
70
71         set retcode [catch {set lda [oralogon ${uidpswd}]}]
72         if {$retcode == 0} {
73                 set cur [oraopen $lda]
74         }
75         return $retcode
76 }
77
78
79 #  .-------------.------------------------------------------------------.
80 #  |  procedure  |  ora_Disconnect                                      |
81 #  |  author     |  Todd M. Helfter                                     |
82 #  |  date       |  (09/02/94)                                          |
83 #  `-------------^------------------------------------------------------'
84 proc ora_Disconnect {} {
85         global lda
86         return [catch {oralogoff $lda }]
87 }
88
89
90 #  .-------------.------------------------------------------------------.
91 #  |  procedure  |  print_log                                           |
92 #  `-------------^------------------------------------------------------'
93 proc print_log {} {
94         global cur oramsg
95
96         set log_min {}
97         set log_max {}
98         set sql {select min(sequence#), max(sequence#) from v$log}
99         if {[catch {orasql $cur $sql}] == 0} {
100                 orafetch $cur "set log_min @1; set log_max @2"
101         }
102         puts stdout [format "\nOldest online log sequence\t%s" $log_min]
103         puts stdout [format "Current log sequence\t\t%s\n" $log_max]
104         flush stdout
105 }
106
107
108 #  .-------------.------------------------------------------------------.
109 #  |  procedure  |  ora_SQL                                             |
110 #  `-------------^------------------------------------------------------'
111 proc ora_SQL {sql_str} {
112         global cur oramsg
113
114         set dbret [catch {orasql $cur $sql_str}]
115         if {$dbret != 0} {
116                 puts stdout $oramsg(errortxt)
117                 flush stdout
118         } else {
119                 orafetch $cur {
120                         puts stdout @0
121                         flush stdout
122                 }
123         }
124 }
125
126
127 #  .--------------------------------------------------------------------.
128 #  |  query_info                                                        |
129 #  `--------------------------------------------------------------------'
130 proc query_info {} {
131         global cur ts_list df_list oramsg uidpswd
132
133         set ts_list {}
134         if {[ora_Connect] == 0} {
135                 set sql0 "SELECT tablespace_name FROM sys.dba_tablespaces"
136                 if {[catch {orasql $cur $sql0}] == 0} {
137                         orafetch $cur {lappend ts_list @0}
138                 }
139         } else {
140                 puts stdout $oramsg(errortxt)
141                 flush stdout
142                 exit 1
143         }
144
145         foreach ts_name $ts_list {
146                 set df_list($ts_name) {}
147                 set sql1 "SELECT file_name FROM sys.dba_data_files \
148                         where tablespace_name = '$ts_name'"
149                 if {[catch {orasql $cur $sql1}] == 0} {
150                         orafetch $cur {lappend df_list($ts_name) @0}
151                 } else {
152                         puts stdout $oramsg(errortxt)
153                         flush stdout
154                         exit 1
155                 }
156         }
157 }
158
159
160 #  .--------------------------------------------------------------------.
161 #  |  print_info                                                        |
162 #  `--------------------------------------------------------------------'
163 proc print_info {} {
164         global ts_list df_list
165
166         foreach ts_name $ts_list {
167                 puts stdout "ts_name : $ts_name"
168                 flush stdout
169                 foreach df_name $df_list($ts_name) {
170                         puts stdout "  df_name : $df_name"
171                         flush stdout
172                 }
173         }
174 }
175
176
177 #  .--------------------------------------------------------------------.
178 #  |  ora_Backup                                                        |
179 #  `--------------------------------------------------------------------'
180 proc ora_Backup {} {
181         global ts_list df_list df_dest cf_dest
182
183         print_log
184
185         foreach ts_name $ts_list {
186
187                 puts stdout "[sysdate] | begin online backup for : $ts_name"
188                 flush stdout
189                 ora_SQL "alter tablespace $ts_name begin backup"
190
191                 foreach df_name $df_list($ts_name) {
192                         puts stdout "[sysdate] | copying $df_name to $df_dest."
193                         flush stdout
194                         file copy -force -- $df_name $df_dest
195                 }
196                 puts stdout "[sysdate] | end online backup for : $ts_name"
197                 flush stdout
198                 ora_SQL "alter tablespace $ts_name end backup"
199         }
200         puts stdout "[sysdate] | switching logfile."
201         flush stdout
202         ora_SQL {alter system switch logfile}
203
204         print_log
205
206         puts stdout "[sysdate] | copying control file to $cf_dest."
207         flush stdout
208         ora_SQL "alter database backup controlfile to '$cf_dest' reuse"
209 }
210
211
212 #  .-------------.------------------------------------------------------.
213 #  |  procedure  |  main                                                |
214 #  |  purpose    |  scan database and report results to parent window   |
215 #  |  author     |  Todd M. Helfter                                     |
216 #  |  date       |  (10/13/94)                                          |
217 #  `-------------^------------------------------------------------------'
218 proc main {} {
219         global cur uidpswd oramsg ts_list df_list
220
221         query_info
222         print_info
223
224         ora_Backup
225         ora_Disconnect
226 }
227
228
229 main
230 exit 0