获取备份的列表
- 找到实例名的路径,backup_instance_path: /home/pjr/backup/backups/db_backup
// 拼出路径
nRet = snprintf_s(backup_instance_path,MAXPGPATH,MAXPGPATH - 1, "%s/%s/%s",
backup_path, BACKUPS_DIR, instance_name);
...
// 打开目录
data_dir = fio_opendir(backup_instance_path, FIO_BACKUP_HOST);
- 遍历目录中的每个文件,不是文件夹的就跳过。
for (; (data_ent = fio_readdir(data_dir)) != NULL; errno = 0)
{
char backup_conf_path[MAXPGPATH];
char data_path[MAXPGPATH];
pgBackup *backup = NULL;
/* skip not-directory entries and hidden entries */
if (!IsDir(backup_instance_path, data_ent->d_name, FIO_BACKUP_HOST)
|| data_ent->d_name[0] == '.')
continue;
...
}
- 找到单次备份的目录,data_path: /home/pjr/backup/backups/db_backup/RD1WFX
/* open subdirectory of specific backup */
join_path_components(data_path, backup_instance_path, data_ent->d_name);
- 找到目录下的backup.control文件,backup_conf_path:/home/pjr/backup/backups/db_backup/RD1WFX/backup.control
nRet = snprintf_s(backup_conf_path, MAXPGPATH, MAXPGPATH - 1,"%s/%s", data_path, BACKUP_CONTROL_FILE);
- 将backup.control文件的信息写入结构体中
backup = readBackupControlFile(backup_conf_path);
static pgBackup *
readBackupControlFile(const char *path)
{
...
ConfigOption options[] =
{
{'s', 0, "backup-mode", &backup_mode, SOURCE_FILE_STRICT},
{'u', 0, "timelineid", &backup->tli, SOURCE_FILE_STRICT},
{'s', 0, "start-lsn", &start_lsn, SOURCE_FILE_STRICT},
{'s', 0, "stop-lsn", &stop_lsn, SOURCE_FILE_STRICT},
{'t', 0, "start-time", &backup->start_time, SOURCE_FILE_STRICT},
{'t', 0, "merge-time", &backup->merge_time, SOURCE_FILE_STRICT},
{'t', 0, "end-time", &backup->end_time, SOURCE_FILE_STRICT},
{'U', 0, "recovery-xid", &backup->recovery_xid, SOURCE_FILE_STRICT},
{'t', 0, "recovery-time", &backup->recovery_time, SOURCE_FILE_STRICT},
{'t', 0, "expire-time", &backup->expire_time, SOURCE_FILE_STRICT},
{'I', 0, "data-bytes", &backup->data_bytes, SOURCE_FILE_STRICT},
{'I', 0, "wal-bytes", &backup->wal_bytes, SOURCE_FILE_STRICT},
{'I', 0, "uncompressed-bytes", &backup->uncompressed_bytes, SOURCE_FILE_STRICT},
{'I', 0, "pgdata-bytes", &backup->pgdata_bytes, SOURCE_FILE_STRICT},
{'u', 0, "block-size", &backup->block_size, SOURCE_FILE_STRICT},
{'u', 0, "xlog-block-size", &backup->wal_block_size, SOURCE_FILE_STRICT},
{'u', 0, "checksum-version", &backup->checksum_version, SOURCE_FILE_STRICT},
{'s', 0, "program-version", &program_version, SOURCE_FILE_STRICT},
{'s', 0, "server-version", &server_version, SOURCE_FILE_STRICT},
{'b', 0, "stream", &backup->stream, SOURCE_FILE_STRICT},
{'s', 0, "status", &status, SOURCE_FILE_STRICT},
{'s', 0, "parent-backup-id", &parent_backup, SOURCE_FILE_STRICT},
{'s', 0, "merge-dest-id", &merge_dest_backup, SOURCE_FILE_STRICT},
{'s', 0, "compress-alg", &compress_alg, SOURCE_FILE_STRICT},
{'u', 0, "compress-level", &backup->compress_level, SOURCE_FILE_STRICT},
{'b', 0, "from-replica", &backup->from_replica, SOURCE_FILE_STRICT},
{'s', 0, "external-dirs", &backup->external_dir_str, SOURCE_FILE_STRICT},
{'s', 0, "note", &backup->note, SOURCE_FILE_STRICT},
{'s', 0, "recovery-name", &recovery_name, SOURCE_FILE_STRICT},
{'u', 0, "content-crc", &backup->content_crc, SOURCE_FILE_STRICT},
{0}
};
....
}
- 如果控制文件读出来的结构体为空,则自己构造一个backup结构,start_time就用目录解码出来的时间。找到的话,就再对名字和他自己start_time进行解码的时间进行一次校验。
if (!backup)
{
backup = pgut_new(pgBackup);
pgBackupInit(backup);
backup->start_time = base36dec(data_ent->d_name);
}
else if (strcmp(base36enc(backup->start_time), data_ent->d_name) != 0)
{
elog(WARNING, "backup ID in control file \"%s\" doesn't match name of the backup folder \"%s\"",
base36enc(backup->start_time), backup_conf_path);
}
- 继续构造backup结构体的属性
a. backup->root_dir: /home/pjr/backup/backups/db_backup/RD1WFX
b. backup->database_dir: /home/pjr/backup/backups/db_backup/RD1WFX/database
backup->root_dir = pgut_strdup(data_path);
backup->database_dir = (char *)pgut_malloc(MAXPGPATH);
join_path_components(backup->database_dir, backup->root_dir, DATABASE_DIR);
/* Initialize page header map */
init_header_map(backup);
/* TODO: save encoded backup id */
backup->backup_id = backup->start_time;
- 添加backup结构到列表
parray_append(backups, backup);
- 把列表按照start_time进行降序排序
parray_qsort(backups, pgBackupCompareIdDesc);
- 遍历所有backup结构,根据自己指向的用二分查找找到自己的祖先,也是这个列表中的一个backup结构,然后将该backup结构中的parent_backup_link指针指向自己父备份的backup结构。最后返回backups列表。
/* Link incremental backups with their ancestors.*/
for (i = 0; i < (int)parray_num(backups); i++)
{
pgBackup *curr = (pgBackup *)parray_get(backups, i);
pgBackup **ancestor;
pgBackup key;
if (curr->backup_mode == BACKUP_MODE_FULL)
continue;
key.start_time = curr->parent_backup;
ancestor = (pgBackup **) parray_bsearch(backups, &key,
pgBackupCompareIdDesc);
if (ancestor)
curr->parent_backup_link = *ancestor;
}
return backups;
在相同timeline里找到父备份
- 首先找父全量备份。遍历已经按start_time降序排序的backups列表。如果状态是OK或者DONE,backup_mode是全量备份,以及timeline相等,那么就算找到了。找不到全量备份就直接返回。
/* backup_list is sorted in order of descending ID */
for (i = 0; i < (int)parray_num(backup_list); i++)
{
pgBackup *backup = (pgBackup *) parray_get(backup_list, i);
if ((backup->backup_mode == BACKUP_MODE_FULL &&
(backup->status == BACKUP_STATUS_OK ||
backup->status == BACKUP_STATUS_DONE)) && backup->tli == tli)
{
full_backup = backup;
break;
}
}
/* Failed to find valid FULL backup to fulfill ancestor role */
if (!full_backup)
return NULL;
- 找到了全量备份,就找它的最新的一个孩子。同样遍历backups列表,取一个backup,判断继承链的状态。继承链有三种状态。
#define ChainIsBroken 0
#define ChainIsInvalid 1
#define ChainIsOk 2
- 因为backup结构中已经使用parent_backup_link指向了自己的父备份。所以可以用以下的循环,不断的用这个指针找到自己的父备份,直到找到一个全量备份,或者找不到。这两种情况的parent_backup_link指针都为NULL。
while (target_backup->parent_backup_link)
{
if (target_backup->status != BACKUP_STATUS_OK &&
target_backup->status != BACKUP_STATUS_DONE)
/* oldest invalid backup in parent chain */
invalid_backup = target_backup;
target_backup = target_backup->parent_backup_link;
}
- 如果最终找到的父备份不是全量备份,那么认为这个继承链断裂,返回ChainIsBroken。
如果是全量备份,但是状态不OK或者DONE那么,这个备份认为是无效的,返回ChainIsInvalid。
否则,继承链OK,返回ChainIsOk,以及找到这个全量父备份。
/* Previous loop will skip FULL backup because his parent_backup_link is NULL */
if (target_backup->backup_mode == BACKUP_MODE_FULL &&
(target_backup->status != BACKUP_STATUS_OK &&
target_backup->status != BACKUP_STATUS_DONE))
{
invalid_backup = target_backup;
}
/* found chain end and oldest backup is not FULL */
if (target_backup->backup_mode != BACKUP_MODE_FULL)
{
/* Set oldest child backup in chain */
*result_backup = target_backup;
return ChainIsBroken;
}
/* chain is ok, but some backups are invalid */
if (invalid_backup)
{
*result_backup = invalid_backup;
return ChainIsInvalid;
}
*result_backup = target_backup;
return ChainIsOk;
- 对无效和断裂的继承链,继续遍历backups列表中的下一个backup。对于继承链OK的backup,因为已经按照时间排过顺序了,那么这个继承就是某个全量备份的最新的孩子。这时候再最终确认一次,开始找到的全量备份和这个最新的备份之间是不是继承关系。确认方法还是用parent_backup_link指针不断的指向自己的父备份,找到最终全量备份,看这个全量备份跟我们之前找到的备份是不是同一个。确认无误后,返回全量备份的最新的孩子(也有可能是全量备份自己)。
- 这里我感觉有点多次一举,上一步已经找到了一个result_backup,直接比较这个和开始的那个全量备份start_time就行。
/* Yes, we could call is_parent() earlier - after choosing the ancestor,
* but this way we have an opportunity to detect and report all possible
* anomalies.
*/
if (is_parent(full_backup->start_time, backup, true))
return backup;
bool is_parent(time_t parent_backup_time, pgBackup *child_backup, bool inclusive)
{
if (!child_backup)
elog(ERROR, "Target backup cannot be NULL");
if (inclusive && child_backup->start_time == parent_backup_time)
return true;
while (child_backup->parent_backup_link &&
child_backup->parent_backup != parent_backup_time)
{
child_backup = child_backup->parent_backup_link;
}
if (child_backup->parent_backup == parent_backup_time)
return true;
return false;
}
在不同timeline里找父备份找到父备份(暂不支持)
网友评论