r3780: final release notes
[Samba.git] / source / tdb / tdbback.c
blob68b6fadc882e59628fc0d009b306d6568b9b717c
1 /*
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.
21 #ifdef STANDALONE
22 #if HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <errno.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <fcntl.h>
33 #include <time.h>
34 #include <sys/mman.h>
36 #include <sys/stat.h>
37 #include <sys/time.h>
38 #include <ctype.h>
39 #include <signal.h>
41 #else
42 #include "includes.h"
43 #endif
45 #include "tdb.h"
47 static int failed;
49 char *add_suffix(const char *name, const char *suffix)
51 char *ret;
52 int len = strlen(name) + strlen(suffix) + 1;
53 ret = malloc(len);
54 if (!ret) {
55 fprintf(stderr,"Out of memory!\n");
56 exit(1);
58 snprintf(ret, len, "%s%s", name, suffix);
59 return ret;
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);
68 failed = 1;
69 return 1;
71 return 0;
75 static int test_fn(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
77 return 0;
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)
87 TDB_CONTEXT *tdb;
88 TDB_CONTEXT *tdb_new;
89 char *tmp_name;
90 struct stat st;
91 int count1, count2;
93 tmp_name = add_suffix(new_name, ".tmp");
95 /* stat the old tdb to find its permissions */
96 if (stat(old_name, &st) != 0) {
97 perror(old_name);
98 return 1;
101 /* open the old tdb */
102 tdb = tdb_open(old_name, 0, 0, O_RDWR, 0);
103 if (!tdb) {
104 printf("Failed to open %s\n", old_name);
105 return 1;
108 /* create the new tdb */
109 unlink(tmp_name);
110 tdb_new = tdb_open(tmp_name, tdb->header.hash_size,
111 TDB_DEFAULT, O_RDWR|O_CREAT|O_EXCL,
112 st.st_mode & 0777);
113 if (!tdb_new) {
114 perror(tmp_name);
115 free(tmp_name);
116 return 1;
119 /* lock the old tdb */
120 if (tdb_lockall(tdb) != 0) {
121 fprintf(stderr,"Failed to lock %s\n", old_name);
122 tdb_close(tdb);
123 tdb_close(tdb_new);
124 unlink(tmp_name);
125 free(tmp_name);
126 return 1;
129 failed = 0;
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);
135 tdb_close(tdb);
136 tdb_close(tdb_new);
137 unlink(tmp_name);
138 free(tmp_name);
139 return 1;
142 /* close the old tdb */
143 tdb_close(tdb);
145 /* close the new tdb and re-open read-only */
146 tdb_close(tdb_new);
147 tdb_new = tdb_open(tmp_name, 0, TDB_DEFAULT, O_RDONLY, 0);
148 if (!tdb_new) {
149 fprintf(stderr,"failed to reopen %s\n", tmp_name);
150 unlink(tmp_name);
151 perror(tmp_name);
152 free(tmp_name);
153 return 1;
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);
160 tdb_close(tdb_new);
161 unlink(tmp_name);
162 free(tmp_name);
163 return 1;
166 /* make sure the new tdb has reached stable storage */
167 fsync(tdb_new->fd);
169 /* close the new tdb and rename it to .bak */
170 tdb_close(tdb_new);
171 unlink(new_name);
172 if (rename(tmp_name, new_name) != 0) {
173 perror(new_name);
174 free(tmp_name);
175 return 1;
178 free(tmp_name);
180 return 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)
190 TDB_CONTEXT *tdb;
191 int count = -1;
193 /* open the tdb */
194 tdb = tdb_open(fname, 0, 0, O_RDONLY, 0);
196 /* traverse the tdb, then close it */
197 if (tdb) {
198 count = tdb_traverse(tdb, test_fn, NULL);
199 tdb_close(tdb);
202 /* count is < 0 means an error */
203 if (count < 0) {
204 printf("restoring %s\n", fname);
205 return backup_tdb(bak_name, fname);
208 printf("%s : %d records\n", fname, count);
210 return 0;