[GLUE] Rsync SAMBA_3_0 SVN r25598 in order to create the v3-0-test branch.
[Samba.git] / source / tdb / tools / tdbbackup.c
blob4ae5a4f9212337ede33387a53a821fd9ea731a78
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.
23 This program is meant for backup/restore of tdb databases. Typical usage would be:
24 tdbbackup *.tdb
25 when Samba shuts down cleanly, which will make a backup of all the local databases
26 to *.bak files. Then on Samba startup you would use:
27 tdbbackup -v *.tdb
28 and this will check the databases for corruption and if corruption is detected then
29 the backup will be restored.
31 You may also like to do a backup on a regular basis while Samba is
32 running, perhaps using cron.
34 The reason this program is needed is to cope with power failures
35 while Samba is running. A power failure could lead to database
36 corruption and Samba will then not start correctly.
38 Note that many of the databases in Samba are transient and thus
39 don't need to be backed up, so you can optimise the above a little
40 by only running the backup on the critical databases.
44 #ifdef STANDALONE
45 #if HAVE_CONFIG_H
46 #include <config.h>
47 #endif
49 #include <errno.h>
50 #include <stdlib.h>
51 #include <stdio.h>
52 #include <fcntl.h>
53 #include <unistd.h>
54 #include <string.h>
55 #include <fcntl.h>
56 #include <time.h>
57 #include <sys/mman.h>
58 #include <sys/stat.h>
59 #include <sys/time.h>
60 #include <ctype.h>
61 #include <signal.h>
63 #else
65 #include "includes.h"
67 #endif
69 #include "tdb.h"
70 #include "tdbback.h"
72 extern int optind;
73 extern char *optarg;
76 see if one file is newer than another
78 static int file_newer(const char *fname1, const char *fname2)
80 struct stat st1, st2;
81 if (stat(fname1, &st1) != 0) {
82 return 0;
84 if (stat(fname2, &st2) != 0) {
85 return 1;
87 return (st1.st_mtime > st2.st_mtime);
90 static void usage(void)
92 printf("Usage: tdbbackup [options] <fname...>\n\n");
93 printf(" -h this help message\n");
94 printf(" -s suffix set the backup suffix\n");
95 printf(" -v verify mode (restore if corrupt)\n");
96 printf(" -n hashsize set the new hash size for the backup\n");
100 int main(int argc, char *argv[])
102 int i;
103 int ret = 0;
104 int c;
105 int verify = 0;
106 int hashsize = 0;
107 const char *suffix = ".bak";
109 while ((c = getopt(argc, argv, "vhs:n:")) != -1) {
110 switch (c) {
111 case 'h':
112 usage();
113 exit(0);
114 case 'v':
115 verify = 1;
116 break;
117 case 's':
118 suffix = optarg;
119 break;
120 case 'n':
121 hashsize = atoi(optarg);
122 break;
126 argc -= optind;
127 argv += optind;
129 if (argc < 1) {
130 usage();
131 exit(1);
134 for (i=0; i<argc; i++) {
135 const char *fname = argv[i];
136 char *bak_name;
138 bak_name = add_suffix(fname, suffix);
140 if (verify) {
141 if (verify_tdb(fname, bak_name) != 0) {
142 ret = 1;
144 } else {
145 if (file_newer(fname, bak_name) &&
146 backup_tdb(fname, bak_name, hashsize) != 0) {
147 ret = 1;
151 free(bak_name);
154 return ret;