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 3 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, see <http://www.gnu.org/licenses/>.
22 This program is meant for backup/restore of tdb databases. Typical usage would be:
24 when Samba shuts down cleanly, which will make a backup of all the local databases
25 to *.bak files. Then on Samba startup you would use:
27 and this will check the databases for corruption and if corruption is detected then
28 the backup will be restored.
30 You may also like to do a backup on a regular basis while Samba is
31 running, perhaps using cron.
33 The reason this program is needed is to cope with power failures
34 while Samba is running. A power failure could lead to database
35 corruption and Samba will then not start correctly.
37 Note that many of the databases in Samba are transient and thus
38 don't need to be backed up, so you can optimise the above a little
39 by only running the backup on the critical databases.
44 #include "system/locale.h"
45 #include "system/time.h"
46 #include "system/filesys.h"
47 #include "system/wait.h"
56 static char *add_suffix(const char *name, const char *suffix)
59 int len = strlen(name) + strlen(suffix) + 1;
60 ret = (char *)malloc(len);
62 fprintf(stderr,"Out of memory!\n");
65 snprintf(ret, len, "%s%s", name, suffix);
69 static int copy_fn(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
71 TDB_CONTEXT *tdb_new = (TDB_CONTEXT *)state;
73 if (tdb_store(tdb_new, key, dbuf, TDB_INSERT) != 0) {
74 fprintf(stderr,"Failed to insert into %s\n", tdb_name(tdb_new));
82 static int test_fn(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
88 carefully backup a tdb, validating the contents and
89 only doing the backup if its OK
90 this function is also used for restore
92 static int backup_tdb(const char *old_name, const char *new_name, int hash_size)
100 tmp_name = add_suffix(new_name, ".tmp");
102 /* stat the old tdb to find its permissions */
103 if (stat(old_name, &st) != 0) {
109 /* open the old tdb */
110 tdb = tdb_open(old_name, 0, 0, O_RDWR, 0);
112 printf("Failed to open %s\n", old_name);
117 /* create the new tdb */
119 tdb_new = tdb_open(tmp_name,
120 hash_size ? hash_size : tdb_hash_size(tdb),
121 TDB_DEFAULT, O_RDWR|O_CREAT|O_EXCL,
129 /* lock the old tdb */
130 if (tdb_lockall(tdb) != 0) {
131 fprintf(stderr,"Failed to lock %s\n", old_name);
141 /* traverse and copy */
142 count1 = tdb_traverse(tdb, copy_fn, (void *)tdb_new);
143 if (count1 < 0 || failed) {
144 fprintf(stderr,"failed to copy %s\n", old_name);
152 /* close the old tdb */
155 /* close the new tdb and re-open read-only */
157 tdb_new = tdb_open(tmp_name, 0, TDB_DEFAULT, O_RDONLY, 0);
159 fprintf(stderr,"failed to reopen %s\n", tmp_name);
166 /* traverse the new tdb to confirm */
167 count2 = tdb_traverse(tdb_new, test_fn, NULL);
168 if (count2 != count1) {
169 fprintf(stderr,"failed to copy %s\n", old_name);
176 /* make sure the new tdb has reached stable storage */
177 fsync(tdb_fd(tdb_new));
179 /* close the new tdb and rename it to .bak */
181 if (rename(tmp_name, new_name) != 0) {
193 verify a tdb and if it is corrupt then restore from *.bak
195 static int verify_tdb(const char *fname, const char *bak_name)
201 tdb = tdb_open(fname, 0, 0, O_RDONLY, 0);
203 /* traverse the tdb, then close it */
205 count = tdb_traverse(tdb, test_fn, NULL);
209 /* count is < 0 means an error */
211 printf("restoring %s\n", fname);
212 return backup_tdb(bak_name, fname, 0);
215 printf("%s : %d records\n", fname, count);
221 see if one file is newer than another
223 static int file_newer(const char *fname1, const char *fname2)
225 struct stat st1, st2;
226 if (stat(fname1, &st1) != 0) {
229 if (stat(fname2, &st2) != 0) {
232 return (st1.st_mtime > st2.st_mtime);
235 static void usage(void)
237 printf("Usage: tdbbackup [options] <fname...>\n\n");
238 printf(" -h this help message\n");
239 printf(" -s suffix set the backup suffix\n");
240 printf(" -v verify mode (restore if corrupt)\n");
241 printf(" -n hashsize set the new hash size for the backup\n");
245 int main(int argc, char *argv[])
252 const char *suffix = ".bak";
254 while ((c = getopt(argc, argv, "vhs:n:")) != -1) {
266 hashsize = atoi(optarg);
279 for (i=0; i<argc; i++) {
280 const char *fname = argv[i];
283 bak_name = add_suffix(fname, suffix);
286 if (verify_tdb(fname, bak_name) != 0) {
290 if (file_newer(fname, bak_name) &&
291 backup_tdb(fname, bak_name, hashsize) != 0) {