chiark / gitweb /
0eaf6b6c0b7e68cbba3ed4b772469942a1dfbf37
[elogind.git] / tdb / tdbbackup.c
1 /* 
2    Unix SMB/CIFS implementation.
3    low level tdb backup and restore utility
4    Copyright (C) Andrew Tridgell              2002
5
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.
10    
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.
15    
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.
19 */
20
21 /*
22
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.
30
31   You may also like to do a backup on a regular basis while Samba is
32   running, perhaps using cron.
33
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.
37
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.
41
42  */
43
44 #include <errno.h>
45 #include <stdlib.h>
46 #include <stdio.h>
47 #include <fcntl.h>
48 #include <unistd.h>
49 #include <string.h>
50 #include <fcntl.h>
51 #include <time.h>
52 #include <sys/mman.h>
53 #include <sys/stat.h>
54 #include <sys/time.h>
55 #include <ctype.h>
56 #include <signal.h>
57 #include "tdb.h"
58 #include "tdbback.h"
59
60 /*
61   see if one file is newer than another
62 */
63 static int file_newer(const char *fname1, const char *fname2)
64 {
65         struct stat st1, st2;
66         if (stat(fname1, &st1) != 0) {
67                 return 0;
68         }
69         if (stat(fname2, &st2) != 0) {
70                 return 1;
71         }
72         return (st1.st_mtime > st2.st_mtime);
73 }
74
75 static void usage(void)
76 {
77         printf("Usage: tdbbackup [options] <fname...>\n\n");
78         printf("   -h            this help message\n");
79         printf("   -s suffix     set the backup suffix\n");
80         printf("   -v            verify mode (restore if corrupt)\n");
81 }
82                 
83
84  int main(int argc, char *argv[])
85 {
86         int i;
87         int ret = 0;
88         int c;
89         int verify = 0;
90         const char *suffix = ".bak";
91         extern int optind;
92         extern char *optarg;
93
94         while ((c = getopt(argc, argv, "vhs:")) != -1) {
95                 switch (c) {
96                 case 'h':
97                         usage();
98                         exit(0);
99                 case 'v':
100                         verify = 1;
101                         break;
102                 case 's':
103                         suffix = optarg;
104                         break;
105                 }
106         }
107
108         argc -= optind;
109         argv += optind;
110
111         if (argc < 1) {
112                 usage();
113                 exit(1);
114         }
115
116         for (i=0; i<argc; i++) {
117                 const char *fname = argv[i];
118                 char *bak_name;
119
120                 bak_name = add_suffix(fname, suffix);
121
122                 if (verify) {
123                         if (verify_tdb(fname, bak_name) != 0) {
124                                 ret = 1;
125                         }
126                 } else {
127                         if (file_newer(fname, bak_name) &&
128                             backup_tdb(fname, bak_name) != 0) {
129                                 ret = 1;
130                         }
131                 }
132
133                 free(bak_name);
134         }
135
136         return ret;
137 }