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 struct tdb_logging_context log_ctx
;
58 #ifdef PRINTF_ATTRIBUTE
59 static void tdb_log(struct tdb_context
*tdb
, enum tdb_debug_level level
, const char *format
, ...) PRINTF_ATTRIBUTE(3,4);
61 static void tdb_log(struct tdb_context
*tdb
, enum tdb_debug_level level
, const char *format
, ...)
66 vfprintf(stdout
, format
, ap
);
71 static char *add_suffix(const char *name
, const char *suffix
)
74 int len
= strlen(name
) + strlen(suffix
) + 1;
75 ret
= (char *)malloc(len
);
77 fprintf(stderr
,"Out of memory!\n");
80 snprintf(ret
, len
, "%s%s", name
, suffix
);
84 static int copy_fn(TDB_CONTEXT
*tdb
, TDB_DATA key
, TDB_DATA dbuf
, void *state
)
86 TDB_CONTEXT
*tdb_new
= (TDB_CONTEXT
*)state
;
88 if (tdb_store(tdb_new
, key
, dbuf
, TDB_INSERT
) != 0) {
89 fprintf(stderr
,"Failed to insert into %s\n", tdb_name(tdb_new
));
97 static int test_fn(TDB_CONTEXT
*tdb
, TDB_DATA key
, TDB_DATA dbuf
, void *state
)
103 carefully backup a tdb, validating the contents and
104 only doing the backup if its OK
105 this function is also used for restore
107 static int backup_tdb(const char *old_name
, const char *new_name
,
108 int hash_size
, int nolock
)
111 TDB_CONTEXT
*tdb_new
;
116 tmp_name
= add_suffix(new_name
, ".tmp");
118 /* stat the old tdb to find its permissions */
119 if (stat(old_name
, &st
) != 0) {
125 /* open the old tdb */
126 tdb
= tdb_open_ex(old_name
, 0,
127 TDB_DEFAULT
| (nolock
? TDB_NOLOCK
: 0),
128 O_RDWR
, 0, &log_ctx
, NULL
);
130 printf("Failed to open %s\n", old_name
);
135 /* create the new tdb */
137 tdb_new
= tdb_open_ex(tmp_name
,
138 hash_size
? hash_size
: tdb_hash_size(tdb
),
140 O_RDWR
|O_CREAT
|O_EXCL
, st
.st_mode
& 0777,
148 if (tdb_transaction_start(tdb
) != 0) {
149 printf("Failed to start transaction on old tdb\n");
157 /* lock the backup tdb so that nobody else can change it */
158 if (tdb_lockall(tdb_new
) != 0) {
159 printf("Failed to lock backup tdb\n");
169 /* traverse and copy */
170 count1
= tdb_traverse(tdb
, copy_fn
, (void *)tdb_new
);
171 if (count1
< 0 || failed
) {
172 fprintf(stderr
,"failed to copy %s\n", old_name
);
180 /* close the old tdb */
183 /* copy done, unlock the backup tdb */
184 tdb_unlockall(tdb_new
);
186 #ifdef HAVE_FDATASYNC
187 if (fdatasync(tdb_fd(tdb_new
)) != 0) {
189 if (fsync(tdb_fd(tdb_new
)) != 0) {
192 fprintf(stderr
, "failed to fsync backup file\n");
195 /* close the new tdb and re-open read-only */
197 tdb_new
= tdb_open_ex(tmp_name
,
203 fprintf(stderr
,"failed to reopen %s\n", tmp_name
);
210 /* traverse the new tdb to confirm */
211 count2
= tdb_traverse(tdb_new
, test_fn
, NULL
);
212 if (count2
!= count1
) {
213 fprintf(stderr
,"failed to copy %s\n", old_name
);
220 /* close the new tdb and rename it to .bak */
222 if (rename(tmp_name
, new_name
) != 0) {
234 verify a tdb and if it is corrupt then restore from *.bak
236 static int verify_tdb(const char *fname
, const char *bak_name
)
242 tdb
= tdb_open_ex(fname
, 0, 0,
243 O_RDONLY
, 0, &log_ctx
, NULL
);
245 /* traverse the tdb, then close it */
247 count
= tdb_traverse(tdb
, test_fn
, NULL
);
251 /* count is < 0 means an error */
253 printf("restoring %s\n", fname
);
254 return backup_tdb(bak_name
, fname
, 0, 0);
257 printf("%s : %d records\n", fname
, count
);
263 see if one file is newer than another
265 static int file_newer(const char *fname1
, const char *fname2
)
267 struct stat st1
, st2
;
268 if (stat(fname1
, &st1
) != 0) {
271 if (stat(fname2
, &st2
) != 0) {
274 return (st1
.st_mtime
> st2
.st_mtime
);
277 static void usage(void)
279 printf("Usage: tdbbackup [options] <fname...>\n\n");
280 printf(" -h this help message\n");
281 printf(" -s suffix set the backup suffix\n");
282 printf(" -v verify mode (restore if corrupt)\n");
283 printf(" -n hashsize set the new hash size for the backup\n");
284 printf(" -l open without locking to back up mutex dbs\n");
287 int main(int argc
, char *argv
[])
295 const char *suffix
= ".bak";
297 log_ctx
.log_fn
= tdb_log
;
299 while ((c
= getopt(argc
, argv
, "vhs:n:l")) != -1) {
311 hashsize
= atoi(optarg
);
327 for (i
=0; i
<argc
; i
++) {
328 const char *fname
= argv
[i
];
331 bak_name
= add_suffix(fname
, suffix
);
334 if (verify_tdb(fname
, bak_name
) != 0) {
338 if (file_newer(fname
, bak_name
) &&
339 backup_tdb(fname
, bak_name
, hashsize
,