/*
Bacula® - The Network Backup Solution
- Copyright (C) 2000-2010 Free Software Foundation Europe e.V.
+ Copyright (C) 2000-2012 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.
*
*/
+#include "bacula.h"
-/**
- * The following is necessary so that we do not include
- * the dummy external definition of DB.
- */
-#define __SQL_C /* indicate that this is sql.c */
+#if HAVE_SQLITE3 || HAVE_MYSQL || HAVE_POSTGRESQL || HAVE_INGRES || HAVE_DBI
-#include "bacula.h"
#include "cats.h"
-
-#if HAVE_SQLITE3 || HAVE_MYSQL || HAVE_SQLITE || HAVE_POSTGRESQL || HAVE_INGRES || HAVE_DBI
+#include "bdb_priv.h"
+#include "sql_glue.h"
/* -----------------------------------------------------------------------
*
* use includes the directory twice. In this case, Verify
* VolumeToCatalog fails because we have two copies in the catalog,
* and only the first one is marked (twice). So, when calling from Verify,
- * jr is not NULL and we know jr->FileIndex is the fileindex
+ * VolumeToCatalog jr is not NULL and we know jr->FileIndex is the fileindex
* of the version of the directory/file we actually want and do
* a more explicit SQL search.
*/
SQL_ROW row;
int stat = 0;
char ed1[50], ed2[50], ed3[50];
+ int num_rows;
if (jcr->getJobLevel() == L_VERIFY_DISK_TO_CATALOG) {
Mmsg(mdb->cmd,
edit_int64(fdbr->PathId, ed1),
edit_int64(fdbr->FilenameId, ed2),
edit_int64(jr->ClientId,ed3));
+ } else if (jcr->getJobLevel() == L_VERIFY_VOLUME_TO_CATALOG) {
+ Mmsg(mdb->cmd,
+ "SELECT FileId, LStat, MD5 FROM File WHERE File.JobId=%s AND File.PathId=%s AND "
+ "File.FilenameId=%s AND File.FileIndex=%u",
+ edit_int64(fdbr->JobId, ed1),
+ edit_int64(fdbr->PathId, ed2),
+ edit_int64(fdbr->FilenameId,ed3),
+ jr->FileIndex);
} else {
Mmsg(mdb->cmd,
"SELECT FileId, LStat, MD5 FROM File WHERE File.JobId=%s AND File.PathId=%s AND "
Dmsg1(100, "Query=%s\n", mdb->cmd);
if (QUERY_DB(jcr, mdb, mdb->cmd)) {
- mdb->num_rows = sql_num_rows(mdb);
- Dmsg1(050, "get_file_record num_rows=%d\n", (int)mdb->num_rows);
- if (mdb->num_rows >= 1) {
+ num_rows = sql_num_rows(mdb);
+ Dmsg1(050, "get_file_record num_rows=%d\n", num_rows);
+ if (num_rows >= 1) {
if ((row = sql_fetch_row(mdb)) == NULL) {
Mmsg1(mdb->errmsg, _("Error fetching row: %s\n"), sql_strerror(mdb));
} else {
bstrncpy(fdbr->LStat, row[1], sizeof(fdbr->LStat));
bstrncpy(fdbr->Digest, row[2], sizeof(fdbr->Digest));
stat = 1;
- if (mdb->num_rows > 1) {
+ if (num_rows > 1) {
Mmsg3(mdb->errmsg, _("get_file_record want 1 got rows=%d PathId=%s FilenameId=%s\n"),
- mdb->num_rows,
+ num_rows,
edit_int64(fdbr->PathId, ed1),
edit_int64(fdbr->FilenameId, ed2));
Dmsg1(000, "=== Problem! %s", mdb->errmsg);
{
SQL_ROW row;
int FilenameId = 0;
+ int num_rows;
mdb->esc_name = check_pool_memory_size(mdb->esc_name, 2*mdb->fnl+2);
db_escape_string(jcr, mdb, mdb->esc_name, mdb->fname, mdb->fnl);
Mmsg(mdb->cmd, "SELECT FilenameId FROM Filename WHERE Name='%s'", mdb->esc_name);
if (QUERY_DB(jcr, mdb, mdb->cmd)) {
char ed1[30];
- mdb->num_rows = sql_num_rows(mdb);
- if (mdb->num_rows > 1) {
+ num_rows = sql_num_rows(mdb);
+ if (num_rows > 1) {
Mmsg2(mdb->errmsg, _("More than one Filename!: %s for file: %s\n"),
- edit_uint64(mdb->num_rows, ed1), mdb->fname);
+ edit_uint64(num_rows, ed1), mdb->fname);
Jmsg(jcr, M_WARNING, 0, "%s", mdb->errmsg);
}
- if (mdb->num_rows >= 1) {
+ if (num_rows >= 1) {
if ((row = sql_fetch_row(mdb)) == NULL) {
Mmsg1(mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
} else {
{
SQL_ROW row;
uint32_t PathId = 0;
+ int num_rows;
mdb->esc_name = check_pool_memory_size(mdb->esc_name, 2*mdb->pnl+2);
db_escape_string(jcr, mdb, mdb->esc_name, mdb->path, mdb->pnl);
if (QUERY_DB(jcr, mdb, mdb->cmd)) {
char ed1[30];
- mdb->num_rows = sql_num_rows(mdb);
- if (mdb->num_rows > 1) {
+ num_rows = sql_num_rows(mdb);
+ if (num_rows > 1) {
Mmsg2(mdb->errmsg, _("More than one Path!: %s for path: %s\n"),
- edit_uint64(mdb->num_rows, ed1), mdb->path);
+ edit_uint64(num_rows, ed1), mdb->path);
Jmsg(jcr, M_WARNING, 0, "%s", mdb->errmsg);
}
/* Even if there are multiple paths, take the first one */
- if (mdb->num_rows >= 1) {
+ if (num_rows >= 1) {
if ((row = sql_fetch_row(mdb)) == NULL) {
Mmsg1(mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
} else {
{
SQL_ROW row;
char ed1[50];
+ char esc[MAX_ESCAPE_NAME_LENGTH];
db_lock(mdb);
if (jr->JobId == 0) {
+ mdb->db_escape_string(jcr, esc, jr->Job, strlen(jr->Job));
Mmsg(mdb->cmd, "SELECT VolSessionId,VolSessionTime,"
"PoolId,StartTime,EndTime,JobFiles,JobBytes,JobTDate,Job,JobStatus,"
"Type,Level,ClientId,Name,PriorJobId,RealEndTime,JobId,FileSetId,"
"SchedTime,RealEndTime,ReadBytes,HasBase,PurgedFiles "
-"FROM Job WHERE Job='%s'", jr->Job);
+"FROM Job WHERE Job='%s'", esc);
} else {
Mmsg(mdb->cmd, "SELECT VolSessionId,VolSessionTime,"
"PoolId,StartTime,EndTime,JobFiles,JobBytes,JobTDate,Job,JobStatus,"
char ed1[50];
int stat = 0;
int i;
+ int num_rows;
db_lock(mdb);
/* Get one entry per VolumeName, but "sort" by VolIndex */
Dmsg1(130, "VolNam=%s\n", mdb->cmd);
*VolumeNames[0] = 0;
if (QUERY_DB(jcr, mdb, mdb->cmd)) {
- mdb->num_rows = sql_num_rows(mdb);
- Dmsg1(130, "Num rows=%d\n", mdb->num_rows);
- if (mdb->num_rows <= 0) {
+ num_rows = sql_num_rows(mdb);
+ Dmsg1(130, "Num rows=%d\n", num_rows);
+ if (num_rows <= 0) {
Mmsg1(mdb->errmsg, _("No volumes found for JobId=%d\n"), JobId);
stat = 0;
} else {
- stat = mdb->num_rows;
+ stat = num_rows;
for (i=0; i < stat; i++) {
if ((row = sql_fetch_row(mdb)) == NULL) {
Mmsg2(mdb->errmsg, _("Error fetching row %d: ERR=%s\n"), i, sql_strerror(mdb));
int stat = 0;
int i;
VOL_PARAMS *Vols = NULL;
+ int num_rows;
db_lock(mdb);
Mmsg(mdb->cmd,
Dmsg1(130, "VolNam=%s\n", mdb->cmd);
if (QUERY_DB(jcr, mdb, mdb->cmd)) {
- mdb->num_rows = sql_num_rows(mdb);
- Dmsg1(200, "Num rows=%d\n", mdb->num_rows);
- if (mdb->num_rows <= 0) {
+ num_rows = sql_num_rows(mdb);
+ Dmsg1(200, "Num rows=%d\n", num_rows);
+ if (num_rows <= 0) {
Mmsg1(mdb->errmsg, _("No volumes found for JobId=%d\n"), JobId);
stat = 0;
} else {
- stat = mdb->num_rows;
+ stat = num_rows;
DBId_t *SId = NULL;
if (stat > 0) {
*VolParams = Vols = (VOL_PARAMS *)malloc(stat * sizeof(VOL_PARAMS));
SQL_ROW row;
bool ok = false;
char ed1[50];
+ int num_rows;
+ char esc[MAX_ESCAPE_NAME_LENGTH];
db_lock(mdb);
if (pdbr->PoolId != 0) { /* find by id */
"ActionOnPurge FROM Pool WHERE Pool.PoolId=%s",
edit_int64(pdbr->PoolId, ed1));
} else { /* find by name */
+ mdb->db_escape_string(jcr, esc, pdbr->Name, strlen(pdbr->Name));
Mmsg(mdb->cmd,
"SELECT PoolId,Name,NumVols,MaxVols,UseOnce,UseCatalog,AcceptAnyVolume,"
"AutoPrune,Recycle,VolRetention,VolUseDuration,MaxVolJobs,MaxVolFiles,"
"MaxVolBytes,PoolType,LabelType,LabelFormat,RecyclePoolId,ScratchPoolId,"
-"ActionOnPurge FROM Pool WHERE Pool.Name='%s'",
- pdbr->Name);
+"ActionOnPurge FROM Pool WHERE Pool.Name='%s'", esc);
}
if (QUERY_DB(jcr, mdb, mdb->cmd)) {
- mdb->num_rows = sql_num_rows(mdb);
- if (mdb->num_rows > 1) {
+ num_rows = sql_num_rows(mdb);
+ if (num_rows > 1) {
char ed1[30];
Mmsg1(mdb->errmsg, _("More than one Pool!: %s\n"),
- edit_uint64(mdb->num_rows, ed1));
+ edit_uint64(num_rows, ed1));
Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
- } else if (mdb->num_rows == 1) {
+ } else if (num_rows == 1) {
if ((row = sql_fetch_row(mdb)) == NULL) {
Mmsg1(mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
SQL_ROW row;
int stat = 0;
char ed1[50];
+ int num_rows;
+ char esc[MAX_ESCAPE_NAME_LENGTH];
db_lock(mdb);
if (cdbr->ClientId != 0) { /* find by id */
"FROM Client WHERE Client.ClientId=%s",
edit_int64(cdbr->ClientId, ed1));
} else { /* find by name */
+ mdb->db_escape_string(jcr, esc, cdbr->Name, strlen(cdbr->Name));
Mmsg(mdb->cmd,
"SELECT ClientId,Name,Uname,AutoPrune,FileRetention,JobRetention "
-"FROM Client WHERE Client.Name='%s'", cdbr->Name);
+"FROM Client WHERE Client.Name='%s'", esc);
}
if (QUERY_DB(jcr, mdb, mdb->cmd)) {
- mdb->num_rows = sql_num_rows(mdb);
- if (mdb->num_rows > 1) {
+ num_rows = sql_num_rows(mdb);
+ if (num_rows > 1) {
Mmsg1(mdb->errmsg, _("More than one Client!: %s\n"),
- edit_uint64(mdb->num_rows, ed1));
+ edit_uint64(num_rows, ed1));
Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
- } else if (mdb->num_rows == 1) {
+ } else if (num_rows == 1) {
if ((row = sql_fetch_row(mdb)) == NULL) {
Mmsg1(mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
int db_get_counter_record(JCR *jcr, B_DB *mdb, COUNTER_DBR *cr)
{
SQL_ROW row;
+ int num_rows;
+ char esc[MAX_ESCAPE_NAME_LENGTH];
db_lock(mdb);
- Mmsg(mdb->cmd, "SELECT MinValue,MaxValue,CurrentValue,WrapCounter "
- "FROM Counters WHERE Counter='%s'", cr->Counter);
+ mdb->db_escape_string(jcr, esc, cr->Counter, strlen(cr->Counter));
+
+ Mmsg(mdb->cmd, "SELECT Counters.MinValue,Counters.MaxValue,CurrentValue,WrapCounter "
+ "FROM Counters WHERE Counter='%s'", esc);
if (QUERY_DB(jcr, mdb, mdb->cmd)) {
- mdb->num_rows = sql_num_rows(mdb);
+ num_rows = sql_num_rows(mdb);
/* If more than one, report error, but return first row */
- if (mdb->num_rows > 1) {
- Mmsg1(mdb->errmsg, _("More than one Counter!: %d\n"), (int)(mdb->num_rows));
+ if (num_rows > 1) {
+ Mmsg1(mdb->errmsg, _("More than one Counter!: %d\n"), num_rows);
Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
}
- if (mdb->num_rows >= 1) {
+ if (num_rows >= 1) {
if ((row = sql_fetch_row(mdb)) == NULL) {
Mmsg1(mdb->errmsg, _("error fetching Counter row: %s\n"), sql_strerror(mdb));
Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
SQL_ROW row;
int stat = 0;
char ed1[50];
+ int num_rows;
+ char esc[MAX_ESCAPE_NAME_LENGTH];
db_lock(mdb);
if (fsr->FileSetId != 0) { /* find by id */
"WHERE FileSetId=%s",
edit_int64(fsr->FileSetId, ed1));
} else { /* find by name */
+ mdb->db_escape_string(jcr, esc, fsr->FileSet, strlen(fsr->FileSet));
Mmsg(mdb->cmd,
"SELECT FileSetId,FileSet,MD5,CreateTime FROM FileSet "
- "WHERE FileSet='%s' ORDER BY CreateTime DESC LIMIT 1", fsr->FileSet);
+ "WHERE FileSet='%s' ORDER BY CreateTime DESC LIMIT 1", esc);
}
if (QUERY_DB(jcr, mdb, mdb->cmd)) {
- mdb->num_rows = sql_num_rows(mdb);
- if (mdb->num_rows > 1) {
+ num_rows = sql_num_rows(mdb);
+ if (num_rows > 1) {
char ed1[30];
Mmsg1(mdb->errmsg, _("Error got %s FileSets but expected only one!\n"),
- edit_uint64(mdb->num_rows, ed1));
- sql_data_seek(mdb, mdb->num_rows-1);
+ edit_uint64(num_rows, ed1));
+ sql_data_seek(mdb, num_rows-1);
}
if ((row = sql_fetch_row(mdb)) == NULL) {
Mmsg1(mdb->errmsg, _("FileSet record \"%s\" not found.\n"), fsr->FileSet);
SQL_ROW row;
char ed1[50];
bool ok = false;
+ int num_rows;
+ char esc[MAX_ESCAPE_NAME_LENGTH];
db_lock(mdb);
if (mr->MediaId == 0 && mr->VolumeName[0] == 0) {
"FROM Media WHERE MediaId=%s",
edit_int64(mr->MediaId, ed1));
} else { /* find by name */
+ mdb->db_escape_string(jcr, esc, mr->VolumeName, strlen(mr->VolumeName));
Mmsg(mdb->cmd, "SELECT MediaId,VolumeName,VolJobs,VolFiles,VolBlocks,"
"VolBytes,VolMounts,VolErrors,VolWrites,MaxVolBytes,VolCapacityBytes,"
"MediaType,VolStatus,PoolId,VolRetention,VolUseDuration,MaxVolJobs,"
"EndFile,EndBlock,VolParts,LabelType,LabelDate,StorageId,"
"Enabled,LocationId,RecycleCount,InitialWrite,"
"ScratchPoolId,RecyclePoolId,VolReadTime,VolWriteTime,ActionOnPurge "
- "FROM Media WHERE VolumeName='%s'", mr->VolumeName);
+ "FROM Media WHERE VolumeName='%s'", esc);
}
if (QUERY_DB(jcr, mdb, mdb->cmd)) {
char ed1[50];
- mdb->num_rows = sql_num_rows(mdb);
- if (mdb->num_rows > 1) {
+ num_rows = sql_num_rows(mdb);
+ if (num_rows > 1) {
Mmsg1(mdb->errmsg, _("More than one Volume!: %s\n"),
- edit_uint64(mdb->num_rows, ed1));
+ edit_uint64(num_rows, ed1));
Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
- } else if (mdb->num_rows == 1) {
+ } else if (num_rows == 1) {
if ((row = sql_fetch_row(mdb)) == NULL) {
Mmsg1(mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
return ok;
}
+/* Remove all MD5 from a query (can save lot of memory with many files) */
+static void strip_md5(char *q)
+{
+ char *p = q;
+ while ((p = strstr(p, ", MD5"))) {
+ memset(p, ' ', 5 * sizeof(char));
+ }
+}
+
/**
* Find the last "accurate" backup state (that can take deleted files in
* account)
*
* TODO: See if we can do the SORT only if needed (as an argument)
*/
-bool db_get_file_list(JCR *jcr, B_DB *mdb, char *jobids,
+bool db_get_file_list(JCR *jcr, B_DB *mdb, char *jobids,
+ bool use_md5, bool use_delta,
DB_RESULT_HANDLER *result_handler, void *ctx)
{
if (!*jobids) {
return false;
}
POOL_MEM buf(PM_MESSAGE);
-
-#define new_db_get_file_list
-#ifdef new_db_get_file_list
POOL_MEM buf2(PM_MESSAGE);
- Mmsg(buf2, select_recent_version_with_basejob[db_type],
- jobids, jobids, jobids, jobids);
+ if (use_delta) {
+ Mmsg(buf2, select_recent_version_with_basejob_and_delta[db_get_type_index(mdb)],
+ jobids, jobids, jobids, jobids);
+
+ } else {
+ Mmsg(buf2, select_recent_version_with_basejob[db_get_type_index(mdb)],
+ jobids, jobids, jobids, jobids);
+ }
+
+ /* bsr code is optimized for JobId sorted, with Delta, we need to get
+ * them ordered by date. JobTDate and JobId can be mixed if using Copy
+ * or Migration
+ */
Mmsg(buf,
-"SELECT Path.Path, Filename.Name, Temp.FileIndex, Temp.JobId, LStat, MD5 "
- "FROM ( %s ) AS Temp "
- "JOIN Filename ON (Filename.FilenameId = Temp.FilenameId) "
- "JOIN Path ON (Path.PathId = Temp.PathId) "
+"SELECT Path.Path, Filename.Name, T1.FileIndex, T1.JobId, LStat, DeltaSeq, MD5 "
+ "FROM ( %s ) AS T1 "
+ "JOIN Filename ON (Filename.FilenameId = T1.FilenameId) "
+ "JOIN Path ON (Path.PathId = T1.PathId) "
"WHERE FileIndex > 0 "
-"ORDER BY Temp.JobId, FileIndex ASC",/* Return sorted by JobId, */
- /* FileIndex for restore code */
+"ORDER BY T1.JobTDate, FileIndex ASC",/* Return sorted by JobTDate */
+ /* FileIndex for restore code */
buf2.c_str());
-#else
- /*
- * I am not sure that this works the same as the code in ua_restore.c but it
- * is very similar. The accurate-test fails in a restore. Bad file count.
- */
- Mmsg(buf, uar_sel_files, jobids);
-#endif
- return db_sql_query(mdb, buf.c_str(), result_handler, ctx);
+ if (!use_md5) {
+ strip_md5(buf.c_str());
+ }
+
+ Dmsg1(100, "q=%s\n", buf.c_str());
+
+ return db_big_sql_query(mdb, buf.c_str(), result_handler, ctx);
}
/**
jobids->reset();
/* First, find the last good Full backup for this job/client/fileset */
- Mmsg(query, create_temp_accurate_jobids[db_type],
+ Mmsg(query, create_temp_accurate_jobids[db_get_type_index(mdb)],
edit_uint64(jcr->JobId, jobid),
edit_uint64(jr->ClientId, clientid),
date,
return ret;
}
-bool db_get_base_file_list(JCR *jcr, B_DB *mdb,
+bool db_get_base_file_list(JCR *jcr, B_DB *mdb, bool use_md5,
DB_RESULT_HANDLER *result_handler, void *ctx)
{
POOL_MEM buf(PM_MESSAGE);
Mmsg(buf,
- "SELECT Path, Name, FileIndex, JobId, LStat, MD5 "
+ "SELECT Path, Name, FileIndex, JobId, LStat, 0 As DeltaSeq, MD5 "
"FROM new_basefile%lld ORDER BY JobId, FileIndex ASC",
(uint64_t) jcr->JobId);
-
+
+ if (!use_md5) {
+ strip_md5(buf.c_str());
+ }
return db_sql_query(mdb, buf.c_str(), result_handler, ctx);
}
utime_t StartTime;
db_int64_ctx lctx;
char date[MAX_TIME_LENGTH];
+ char esc[MAX_ESCAPE_NAME_LENGTH];
bool ret=false;
// char clientid[50], filesetid[50];
*jobid = 0;
StartTime = (jr->StartTime)?jr->StartTime:time(NULL);
bstrutime(date, sizeof(date), StartTime + 1);
-
+ mdb->db_escape_string(jcr, esc, jr->Name, strlen(jr->Name));
+
/* we can take also client name, fileset, etc... */
Mmsg(query,
// "AND Client.Name = '%s' "
"AND StartTime<'%s' "
"ORDER BY Job.JobTDate DESC LIMIT 1",
- jr->Name,
+ esc,
// edit_uint64(jr->ClientId, clientid),
// edit_uint64(jr->FileSetId, filesetid));
date);
return ret;
}
-#endif /* HAVE_SQLITE3 || HAVE_MYSQL || HAVE_SQLITE || HAVE_POSTGRESQL || HAVE_INGRES || HAVE_DBI */
+/* Get JobIds associated with a volume */
+bool db_get_volume_jobids(JCR *jcr, B_DB *mdb,
+ MEDIA_DBR *mr, db_list_ctx *lst)
+{
+ char ed1[50];
+ bool ret=false;
+
+ db_lock(mdb);
+ Mmsg(mdb->cmd, "SELECT DISTINCT JobId FROM JobMedia WHERE MediaId=%s",
+ edit_int64(mr->MediaId, ed1));
+ ret = db_sql_query(mdb, mdb->cmd, db_list_handler, lst);
+ db_unlock(mdb);
+ return ret;
+}
+
+#endif /* HAVE_SQLITE3 || HAVE_MYSQL || HAVE_POSTGRESQL || HAVE_INGRES || HAVE_DBI */