2 Unix SMB/CIFS implementation.
3 low level ntdb 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 ntdb 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.
53 static void ntdb_log(struct ntdb_context
*ntdb
,
54 enum ntdb_log_level level
,
55 enum NTDB_ERROR ecode
,
59 fprintf(stderr
, "%s:%s\n", ntdb_errorstr(ecode
), message
);
62 static char *add_suffix(const char *name
, const char *suffix
)
65 int len
= strlen(name
) + strlen(suffix
) + 1;
66 ret
= (char *)malloc(len
);
68 fprintf(stderr
,"Out of memory!\n");
71 snprintf(ret
, len
, "%s%s", name
, suffix
);
75 static int copy_fn(struct ntdb_context
*ntdb
, NTDB_DATA key
, NTDB_DATA dbuf
, void *state
)
77 struct ntdb_context
*ntdb_new
= (struct ntdb_context
*)state
;
80 err
= ntdb_store(ntdb_new
, key
, dbuf
, NTDB_INSERT
);
82 fprintf(stderr
,"Failed to insert into %s: %s\n",
83 ntdb_name(ntdb_new
), ntdb_errorstr(err
));
91 static int test_fn(struct ntdb_context
*ntdb
, NTDB_DATA key
, NTDB_DATA dbuf
, void *state
)
97 carefully backup a ntdb, validating the contents and
98 only doing the backup if its OK
99 this function is also used for restore
101 static int backup_ntdb(const char *old_name
, const char *new_name
)
103 struct ntdb_context
*ntdb
;
104 struct ntdb_context
*ntdb_new
;
109 union ntdb_attribute log_attr
;
111 tmp_name
= add_suffix(new_name
, ".tmp");
113 /* stat the old ntdb to find its permissions */
114 if (stat(old_name
, &st
) != 0) {
120 log_attr
.base
.attr
= NTDB_ATTRIBUTE_LOG
;
121 log_attr
.base
.next
= NULL
;
122 log_attr
.log
.fn
= ntdb_log
;
124 /* open the old ntdb */
125 ntdb
= ntdb_open(old_name
, NTDB_DEFAULT
, O_RDWR
, 0, &log_attr
);
127 printf("Failed to open %s\n", old_name
);
133 ntdb_new
= ntdb_open(tmp_name
, NTDB_DEFAULT
,
134 O_RDWR
|O_CREAT
|O_EXCL
, st
.st_mode
& 0777,
142 err
= ntdb_transaction_start(ntdb
);
144 fprintf(stderr
, "Failed to start transaction on old ntdb: %s\n",
147 ntdb_close(ntdb_new
);
153 /* lock the backup ntdb so that nobody else can change it */
154 err
= ntdb_lockall(ntdb_new
);
156 fprintf(stderr
, "Failed to lock backup ntdb: %s\n",
159 ntdb_close(ntdb_new
);
167 /* traverse and copy */
168 count1
= ntdb_traverse(ntdb
, copy_fn
, (void *)ntdb_new
);
169 if (count1
< 0 || failed
) {
170 fprintf(stderr
,"failed to copy %s\n", old_name
);
172 ntdb_close(ntdb_new
);
178 /* close the old ntdb */
181 /* copy done, unlock the backup ntdb */
182 ntdb_unlockall(ntdb_new
);
184 #ifdef HAVE_FDATASYNC
185 if (fdatasync(ntdb_fd(ntdb_new
)) != 0) {
187 if (fsync(ntdb_fd(ntdb_new
)) != 0) {
190 fprintf(stderr
, "failed to fsync backup file\n");
193 /* close the new ntdb and re-open read-only */
194 ntdb_close(ntdb_new
);
196 /* we don't need the hash attr any more */
197 log_attr
.base
.next
= NULL
;
199 ntdb_new
= ntdb_open(tmp_name
, NTDB_DEFAULT
, O_RDONLY
, 0, &log_attr
);
201 fprintf(stderr
,"failed to reopen %s\n", tmp_name
);
208 /* traverse the new ntdb to confirm */
209 count2
= ntdb_traverse(ntdb_new
, test_fn
, NULL
);
210 if (count2
!= count1
) {
211 fprintf(stderr
,"failed to copy %s\n", old_name
);
212 ntdb_close(ntdb_new
);
218 /* close the new ntdb and rename it to .bak */
219 ntdb_close(ntdb_new
);
220 if (rename(tmp_name
, new_name
) != 0) {
232 verify a ntdb and if it is corrupt then restore from *.bak
234 static int verify_ntdb(const char *fname
, const char *bak_name
)
236 struct ntdb_context
*ntdb
;
238 union ntdb_attribute log_attr
;
240 log_attr
.base
.attr
= NTDB_ATTRIBUTE_LOG
;
241 log_attr
.base
.next
= NULL
;
242 log_attr
.log
.fn
= ntdb_log
;
245 ntdb
= ntdb_open(fname
, NTDB_DEFAULT
, O_RDONLY
, 0, &log_attr
);
247 /* traverse the ntdb, then close it */
249 count
= ntdb_traverse(ntdb
, test_fn
, NULL
);
253 /* count is < 0 means an error */
255 printf("restoring %s\n", fname
);
256 return backup_ntdb(bak_name
, fname
);
259 printf("%s : %d records\n", fname
, count
);
265 see if one file is newer than another
267 static int file_newer(const char *fname1
, const char *fname2
)
269 struct stat st1
, st2
;
270 if (stat(fname1
, &st1
) != 0) {
273 if (stat(fname2
, &st2
) != 0) {
276 return (st1
.st_mtime
> st2
.st_mtime
);
279 static void usage(void)
281 printf("Usage: ntdbbackup [options] <fname...>\n\n");
282 printf(" -h this help message\n");
283 printf(" -v verify mode (restore if corrupt)\n");
284 printf(" -s suffix set the backup suffix\n");
285 printf(" -v verify mode (restore if corrupt)\n");
289 int main(int argc
, char *argv
[])
295 const char *suffix
= ".bak";
297 while ((c
= getopt(argc
, argv
, "vhs:")) != -1) {
319 for (i
=0; i
<argc
; i
++) {
320 const char *fname
= argv
[i
];
323 bak_name
= add_suffix(fname
, suffix
);
326 if (verify_ntdb(fname
, bak_name
) != 0) {
330 if (file_newer(fname
, bak_name
) &&
331 backup_ntdb(fname
, bak_name
) != 0) {