Skip to content

Commit

Permalink
Tweak queries a bit.
Browse files Browse the repository at this point in the history
Create a second query for finding the last full backup when no specific
pool is specified as then we don't need to JOIN the JobMedia and Media
table into the query as we don't select on the Media's PoolId.

Also drop the JOIN on Client as we don't select any column from that
table anyway and the query is also kind of clumbsy by specifying twice
the ClientId instead of using a single WHERE on the ClientId and use
an AND Client.ClientId = Job.ClientId.

Fixes #363: zero-byte Full backups are ignored for restore
  • Loading branch information
Marco van Wieringen committed Feb 17, 2015
1 parent 24de15e commit b8ef82e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 23 deletions.
17 changes: 14 additions & 3 deletions src/cats/sql_cmds.c
Expand Up @@ -133,8 +133,8 @@ const char *uar_del_temp1 = "DROP TABLE temp1";

const char *uar_last_full =
"INSERT INTO temp1 SELECT Job.JobId,JobTdate "
"FROM Client,Job,JobMedia,Media,FileSet WHERE Client.ClientId=%s "
"AND Job.ClientId=%s "
"FROM Job,JobMedia,Media,FileSet "
"WHERE Job.ClientId=%s "
"AND Job.StartTime < '%s' "
"AND Level='F' AND JobStatus IN ('T','W') AND Type='B' "
"AND JobMedia.JobId=Job.JobId "
Expand All @@ -145,11 +145,22 @@ const char *uar_last_full =
"%s"
"ORDER BY Job.JobTDate DESC LIMIT 1";

const char *uar_last_full_no_pool =
"INSERT INTO temp1 SELECT Job.JobId,JobTdate "
"FROM Job,FileSet "
"WHERE Job.ClientId=%s "
"AND Job.StartTime < '%s' "
"AND Level='F' AND JobStatus IN ('T','W') AND Type='B' "
"AND Job.FileSetId=FileSet.FileSetId "
"AND FileSet.FileSet='%s' "
"ORDER BY Job.JobTDate DESC LIMIT 1";

const char *uar_full =
"INSERT INTO temp SELECT Job.JobId,Job.JobTDate,"
"Job.ClientId,Job.Level,Job.JobFiles,Job.JobBytes,"
"StartTime,VolumeName,JobMedia.StartFile,VolSessionId,VolSessionTime "
"FROM temp1,Job,JobMedia,Media WHERE temp1.JobId=Job.JobId "
"FROM temp1,Job,JobMedia,Media "
"WHERE temp1.JobId=Job.JobId "
"AND Level='F' AND JobStatus IN ('T','W') AND Type='B' "
"AND Media.Enabled=1 "
"AND JobMedia.JobId=Job.JobId "
Expand Down
1 change: 1 addition & 0 deletions src/cats/sql_cmds.h
Expand Up @@ -40,6 +40,7 @@ extern const char CATS_IMP_EXP *uar_sel_files;
extern const char CATS_IMP_EXP *uar_del_temp;
extern const char CATS_IMP_EXP *uar_del_temp1;
extern const char CATS_IMP_EXP *uar_last_full;
extern const char CATS_IMP_EXP *uar_last_full_no_pool;
extern const char CATS_IMP_EXP *uar_full;
extern const char CATS_IMP_EXP *uar_inc;
extern const char CATS_IMP_EXP *uar_list_temp;
Expand Down
66 changes: 46 additions & 20 deletions src/dird/ua_restore.c
Expand Up @@ -1287,8 +1287,7 @@ static bool build_directory_tree(UAContext *ua, RESTORE_CTX *rx)
}

/*
* This routine is used to get the current backup or a backup
* before the specified date.
* This routine is used to get the current backup or a backup before the specified date.
*/
static bool select_backups_before_date(UAContext *ua, RESTORE_CTX *rx, char *date)
{
Expand Down Expand Up @@ -1356,7 +1355,9 @@ static bool select_backups_before_date(UAContext *ua, RESTORE_CTX *rx, char *dat
}
}

/* If Pool specified, add PoolId specification */
/*
* If Pool specified, add PoolId specification
*/
pool_select[0] = 0;
if (rx->pool) {
POOL_DBR pr;
Expand All @@ -1370,23 +1371,35 @@ static bool select_backups_before_date(UAContext *ua, RESTORE_CTX *rx, char *dat
}
}

