1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 2007,2008 Oracle.  All rights reserved.
4#
5# $Id: backup.tcl,v 1.5 2008/01/08 20:58:53 bostic Exp $
6#
7# TEST	backup
8# TEST 	Test of hotbackup functionality.
9# TEST
10# TEST	Do all the of the following tests with and without
11# TEST	the -c (checkpoint) option.  Make sure that -c and
12# TEST	-d (data_dir) are not allowed together.
13# TEST
14# TEST	(1) Test that plain and simple hotbackup works.
15# TEST	(2) Test with -data_dir (-d).
16# TEST	(3) Test updating an existing hot backup (-u).
17# TEST	(4) Test with absolute path.
18# TEST	(5) Test with DB_CONFIG (-D), setting log_dir (-l)
19# TEST		and data_dir (-d).
20# TEST	(6) DB_CONFIG and update.
21# TEST	(7) Repeat hot backup (non-update) with DB_CONFIG and
22# TEST		existing directories.
23
24proc backup { {nentries 1000} } {
25	source ./include.tcl
26	global util_path
27
28	set omethod "-btree"
29	set testfile "foo.db"
30	set backupdir "backup"
31
32	# Set up small logs so we quickly create more than one.
33	set log_size 20000
34	set env_flags " -create -txn -home $testdir -log_max $log_size"
35	set db_flags " -create $omethod -auto_commit $testfile "
36
37	foreach option { checkpoint nocheckpoint } {
38		if { $option == "checkpoint" } {
39			set c "c"
40			set msg "with checkpoint"
41		} else {
42			set c ""
43			set msg "without checkpoint"
44		}
45		puts "Backuptest $msg."
46
47		env_cleanup $testdir
48		env_cleanup $backupdir
49
50		set env [eval {berkdb_env} $env_flags]
51		set db [eval {berkdb_open} -env $env $db_flags]
52		set txn [$env txn]
53		populate $db $omethod $txn $nentries 0 0
54		$txn commit
55
56		# Backup directory is empty before hot backup.
57		set files [glob -nocomplain $backupdir/*]
58		error_check_good no_files [llength $files] 0
59
60		puts "\tBackuptest.a: Hot backup to directory $backupdir."
61		if {[catch { eval exec $util_path/db_hotbackup\
62		    -${c}vh $testdir -b $backupdir } res ] } {
63			error "FAIL: $res"
64		}
65
66		set logfiles [glob $backupdir/log*]
67		error_check_bad found_logs [llength $logfiles] 0
68		error_check_good found_db [file exists $backupdir/$testfile] 1
69
70		error_check_good db_close [$db close] 0
71		error_check_good env_close [$env close] 0
72		env_cleanup $testdir
73
74		puts "\tBackuptest.b: Hot backup with data_dir."
75		file mkdir $testdir/data1
76		error_check_good db_data_dir\
77		    [file exists $testdir/data1/$testfile] 0
78
79		# Create a new env with data_dir.
80		set env [eval {berkdb_env_noerr} $env_flags -data_dir data1]
81		set db [eval {berkdb_open} -env $env $db_flags]
82		set txn [$env txn]
83		populate $db $omethod $txn $nentries 0 0
84		$txn commit
85
86		# Check that data went into data_dir.
87		error_check_good db_data_dir\
88		    [file exists $testdir/data1/$testfile] 1
89
90		# You may not specify both -d (data_dir) and -c (checkpoint).
91		set msg2 "cannot specify -d and -c"
92		if { $option == "checkpoint" } {
93			catch {eval exec $util_path/db_hotbackup\
94			     -${c}vh $testdir -b $backupdir\
95			     -d $testdir/data1} res
96			error_check_good c_and_d [is_substr $res $msg2] 1
97		} else {
98			if {[catch {eval exec $util_path/db_hotbackup\
99			    -${c}vh $testdir -b $backupdir\
100			    -d $testdir/data1} res] } {
101				error "FAIL: $res"
102			}
103			# Check that logs and db are in backupdir.
104			error_check_good db_backup\
105			    [file exists $backupdir/$testfile] 1
106			set logfiles [glob $backupdir/log*]
107			error_check_bad logs_backed_up [llength $logfiles] 0
108		}
109
110		# Add more data and try the "update" flag.
111		puts "\tBackuptest.c: Update existing hot backup."
112		set txn [$env txn]
113		populate $db $omethod $txn [expr $nentries * 2] 0 0
114		$txn commit
115
116		if { $option == "checkpoint" } {
117			catch {eval exec $util_path/db_hotbackup\
118			     -${c}vuh $testdir -b backup -d $testdir/data1} res
119			error_check_good c_and_d [is_substr $res $msg2] 1
120		} else {
121			if {[catch {eval exec $util_path/db_hotbackup\
122			    -${c}vuh $testdir -b backup\
123			    -d $testdir/data1} res] } {
124				error "FAIL: $res"
125			}
126			# There should be more log files now.
127			set newlogfiles [glob $backupdir/log*]
128			error_check_bad more_logs $newlogfiles $logfiles
129		}
130
131		puts "\tBackuptest.d: Hot backup with full path."
132		set fullpath [pwd]
133		if { $option == "checkpoint" } {
134			catch {eval exec $util_path/db_hotbackup\
135			    -${c}vh $testdir -b backup\
136			    -d $fullpath/$testdir/data1} res
137			error_check_good c_and_d [is_substr $res $msg2] 1
138		} else {
139			if {[catch {eval exec $util_path/db_hotbackup\
140			    -${c}vh $testdir -b backup\
141			    -d $fullpath/$testdir/data1} res] } {
142				error "FAIL: $res"
143			}
144		}
145
146		error_check_good db_close [$db close] 0
147		error_check_good env_close [$env close] 0
148		env_cleanup $testdir
149		env_cleanup $backupdir
150
151		puts "\tBackuptest.e: Hot backup with DB_CONFIG."
152		backuptest_makeconfig
153
154		set env [eval {berkdb_env_noerr} $env_flags]
155		set db [eval {berkdb_open} -env $env $db_flags]
156		set txn [$env txn]
157		populate $db $omethod $txn $nentries 0 0
158		$txn commit
159
160		if { $option == "checkpoint" } {
161			catch {eval exec $util_path/db_hotbackup\
162			    -${c}vh $testdir -b $backupdir -l $testdir/logs\
163			    -d $testdir/data1} res
164			error_check_good c_and_d [is_substr $res $msg2] 1
165		} else {
166			if {[catch {eval exec $util_path/db_hotbackup\
167			    -${c}vh $testdir -b $backupdir -l $testdir/logs\
168			    -d $testdir/data1} res] } {
169				error "FAIL: $res"
170			}
171			# Check that logs and db are in backupdir.
172			error_check_good db_backup\
173			    [file exists $backupdir/$testfile] 1
174			set logfiles [glob $backupdir/log*]
175			error_check_bad logs_backed_up [llength $logfiles] 0
176		}
177
178		set txn [$env txn]
179		populate $db $omethod $txn [expr $nentries * 2] 0 0
180		$txn commit
181
182		puts "\tBackuptest.f:\
183		    Hot backup update with DB_CONFIG."
184		if { $option == "checkpoint" } {
185			catch {eval exec $util_path/db_hotbackup\
186			    -${c}vuh $testdir -b backup -l $testdir/logs\
187			    -d $testdir/data1} res
188			error_check_good c_and_d [is_substr $res $msg2] 1
189		} else {
190			if {[catch {eval exec $util_path/db_hotbackup\
191			    -${c}vuh $testdir -b backup -l $testdir/logs\
192			    -d $testdir/data1} res] } {
193				error "FAIL: $res"
194			}
195			# There should be more log files now.
196			set newlogfiles [glob $backupdir/log*]
197			error_check_bad more_logs $newlogfiles $logfiles
198		}
199
200		# Repeat with directories already there to test cleaning.
201		# We are not doing an update this time.
202		puts "\tBackuptest.g:\
203		    Hot backup with DB_CONFIG (non-update)."
204		if { [catch { eval exec $util_path/db_hotbackup\
205		    -${c}vh $testdir -b $backupdir -D } res] } {
206			error "FAIL: $res"
207		}
208
209		error_check_good db_close [$db close] 0
210		error_check_good env_close [$env close] 0
211	}
212}
213
214proc backuptest_makeconfig { } {
215	source ./include.tcl
216
217	file mkdir $testdir/logs
218	file mkdir $testdir/data1
219
220	set cid [open $testdir/DB_CONFIG w]
221	puts $cid "set_lg_dir logs"
222	puts $cid "set_data_dir data1"
223	close $cid
224}
225
226