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/filesys.h"
46 /* Currently we default to creating a tdb1. This will change! */
47 #define TDB2_IS_DEFAULT false
55 static void tdb_log(struct tdb_context *tdb,
56 enum tdb_log_level level,
61 fprintf(stderr, "%s:%s\n", tdb_errorstr(ecode), message);
64 static char *add_suffix(const char *name, const char *suffix)
67 int len = strlen(name) + strlen(suffix) + 1;
68 ret = (char *)malloc(len);
70 fprintf(stderr,"Out of memory!\n");
73 snprintf(ret, len, "%s%s", name, suffix);
77 static int copy_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
79 struct tdb_context *tdb_new = (struct tdb_context *)state;
82 err = tdb_store(tdb_new, key, dbuf, TDB_INSERT);
84 fprintf(stderr,"Failed to insert into %s: %s\n",
85 tdb_name(tdb_new), tdb_errorstr(err));
93 static int test_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
99 carefully backup a tdb, validating the contents and
100 only doing the backup if its OK
101 this function is also used for restore
103 static int backup_tdb(const char *old_name, const char *new_name,
104 bool tdb2, int hash_size)
106 struct tdb_context *tdb;
107 struct tdb_context *tdb_new;
112 union tdb_attribute log_attr, hsize_attr;
113 int tdb_flags = TDB_DEFAULT;
116 tdb_flags |= TDB_VERSION1;
119 tmp_name = add_suffix(new_name, ".tmp");
121 /* stat the old tdb to find its permissions */
122 if (stat(old_name, &st) != 0) {
128 log_attr.base.attr = TDB_ATTRIBUTE_LOG;
129 log_attr.base.next = NULL;
130 log_attr.log.fn = tdb_log;
132 /* open the old tdb */
133 tdb = tdb_open(old_name, TDB_DEFAULT, O_RDWR, 0, &log_attr);
135 printf("Failed to open %s\n", old_name);
140 /* create the new tdb */
141 if (!tdb2 && hash_size) {
142 hsize_attr.base.attr = TDB_ATTRIBUTE_TDB1_HASHSIZE;
143 hsize_attr.base.next = NULL;
144 hsize_attr.tdb1_hashsize.hsize = hash_size;
145 log_attr.base.next = &hsize_attr;
149 tdb_new = tdb_open(tmp_name, tdb_flags,
150 O_RDWR|O_CREAT|O_EXCL, st.st_mode & 0777,
158 err = tdb_transaction_start(tdb);
160 fprintf(stderr, "Failed to start transaction on old tdb: %s\n",
169 /* lock the backup tdb so that nobody else can change it */
170 err = tdb_lockall(tdb_new);
172 fprintf(stderr, "Failed to lock backup tdb: %s\n",
183 /* traverse and copy */
184 count1 = tdb_traverse(tdb, copy_fn, (void *)tdb_new);
185 if (count1 < 0 || failed) {
186 fprintf(stderr,"failed to copy %s\n", old_name);
194 /* close the old tdb */
197 /* copy done, unlock the backup tdb */
198 tdb_unlockall(tdb_new);
200 #ifdef HAVE_FDATASYNC
201 if (fdatasync(tdb_fd(tdb_new)) != 0) {
203 if (fsync(tdb_fd(tdb_new)) != 0) {
206 fprintf(stderr, "failed to fsync backup file\n");
209 /* close the new tdb and re-open read-only */
212 /* we don't need the hash attr any more */
213 log_attr.base.next = NULL;
215 tdb_new = tdb_open(tmp_name, TDB_DEFAULT, O_RDONLY, 0, &log_attr);
217 fprintf(stderr,"failed to reopen %s\n", tmp_name);
224 /* traverse the new tdb to confirm */
225 count2 = tdb_traverse(tdb_new, test_fn, NULL);
226 if (count2 != count1) {
227 fprintf(stderr,"failed to copy %s\n", old_name);
234 /* close the new tdb and rename it to .bak */
236 if (rename(tmp_name, new_name) != 0) {
248 verify a tdb and if it is corrupt then restore from *.bak
250 static int verify_tdb(const char *fname, const char *bak_name)
252 struct tdb_context *tdb;
254 union tdb_attribute log_attr;
256 log_attr.base.attr = TDB_ATTRIBUTE_LOG;
257 log_attr.base.next = NULL;
258 log_attr.log.fn = tdb_log;
261 tdb = tdb_open(fname, TDB_DEFAULT, O_RDONLY, 0, &log_attr);
263 /* traverse the tdb, then close it */
265 count = tdb_traverse(tdb, test_fn, NULL);
269 /* count is < 0 means an error */
271 printf("restoring %s\n", fname);
272 return backup_tdb(bak_name, fname, TDB2_IS_DEFAULT, 0);
275 printf("%s : %d records\n", fname, count);
281 see if one file is newer than another
283 static int file_newer(const char *fname1, const char *fname2)
285 struct stat st1, st2;
286 if (stat(fname1, &st1) != 0) {
289 if (stat(fname2, &st2) != 0) {
292 return (st1.st_mtime > st2.st_mtime);
295 static void usage(void)
297 printf("Usage: tdbbackup [options] <fname...>\n\n");
298 printf(" -h this help message\n");
299 printf(" -1 make the backup a TDB1 file\n");
300 printf(" -2 make the backup a TDB2 file\n");
301 printf(" -v verify mode (restore if corrupt)\n");
302 printf(" -s suffix set the backup suffix\n");
303 printf(" -v verify mode (restore if corrupt)\n");
304 printf(" -n hashsize set the new hash size for the backup\n");
308 int main(int argc, char *argv[])
315 bool tdb2 = TDB2_IS_DEFAULT;
316 const char *suffix = ".bak";
318 while ((c = getopt(argc, argv, "vhs:n:12")) != -1) {
336 hashsize = atoi(optarg);
349 for (i=0; i<argc; i++) {
350 const char *fname = argv[i];
353 bak_name = add_suffix(fname, suffix);
356 if (verify_tdb(fname, bak_name) != 0) {
360 if (file_newer(fname, bak_name) &&
361 backup_tdb(fname, bak_name, tdb2, hashsize) != 0) {