2 Unix SMB/CIFS implementation.
3 low level tdb backup and restore utility
4 Copyright (C) Andrew Tridgell 2002
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
49 char *add_suffix(const char *name
, const char *suffix
)
52 int len
= strlen(name
) + strlen(suffix
) + 1;
55 fprintf(stderr
,"Out of memory!\n");
58 snprintf(ret
, len
, "%s%s", name
, suffix
);
62 static int copy_fn(TDB_CONTEXT
*tdb
, TDB_DATA key
, TDB_DATA dbuf
, void *state
)
64 TDB_CONTEXT
*tdb_new
= (TDB_CONTEXT
*)state
;
66 if (tdb_store(tdb_new
, key
, dbuf
, TDB_INSERT
) != 0) {
67 fprintf(stderr
,"Failed to insert into %s\n", tdb_new
->name
);
75 static int test_fn(TDB_CONTEXT
*tdb
, TDB_DATA key
, TDB_DATA dbuf
, void *state
)
81 carefully backup a tdb, validating the contents and
82 only doing the backup if its OK
83 this function is also used for restore
85 int backup_tdb(const char *old_name
, const char *new_name
)
93 tmp_name
= add_suffix(new_name
, ".tmp");
95 /* stat the old tdb to find its permissions */
96 if (stat(old_name
, &st
) != 0) {
101 /* open the old tdb */
102 tdb
= tdb_open(old_name
, 0, 0, O_RDWR
, 0);
104 printf("Failed to open %s\n", old_name
);
108 /* create the new tdb */
110 tdb_new
= tdb_open(tmp_name
, tdb
->header
.hash_size
,
111 TDB_DEFAULT
, O_RDWR
|O_CREAT
|O_EXCL
,
119 /* lock the old tdb */
120 if (tdb_lockall(tdb
) != 0) {
121 fprintf(stderr
,"Failed to lock %s\n", old_name
);
131 /* traverse and copy */
132 count1
= tdb_traverse(tdb
, copy_fn
, (void *)tdb_new
);
133 if (count1
< 0 || failed
) {
134 fprintf(stderr
,"failed to copy %s\n", old_name
);
142 /* close the old tdb */
145 /* close the new tdb and re-open read-only */
147 tdb_new
= tdb_open(tmp_name
, 0, TDB_DEFAULT
, O_RDONLY
, 0);
149 fprintf(stderr
,"failed to reopen %s\n", tmp_name
);
156 /* traverse the new tdb to confirm */
157 count2
= tdb_traverse(tdb_new
, test_fn
, 0);
158 if (count2
!= count1
) {
159 fprintf(stderr
,"failed to copy %s\n", old_name
);
166 /* make sure the new tdb has reached stable storage */
169 /* close the new tdb and rename it to .bak */
172 if (rename(tmp_name
, new_name
) != 0) {
186 verify a tdb and if it is corrupt then restore from *.bak
188 int verify_tdb(const char *fname
, const char *bak_name
)
194 tdb
= tdb_open(fname
, 0, 0, O_RDONLY
, 0);
196 /* traverse the tdb, then close it */
198 count
= tdb_traverse(tdb
, test_fn
, NULL
);
202 /* count is < 0 means an error */
204 printf("restoring %s\n", fname
);
205 return backup_tdb(bak_name
, fname
);
208 printf("%s : %d records\n", fname
, count
);