/* Find JobId of last Full backup for this client, fileset */
edit_int64(cr.ClientId, ed1);
Mmsg(rx->query, uar_last_full, ed1, ed1, date, fsr.FileSet,
pool_select);
if (!db_sql_query(ua->db, rx->query)) {
ua->error_msg("%s\n", db_strerror(ua->db));
goto bail_out;
/*
* Find JobId of last Full backup for this client, fileset
*/
if (pool_select[0]) {
edit_int64(cr.ClientId, ed1);
Mmsg(rx->query, uar_last_full, ed1, date, fsr.FileSet, pool_select);
if (!db_sql_query(ua->db, rx->query)) {
ua->error_msg("%s\n", db_strerror(ua->db));
goto bail_out;
}
} else {
edit_int64(cr.ClientId, ed1);
Mmsg(rx->query, uar_last_full_no_pool, ed1, date, fsr.FileSet);
if (!db_sql_query(ua->db, rx->query)) {
ua->error_msg("%s\n", db_strerror(ua->db));
goto bail_out;
}
}

/* Find all Volumes used by that JobId */
/*
* Find all Volumes used by that JobId
*/
if (!db_sql_query(ua->db, uar_full)) {
ua->error_msg("%s\n", db_strerror(ua->db));
goto bail_out;
}

/* Note, this is needed because I don't seem to get the callback
* from the call just above.
/*
* Note, this is needed because I don't seem to get the callback from the call just above.
*/
rx->JobTDate = 0;
if (!db_sql_query(ua->db, uar_sel_all_temp1, last_full_handler, (void *)rx)) {
Expand All @@ -1397,13 +1410,18 @@ static bool select_backups_before_date(UAContext *ua, RESTORE_CTX *rx, char *dat
goto bail_out;
}

/* Now find most recent Differental Job after Full save, if any */
/*
* Now find most recent Differental Job after Full save, if any
*/
Mmsg(rx->query, uar_dif, edit_uint64(rx->JobTDate, ed1), date,
edit_int64(cr.ClientId, ed2), fsr.FileSet, pool_select);
if (!db_sql_query(ua->db, rx->query)) {
ua->warning_msg("%s\n", db_strerror(ua->db));
}
/* Now update JobTDate to look into Differental, if any */

/*
* Now update JobTDate to look into Differental, if any
*/
rx->JobTDate = 0;
if (!db_sql_query(ua->db, uar_sel_all_temp, last_full_handler, (void *)rx)) {
ua->warning_msg("%s\n", db_strerror(ua->db));
Expand All @@ -1413,14 +1431,18 @@ static bool select_backups_before_date(UAContext *ua, RESTORE_CTX *rx, char *dat
goto bail_out;
}

/* Now find all Incremental Jobs after Full/dif save */
/*
* Now find all Incremental Jobs after Full/dif save
*/
Mmsg(rx->query, uar_inc, edit_uint64(rx->JobTDate, ed1), date,
edit_int64(cr.ClientId, ed2), fsr.FileSet, pool_select);
if (!db_sql_query(ua->db, rx->query)) {
ua->warning_msg("%s\n", db_strerror(ua->db));
}

/* Get the JobIds from that list */
/*
* Get the JobIds from that list
*/
rx->last_jobid[0] = rx->JobIds[0] = 0;

if (!db_sql_query(ua->db, uar_sel_jobid_temp, jobid_handler, (void *)rx)) {
Expand All @@ -1429,14 +1451,18 @@ static bool select_backups_before_date(UAContext *ua, RESTORE_CTX *rx, char *dat

if (rx->JobIds[0] != 0) {
if (find_arg(ua, NT_("copies")) > 0) {
/* Display a list of all copies */
/*
* Display a list of all copies
*/
db_list_copies_records(ua->jcr, ua->db, 0, rx->JobIds,
prtit, ua, HORZ_LIST);
}
/* Display a list of Jobs selected for this restore */

/*
* Display a list of Jobs selected for this restore
*/
db_list_sql_query(ua->jcr, ua->db, uar_list_temp, prtit, ua, true, HORZ_LIST);
ok = true;

} else {
ua->warning_msg(_("No jobs found.\n"));
}
Expand Down

0 comments on commit b8ef82e

Please sign in to comment.