tdb/tools/tdbbackup.c

説明を見る。
00001 /* 
00002    Unix SMB/CIFS implementation.
00003    low level tdb backup and restore utility
00004    Copyright (C) Andrew Tridgell              2002
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU General Public License for more details.
00015    
00016    You should have received a copy of the GNU General Public License
00017    along with this program; if not, write to the Free Software
00018    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019 */
00020 
00021 /*
00022 
00023   This program is meant for backup/restore of tdb databases. Typical usage would be:
00024      tdbbackup *.tdb
00025   when Samba shuts down cleanly, which will make a backup of all the local databases
00026   to *.bak files. Then on Samba startup you would use:
00027      tdbbackup -v *.tdb
00028   and this will check the databases for corruption and if corruption is detected then
00029   the backup will be restored.
00030 
00031   You may also like to do a backup on a regular basis while Samba is
00032   running, perhaps using cron.
00033 
00034   The reason this program is needed is to cope with power failures
00035   while Samba is running. A power failure could lead to database
00036   corruption and Samba will then not start correctly.
00037 
00038   Note that many of the databases in Samba are transient and thus
00039   don't need to be backed up, so you can optimise the above a little
00040   by only running the backup on the critical databases.
00041 
00042  */
00043 
00044 #ifdef STANDALONE
00045 #if HAVE_CONFIG_H
00046 #include <config.h>
00047 #endif
00048 
00049 #include <errno.h>
00050 #include <stdlib.h>
00051 #include <stdio.h>
00052 #include <fcntl.h>
00053 #include <unistd.h>
00054 #include <string.h>
00055 #include <fcntl.h>
00056 #include <time.h>
00057 #include <sys/mman.h>
00058 #include <sys/stat.h>
00059 #include <sys/time.h>
00060 #include <ctype.h>
00061 #include <signal.h>
00062 
00063 #else
00064 
00065 #include "includes.h"
00066 
00067 #endif
00068 
00069 #include "tdb.h"
00070 #include "tdbback.h"
00071 
00072 extern int optind;
00073 extern char *optarg;
00074 
00075 /*
00076   see if one file is newer than another
00077 */
00078 static int file_newer(const char *fname1, const char *fname2)
00079 {
00080         struct stat st1, st2;
00081         if (stat(fname1, &st1) != 0) {
00082                 return 0;
00083         }
00084         if (stat(fname2, &st2) != 0) {
00085                 return 1;
00086         }
00087         return (st1.st_mtime > st2.st_mtime);
00088 }
00089 
00090 static void usage(void)
00091 {
00092         printf("Usage: tdbbackup [options] <fname...>\n\n");
00093         printf("   -h            this help message\n");
00094         printf("   -s suffix     set the backup suffix\n");
00095         printf("   -v            verify mode (restore if corrupt)\n");
00096         printf("   -n hashsize   set the new hash size for the backup\n");
00097 }
00098                 
00099 
00100  int main(int argc, char *argv[])
00101 {
00102         int i;
00103         int ret = 0;
00104         int c;
00105         int verify = 0;
00106         int hashsize = 0;
00107         const char *suffix = ".bak";
00108 
00109         while ((c = getopt(argc, argv, "vhs:n:")) != -1) {
00110                 switch (c) {
00111                 case 'h':
00112                         usage();
00113                         exit(0);
00114                 case 'v':
00115                         verify = 1;
00116                         break;
00117                 case 's':
00118                         suffix = optarg;
00119                         break;
00120                 case 'n':
00121                         hashsize = atoi(optarg);
00122                         break;
00123                 }
00124         }
00125 
00126         argc -= optind;
00127         argv += optind;
00128 
00129         if (argc < 1) {
00130                 usage();
00131                 exit(1);
00132         }
00133 
00134         for (i=0; i<argc; i++) {
00135                 const char *fname = argv[i];
00136                 char *bak_name;
00137 
00138                 bak_name = add_suffix(fname, suffix);
00139 
00140                 if (verify) {
00141                         if (verify_tdb(fname, bak_name) != 0) {
00142                                 ret = 1;
00143                         }
00144                 } else {
00145                         if (file_newer(fname, bak_name) &&
00146                             backup_tdb(fname, bak_name, hashsize) != 0) {
00147                                 ret = 1;
00148                         }
00149                 }
00150 
00151                 free(bak_name);
00152         }
00153 
00154         return ret;
00155 }

Sambaに対してSat Aug 29 21:23:27 2009に生成されました。  doxygen 1.4.7