New feature for tdbbackup: check status (verify but don't restore)

Volodymyr Khomenko Volodymyr.Khomenko at exanet.com
Wed Nov 12 17:32:37 GMT 2008


Hi all,

We are using samba together with own Failover Management System.
Sometimes we need to verify .tdb file for corruption but don't restore it
from .bak file if it's not ok.
For that purpose we've added '-c' (check status) parameter to tdbbackup tool.

We've decided that this feature can be usable for others,
so we would like to contribute our patch (for samba 3.2.4, also it is duplicated in attachment).
Please review it.

P.S. Original version of this feature was implemented by Meital Hershcovich, Exanet (for samba 3.0.x).

--- samba-3.2.4/source/lib/tdb/tools/tdbbackup.c
+++ samba-3.2.4/source/lib/tdb/tools/tdbbackup.c
@@ -190,9 +190,11 @@ static int backup_tdb(const char *old_na
 }
 
 /*
-  verify a tdb and if it is corrupt then restore from *.bak
+  verify a tdb and if it is corrupt then
+  -restore from *.bak (if dont_restore is not set)
+  -do nothing, just return negative value; bak_name will not be used (if dont_restore is set)
 */
-static int verify_tdb(const char *fname, const char *bak_name)
+static int verify_tdb(const char *fname, const char *bak_name, int dont_restore)
 {
 	TDB_CONTEXT *tdb;
 	int count = -1;
@@ -208,6 +210,8 @@ static int verify_tdb(const char *fname,
 
 	/* count is < 0 means an error */
 	if (count < 0) {
+		if(dont_restore)
+			return count;
 		printf("restoring %s\n", fname);
 		return backup_tdb(bak_name, fname, 0);
 	}
@@ -239,6 +243,7 @@ static void usage(void)
 	printf("   -s suffix     set the backup suffix\n");
 	printf("   -v            verify mode (restore if corrupt)\n");
 	printf("   -n hashsize   set the new hash size for the backup\n");
+	printf("   -c            check status (do not restore if corrupt)\n");
 }
 		
 
@@ -249,9 +254,10 @@ static void usage(void)
 	int c;
 	int verify = 0;
 	int hashsize = 0;
+	int check_status = 0;
 	const char *suffix = ".bak";
 
-	while ((c = getopt(argc, argv, "vhs:n:")) != -1) {
+	while ((c = getopt(argc, argv, "vhs:n:c")) != -1) {
 		switch (c) {
 		case 'h':
 			usage();
@@ -265,6 +271,9 @@ static void usage(void)
 		case 'n':
 			hashsize = atoi(optarg);
 			break;
+		case 'c':
+			check_status = 1;
+			break;
 		}
 	}
 
@@ -282,14 +291,20 @@ static void usage(void)
 
 		bak_name = add_suffix(fname, suffix);
 
-		if (verify) {
-			if (verify_tdb(fname, bak_name) != 0) {
+		if(check_status) {
+			if (verify_tdb(fname, NULL, 1) != 0) {
 				ret = 1;
 			}
 		} else {
-			if (file_newer(fname, bak_name) &&
-			    backup_tdb(fname, bak_name, hashsize) != 0) {
-				ret = 1;
+			if (verify) {
+				if (verify_tdb(fname, bak_name, 0) != 0) {
+					ret = 1;
+				}
+			} else {
+				if (file_newer(fname, bak_name) &&
+					backup_tdb(fname, bak_name, hashsize) != 0) {
+					ret = 1;
+				}
 			}
 		}
 

-------------- next part --------------
A non-text attachment was scrubbed...
Name: tdbbackup_check_status.patch
Type: application/octet-stream
Size: 2198 bytes
Desc: tdbbackup_check_status.patch
Url : http://lists.samba.org/archive/samba-technical/attachments/20081112/8342ccc8/tdbbackup_check_status.obj


More information about the samba-technical mailing list