-/*
- *
- * Program to check a Bacula database for consistency and to
- * make repairs
- *
- * Kern E. Sibbald, August 2002
- *
- * Version $Id$
- *
- */
/*
Bacula® - The Network Backup Solution
- Copyright (C) 2002-2006 Free Software Foundation Europe e.V.
+ Copyright (C) 2002-2007 Free Software Foundation Europe e.V.
The main author of Bacula is Kern Sibbald, with contributions from
many others, a complete list can be found in the file AUTHORS.
This program is Free Software; you can redistribute it and/or
modify it under the terms of version two of the GNU General Public
- License as published by the Free Software Foundation plus additions
- that are listed in the file LICENSE.
+ License as published by the Free Software Foundation and included
+ in the file LICENSE.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
(FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
Switzerland, email:ftf@fsfeurope.org.
*/
+/*
+ *
+ * Program to check a Bacula database for consistency and to
+ * make repairs
+ *
+ * Kern E. Sibbald, August 2002
+ *
+ * Version $Id$
+ *
+ */
#include "bacula.h"
#include "cats/cats.h"
" -b batch mode\n"
" -C catalog name in the director conf file\n"
" -c director conf filename\n"
-" -dnn set debug level to nn\n"
+" -d <nn> set debug level to <nn>\n"
+" -dt print timestamp in debug output\n"
" -f fix inconsistencies\n"
" -v verbose\n"
" -? print this message\n\n");
break;
case 'd': /* debug level */
- debug_level = atoi(optarg);
- if (debug_level <= 0)
- debug_level = 1;
+ if (*optarg == 't') {
+ dbg_timestamp = true;
+ } else {
+ debug_level = atoi(optarg);
+ if (debug_level <= 0) {
+ debug_level = 1;
+ }
+ }
break;
case 'f': /* fix inconsistencies */
/* Loop through list of duplicate names */
for (int i=0; i<name_list.num_ids; i++) {
/* Get all the Ids of each name */
- db_escape_string(esc_name, name_list.name[i], strlen(name_list.name[i]));
+ db_escape_string(NULL, db, esc_name, name_list.name[i], strlen(name_list.name[i]));
bsnprintf(buf, sizeof(buf), "SELECT FilenameId FROM Filename WHERE Name='%s'", esc_name);
if (verbose > 1) {
printf("%s\n", buf);
/* Loop through list of duplicate names */
for (int i=0; i<name_list.num_ids; i++) {
/* Get all the Ids of each name */
- db_escape_string(esc_name, name_list.name[i], strlen(name_list.name[i]));
+ db_escape_string(NULL, db, esc_name, name_list.name[i], strlen(name_list.name[i]));
bsnprintf(buf, sizeof(buf), "SELECT PathId FROM Path WHERE Path='%s'", esc_name);
if (verbose > 1) {
printf("%s\n", buf);
static void eliminate_orphaned_jobmedia_records()
{
- const char *query;
+ const char *query = "SELECT JobMedia.JobMediaId,Job.JobId FROM JobMedia "
+ "LEFT OUTER JOIN Job ON (JobMedia.JobId=Job.JobId) "
+ "WHERE Job.JobId IS NULL LIMIT 300000";
printf(_("Checking for orphaned JobMedia entries.\n"));
- query = "SELECT JobMedia.JobMediaId,Job.JobId FROM JobMedia "
- "LEFT OUTER JOIN Job ON (JobMedia.JobId=Job.JobId) "
- "WHERE Job.JobId IS NULL";
if (!make_id_list(query, &id_list)) {
exit(1);
}
- printf(_("Found %d orphaned JobMedia records.\n"), id_list.num_ids);
- if (id_list.num_ids && verbose && yes_no(_("Print them? (yes/no): "))) {
- for (int i=0; i < id_list.num_ids; i++) {
- char ed1[50];
- bsnprintf(buf, sizeof(buf),
+ /* Loop doing 300000 at a time */
+ while (id_list.num_ids != 0) {
+ printf(_("Found %d orphaned JobMedia records.\n"), id_list.num_ids);
+ if (id_list.num_ids && verbose && yes_no(_("Print them? (yes/no): "))) {
+ for (int i=0; i < id_list.num_ids; i++) {
+ char ed1[50];
+ bsnprintf(buf, sizeof(buf),
"SELECT JobMedia.JobMediaId,JobMedia.JobId,Media.VolumeName FROM JobMedia,Media "
"WHERE JobMedia.JobMediaId=%s AND Media.MediaId=JobMedia.MediaId",
- edit_int64(id_list.Id[i], ed1));
- if (!db_sql_query(db, buf, print_jobmedia_handler, NULL)) {
- printf("%s\n", db_strerror(db));
+ edit_int64(id_list.Id[i], ed1));
+ if (!db_sql_query(db, buf, print_jobmedia_handler, NULL)) {
+ printf("%s\n", db_strerror(db));
+ }
}
}
- }
- if (quit) {
- return;
- }
+ if (quit) {
+ return;
+ }
- if (fix && id_list.num_ids > 0) {
- printf(_("Deleting %d orphaned JobMedia records.\n"), id_list.num_ids);
- delete_id_list("DELETE FROM JobMedia WHERE JobMediaId=%s", &id_list);
+ if (fix && id_list.num_ids > 0) {
+ printf(_("Deleting %d orphaned JobMedia records.\n"), id_list.num_ids);
+ delete_id_list("DELETE FROM JobMedia WHERE JobMediaId=%s", &id_list);
+ } else {
+ break; /* get out if not updating db */
+ }
+ if (!make_id_list(query, &id_list)) {
+ exit(1);
+ }
}
}
static void eliminate_orphaned_file_records()
{
- const char *query;
+ const char *query = "SELECT File.FileId,Job.JobId FROM File "
+ "LEFT OUTER JOIN Job ON (File.JobId=Job.JobId) "
+ "WHERE Job.JobId IS NULL LIMIT 300000";
printf(_("Checking for orphaned File entries. This may take some time!\n"));
- query = "SELECT File.FileId,Job.JobId FROM File "
- "LEFT OUTER JOIN Job ON (File.JobId=Job.JobId) "
- "WHERE Job.JobId IS NULL";
if (verbose > 1) {
printf("%s\n", query);
}
if (!make_id_list(query, &id_list)) {
exit(1);
}
- printf(_("Found %d orphaned File records.\n"), id_list.num_ids);
- if (name_list.num_ids && verbose && yes_no(_("Print them? (yes/no): "))) {
- for (int i=0; i < id_list.num_ids; i++) {
- char ed1[50];
- bsnprintf(buf, sizeof(buf),
+ /* Loop doing 300000 at a time */
+ while (id_list.num_ids != 0) {
+ printf(_("Found %d orphaned File records.\n"), id_list.num_ids);
+ if (name_list.num_ids && verbose && yes_no(_("Print them? (yes/no): "))) {
+ for (int i=0; i < id_list.num_ids; i++) {
+ char ed1[50];
+ bsnprintf(buf, sizeof(buf),
"SELECT File.FileId,File.JobId,Filename.Name FROM File,Filename "
"WHERE File.FileId=%s AND File.FilenameId=Filename.FilenameId",
- edit_int64(id_list.Id[i], ed1));
- if (!db_sql_query(db, buf, print_file_handler, NULL)) {
- printf("%s\n", db_strerror(db));
+ edit_int64(id_list.Id[i], ed1));
+ if (!db_sql_query(db, buf, print_file_handler, NULL)) {
+ printf("%s\n", db_strerror(db));
+ }
}
}
- }
- if (quit) {
- return;
- }
- if (fix && id_list.num_ids > 0) {
- printf(_("Deleting %d orphaned File records.\n"), id_list.num_ids);
- delete_id_list("DELETE FROM File WHERE FileId=%s", &id_list);
+ if (quit) {
+ return;
+ }
+ if (fix && id_list.num_ids > 0) {
+ printf(_("Deleting %d orphaned File records.\n"), id_list.num_ids);
+ delete_id_list("DELETE FROM File WHERE FileId=%s", &id_list);
+ } else {
+ break; /* get out if not updating db */
+ }
+ if (!make_id_list(query, &id_list)) {
+ exit(1);
+ }
}
}
static void eliminate_orphaned_path_records()
{
- const char *query;
+ const char *query = "SELECT DISTINCT Path.PathId,File.PathId FROM Path "
+ "LEFT OUTER JOIN File ON (Path.PathId=File.PathId) "
+ "WHERE File.PathId IS NULL LIMIT 300000";
printf(_("Checking for orphaned Path entries. This may take some time!\n"));
- query = "SELECT DISTINCT Path.PathId,File.PathId FROM Path "
- "LEFT OUTER JOIN File ON (Path.PathId=File.PathId) "
- "WHERE File.PathId IS NULL";
if (verbose > 1) {
printf("%s\n", query);
}
if (!make_id_list(query, &id_list)) {
exit(1);
}
- printf(_("Found %d orphaned Path records.\n"), id_list.num_ids);
- if (id_list.num_ids && verbose && yes_no(_("Print them? (yes/no): "))) {
- for (int i=0; i < id_list.num_ids; i++) {
- char ed1[50];
- bsnprintf(buf, sizeof(buf), "SELECT Path FROM Path WHERE PathId=%s",
- edit_int64(id_list.Id[i], ed1));
- db_sql_query(db, buf, print_name_handler, NULL);
+ /* Loop doing 300000 at a time */
+ while (id_list.num_ids != 0) {
+ printf(_("Found %d orphaned Path records.\n"), id_list.num_ids);
+ if (id_list.num_ids && verbose && yes_no(_("Print them? (yes/no): "))) {
+ for (int i=0; i < id_list.num_ids; i++) {
+ char ed1[50];
+ bsnprintf(buf, sizeof(buf), "SELECT Path FROM Path WHERE PathId=%s",
+ edit_int64(id_list.Id[i], ed1));
+ db_sql_query(db, buf, print_name_handler, NULL);
+ }
+ }
+ if (quit) {
+ return;
+ }
+ if (fix && id_list.num_ids > 0) {
+ printf(_("Deleting %d orphaned Path records.\n"), id_list.num_ids);
+ delete_id_list("DELETE FROM Path WHERE PathId=%s", &id_list);
+ } else {
+ break; /* get out if not updating db */
+ }
+ if (!make_id_list(query, &id_list)) {
+ exit(1);
}
- }
- if (quit) {
- return;
- }
- if (fix && id_list.num_ids > 0) {
- printf(_("Deleting %d orphaned Path records.\n"), id_list.num_ids);
- delete_id_list("DELETE FROM Path WHERE PathId=%s", &id_list);
}
}
static void eliminate_orphaned_filename_records()
{
- const char *query;
+ const char *query = "SELECT Filename.FilenameId,File.FilenameId FROM Filename "
+ "LEFT OUTER JOIN File ON (Filename.FilenameId=File.FilenameId) "
+ "WHERE File.FilenameId IS NULL LIMIT 300000";
printf(_("Checking for orphaned Filename entries. This may take some time!\n"));
- query = "SELECT Filename.FilenameId,File.FilenameId FROM Filename "
- "LEFT OUTER JOIN File ON (Filename.FilenameId=File.FilenameId) "
- "WHERE File.FilenameId IS NULL";
if (verbose > 1) {
printf("%s\n", query);
}
if (!make_id_list(query, &id_list)) {
exit(1);
}
- printf(_("Found %d orphaned Filename records.\n"), id_list.num_ids);
- if (id_list.num_ids && verbose && yes_no(_("Print them? (yes/no): "))) {
- for (int i=0; i < id_list.num_ids; i++) {
- char ed1[50];
- bsnprintf(buf, sizeof(buf), "SELECT Name FROM Filename WHERE FilenameId=%s",
- edit_int64(id_list.Id[i], ed1));
- db_sql_query(db, buf, print_name_handler, NULL);
+ /* Loop doing 300000 at a time */
+ while (id_list.num_ids != 0) {
+ printf(_("Found %d orphaned Filename records.\n"), id_list.num_ids);
+ if (id_list.num_ids && verbose && yes_no(_("Print them? (yes/no): "))) {
+ for (int i=0; i < id_list.num_ids; i++) {
+ char ed1[50];
+ bsnprintf(buf, sizeof(buf), "SELECT Name FROM Filename WHERE FilenameId=%s",
+ edit_int64(id_list.Id[i], ed1));
+ db_sql_query(db, buf, print_name_handler, NULL);
+ }
+ }
+ if (quit) {
+ return;
+ }
+ if (fix && id_list.num_ids > 0) {
+ printf(_("Deleting %d orphaned Filename records.\n"), id_list.num_ids);
+ delete_id_list("DELETE FROM Filename WHERE FilenameId=%s", &id_list);
+ } else {
+ break; /* get out if not updating db */
+ }
+ if (!make_id_list(query, &id_list)) {
+ exit(1);
}
- }
- if (quit) {
- return;
- }
- if (fix && id_list.num_ids > 0) {
- printf(_("Deleting %d orphaned Filename records.\n"), id_list.num_ids);
- delete_id_list("DELETE FROM Filename WHERE FilenameId=%s", &id_list);
}
}
esc_name[1] = 0;
} else {
name[len-1] = 0;
- db_escape_string(esc_name, name, len);
+ db_escape_string(NULL, db, esc_name, name, len);
}
bsnprintf(buf, sizeof(buf),
"UPDATE Filename SET Name='%s' WHERE FilenameId=%s",
}
/* Add trailing slash */
len = pm_strcat(&name, "/");
- db_escape_string(esc_name, name, len);
+ db_escape_string(NULL, db, esc_name, name, len);
bsnprintf(buf, sizeof(buf), "UPDATE Path SET Path='%s' WHERE PathId=%s",
esc_name, edit_int64(id_list.Id[i], ed1));
if (verbose > 1) {