00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
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
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 }