2021-12-26 18:15:21 +08:00
|
|
|
|
#include "databackupproxy.h"
|
|
|
|
|
#include <QStorageInfo>
|
|
|
|
|
#include <QDateTime>
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
#include <kysec/status.h>
|
|
|
|
|
#include "../common/utils.h"
|
|
|
|
|
#include "../common/mydusizetool.h"
|
|
|
|
|
#include "mymountproxy.h"
|
|
|
|
|
#include "myprocess/calcbackupsize.h"
|
|
|
|
|
|
|
|
|
|
IMPLEMENT_DYNCREATE(DataBackupProxy)
|
|
|
|
|
|
|
|
|
|
DataBackupProxy::DataBackupProxy()
|
|
|
|
|
{
|
|
|
|
|
m_bSuccess = false;
|
|
|
|
|
m_p = nullptr;
|
|
|
|
|
m_size = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DataBackupProxy::~DataBackupProxy()
|
|
|
|
|
{
|
|
|
|
|
delete m_p;
|
|
|
|
|
m_p = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 环境检测
|
|
|
|
|
* @return false,检测失败;true,检测成功
|
|
|
|
|
*/
|
|
|
|
|
bool DataBackupProxy::checkEnvEx()
|
|
|
|
|
{
|
|
|
|
|
qDebug() << "DataBackupProxy::checkEnv invoke begin";
|
|
|
|
|
|
|
|
|
|
// 1、检查/backup分区是否挂载上(不管是本地磁盘还是u盘设备,都得保证/backup挂载上); 若没挂载,挂载
|
|
|
|
|
MyMountProxy mountProxy;
|
|
|
|
|
if ( MountResult::MOUNTED != mountProxy.mountBackupPartition() ) {
|
|
|
|
|
emit checkResult(int(BackupResult::BACKUP_PARTITION_MOUNT_FAIL));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2、判断备份是否增量备份
|
|
|
|
|
isIncBackup();
|
|
|
|
|
|
|
|
|
|
// 3、检测空间是否满足备份
|
|
|
|
|
bool result = checkFreeCapacity();
|
|
|
|
|
|
|
|
|
|
qDebug() << "DataBackupProxy::checkEnv invoke end";
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 执行备份逻辑
|
|
|
|
|
*/
|
|
|
|
|
void DataBackupProxy::doWorkEx()
|
|
|
|
|
{
|
|
|
|
|
qDebug() << "DataBackupProxy::doWorkEx invoke begin";
|
|
|
|
|
// 环境检测
|
|
|
|
|
if (!checkEnvEx())
|
|
|
|
|
return ;
|
|
|
|
|
|
|
|
|
|
// 开始备份
|
|
|
|
|
doBackup();
|
|
|
|
|
qDebug() << "DataBackupProxy::doWorkEx invoke end";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DataBackupProxy::cancelEx()
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 判断是否增量备份
|
|
|
|
|
* @return true,增量备份; false,全量备份
|
|
|
|
|
*/
|
|
|
|
|
bool DataBackupProxy::isIncBackup()
|
|
|
|
|
{
|
|
|
|
|
QString backupPath;
|
|
|
|
|
ParseBackupList::BackupPoint point;
|
|
|
|
|
if (m_backupWrapper.m_uuid.isEmpty()) {
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
backupPath = Utils::getSysRootPath() + BACKUP_SNAPSHOTS_PATH + "/" + m_backupWrapper.m_uuid + "/data";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
backupPath.replace("//", "/");
|
|
|
|
|
if (Utils::isDirExist(backupPath)) {
|
|
|
|
|
m_backupWrapper.m_bIncrement = true;
|
|
|
|
|
m_backupWrapper.m_type = BackupType::INC_BACKUP_DATA;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 校验剩余空间是否满足备份
|
|
|
|
|
*/
|
|
|
|
|
bool DataBackupProxy::checkFreeCapacity()
|
|
|
|
|
{
|
|
|
|
|
qDebug() << "DataBackupProxy::checkFreeCapacity invoke begin";
|
|
|
|
|
|
|
|
|
|
// 1、计算待备份数据的大小
|
|
|
|
|
qint64 itotalSize = calcSizeForBackup();
|
|
|
|
|
m_size = itotalSize;
|
|
|
|
|
// 备份过程中会有一些临时文件产生,会占用一部分空间,故我们预留500M的空间
|
|
|
|
|
itotalSize += 5 * MB;
|
|
|
|
|
|
|
|
|
|
// 2、计算备份分区剩余空间大小
|
|
|
|
|
QString backupPath(Utils::getSysRootPath() + BACKUP_PATH);
|
|
|
|
|
backupPath.replace("//", "/");
|
|
|
|
|
QStorageInfo backupDisk(backupPath);
|
2022-01-13 14:06:40 +08:00
|
|
|
|
qint64 freeSize = backupDisk.bytesAvailable();
|
2021-12-26 18:15:21 +08:00
|
|
|
|
|
|
|
|
|
// 3、校验空间是否足够
|
|
|
|
|
bool result = true;
|
|
|
|
|
if (itotalSize > freeSize) {
|
|
|
|
|
emit checkResult(int(BackupResult::BACKUP_CAPACITY_IS_NOT_ENOUGH));
|
|
|
|
|
result = false;
|
|
|
|
|
} else {
|
|
|
|
|
emit checkResult(int(BackupResult::CHECK_ENV_SUCCESS));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
qDebug() << "DataBackupProxy::checkFreeCapacity invoke end";
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 计算备份所需空间大小
|
|
|
|
|
* @return 计算备份所需空间大小,单位字节
|
|
|
|
|
*/
|
|
|
|
|
qint64 DataBackupProxy::calcSizeForBackup()
|
|
|
|
|
{
|
|
|
|
|
QString destPath = Utils::getSysRootPath();
|
|
|
|
|
|
|
|
|
|
QStringList args;
|
|
|
|
|
if (m_backupWrapper.m_bIncrement) {
|
|
|
|
|
// 在原来的备份点上增量备份场景
|
|
|
|
|
args = getRsyncArgs(DataBackupScene::TRY_INC_DATA_BACKUP);
|
|
|
|
|
destPath += BACKUP_SNAPSHOTS_PATH;
|
|
|
|
|
destPath += "/";
|
|
|
|
|
destPath += m_backupWrapper.m_uuid;
|
|
|
|
|
destPath += "/data/";
|
|
|
|
|
} else {
|
|
|
|
|
// 全量备份场景
|
|
|
|
|
args = getRsyncArgs(DataBackupScene::TRY_DATA_BACKUP);
|
|
|
|
|
destPath += CHECK_PATH;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 拼接备份源路径和目标路径
|
|
|
|
|
QString srcPath = Utils::getSysRootPath();
|
|
|
|
|
srcPath += "/";
|
|
|
|
|
srcPath.replace("//", "/");
|
|
|
|
|
args << srcPath;
|
|
|
|
|
destPath.replace("//", "/");
|
|
|
|
|
args << destPath;
|
|
|
|
|
Utils::mkpath(destPath);
|
|
|
|
|
|
|
|
|
|
CalcBackupSize calcator;
|
|
|
|
|
return calcator.start(args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 根据场景获取rsync命令参数
|
|
|
|
|
* @param scene,场景
|
|
|
|
|
* @return rsync的参数信息
|
|
|
|
|
*/
|
|
|
|
|
QStringList DataBackupProxy::getRsyncArgs(DataBackupScene scene)
|
|
|
|
|
{
|
|
|
|
|
QStringList args;
|
|
|
|
|
QString backupFile = "/tmp/.backup.user";
|
|
|
|
|
Utils::writeFileByLines(backupFile, m_backupWrapper.m_backupPaths);
|
|
|
|
|
|
|
|
|
|
switch (scene) {
|
|
|
|
|
case DataBackupScene::DATA_BACKUP :
|
|
|
|
|
args << "-avAHXr";
|
|
|
|
|
args << "--info=progress2";
|
|
|
|
|
args << "--no-inc-recursive";
|
|
|
|
|
args << "--ignore-missing-args";
|
|
|
|
|
args << "--delete";
|
|
|
|
|
args << "--files-from" << backupFile;
|
|
|
|
|
break ;
|
|
|
|
|
case DataBackupScene::INC_DATA_BACKUP :
|
|
|
|
|
args << "-avAHXr";
|
|
|
|
|
args << "--info=progress2";
|
|
|
|
|
args << "--no-inc-recursive";
|
|
|
|
|
args << "--ignore-missing-args";
|
|
|
|
|
args << "--delete";
|
|
|
|
|
args << "--files-from" << backupFile;
|
|
|
|
|
break ;
|
|
|
|
|
case DataBackupScene::TRY_DATA_BACKUP :
|
|
|
|
|
args << "-aAHXrn";
|
|
|
|
|
args << "--stats";
|
|
|
|
|
args << "--ignore-missing-args";
|
|
|
|
|
args << "--files-from" << backupFile;
|
|
|
|
|
break ;
|
|
|
|
|
case DataBackupScene::TRY_INC_DATA_BACKUP :
|
|
|
|
|
args << "-aAHXrn";
|
|
|
|
|
args << "--stats";
|
|
|
|
|
args << "--ignore-missing-args";
|
|
|
|
|
args << "--delete";
|
|
|
|
|
args << "--files-from" << backupFile;
|
|
|
|
|
break ;
|
|
|
|
|
default:
|
|
|
|
|
return args;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return args;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 备份
|
|
|
|
|
*/
|
|
|
|
|
void DataBackupProxy::doBackup()
|
|
|
|
|
{
|
|
|
|
|
qDebug() << "DataBackupProxy::doBackup invoke begin";
|
|
|
|
|
|
|
|
|
|
// 准备
|
|
|
|
|
if (!doPrepare())
|
|
|
|
|
return ;
|
|
|
|
|
|
|
|
|
|
// 启动数据备份
|
|
|
|
|
backupData();
|
|
|
|
|
|
|
|
|
|
qDebug() << "DataBackupProxy::doBackup invoke end";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 备份准备
|
|
|
|
|
* @return true,准备成功;false,准备失败
|
|
|
|
|
*/
|
|
|
|
|
bool DataBackupProxy::doPrepare()
|
|
|
|
|
{
|
|
|
|
|
qDebug() << "DataBackupProxy::doPrepare invoke begin";
|
|
|
|
|
|
|
|
|
|
m_bSuccess = false;
|
|
|
|
|
|
|
|
|
|
// 1、设置当前备份的Uuid
|
|
|
|
|
if (m_backupWrapper.m_uuid.isEmpty()) {
|
|
|
|
|
// 新增备份点,不指定uuid的场景
|
|
|
|
|
m_curUuid += Utils::createUuid();
|
|
|
|
|
} else {
|
|
|
|
|
// 指定uuid备份的场景
|
|
|
|
|
m_curUuid = m_backupWrapper.m_uuid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2、准备备份目录及文件
|
|
|
|
|
m_destPath = Utils::getSysRootPath() + BACKUP_SNAPSHOTS_PATH + "/" + m_curUuid + "/data";
|
|
|
|
|
m_destPath.replace("//", "/");
|
|
|
|
|
if (!Utils::mkpath(m_destPath)) {
|
|
|
|
|
emit checkResult(int(BackupResult::WRITE_BACKUP_PATHS_TO_USER_FAILED));
|
|
|
|
|
qCritical() << QString("mkdir %1 failed !").arg(m_destPath) ;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_userFile = Utils::getSysRootPath() + BACKUP_SNAPSHOTS_PATH + "/" + m_curUuid + "/" + PATHS_USER_FILE;
|
|
|
|
|
m_userFile.replace("//", "/");
|
|
|
|
|
if (!Utils::writeFileByLines(m_userFile, m_backupWrapper.m_backupPaths)) {
|
|
|
|
|
emit checkResult(int(BackupResult::WRITE_BACKUP_PATHS_TO_USER_FAILED));
|
|
|
|
|
qCritical() << QString("create file %1 failed !").arg(m_userFile) ;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_excludeUserFile = Utils::getSysRootPath() + BACKUP_SNAPSHOTS_PATH + "/" + m_curUuid + "/" + EXCLUDE_PATHS_USER_FILE;
|
|
|
|
|
m_excludeUserFile.replace("//", "/");
|
|
|
|
|
if (!Utils::writeFileByLines(m_excludeUserFile, m_backupWrapper.m_backupExcludePaths)) {
|
|
|
|
|
emit checkResult(int(BackupResult::WRITE_BACKUP_PATHS_TO_USER_FAILED));
|
|
|
|
|
qCritical() << QString("create file %1 failed !").arg(m_excludeUserFile) ;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 3、记录/backup/snapshots/backuplist.xml文件
|
|
|
|
|
if (!recordBackupPoint()) {
|
|
|
|
|
qCritical() << "add or update item to backuplist.xml failed !";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
qDebug() << "DataBackupProxy::doPrepare invoke end";
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 记录/backup/snapshots/backuplist.xml文件
|
|
|
|
|
* @return true-成功;false-失败
|
|
|
|
|
*/
|
|
|
|
|
bool DataBackupProxy::recordBackupPoint()
|
|
|
|
|
{
|
|
|
|
|
m_backupPoint.m_backupName = m_backupWrapper.m_backupName;
|
|
|
|
|
m_backupPoint.m_uuid = m_curUuid;
|
|
|
|
|
m_backupPoint.m_iPosition = m_backupWrapper.m_iPosition;
|
|
|
|
|
m_backupPoint.m_type = m_backupWrapper.m_type;
|
|
|
|
|
m_backupPoint.m_size = Utils::StringBySize(m_size);
|
|
|
|
|
m_backupPoint.m_time = QDateTime::currentDateTime().toString("yy-MM-dd hh:mm:ss");
|
|
|
|
|
m_backupPoint.m_state = BACKUP_PARSE_STATE_FAIL_STRTING;
|
|
|
|
|
m_backupPoint.m_os = SystemInfo::m_os;
|
|
|
|
|
m_backupPoint.m_arch = SystemInfo::m_arch;
|
|
|
|
|
m_backupPoint.m_archdetect = SystemInfo::m_archDetect;
|
|
|
|
|
QString xmlPath = Utils::getSysRootPath() + BACKUP_XML_PATH;
|
|
|
|
|
xmlPath.replace("//", "/");
|
|
|
|
|
ParseBackupList parse(xmlPath);
|
|
|
|
|
if (m_backupWrapper.m_bIncrement) {
|
|
|
|
|
if (parse.updateItem(m_backupPoint) != ParseBackupList::SUCCESS) {
|
|
|
|
|
emit checkResult(int(BackupResult::WRITE_STORAGEINFO_UPDATE_ITEM_FAIL));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (parse.addItem(m_backupPoint) != ParseBackupList::SUCCESS) {
|
|
|
|
|
emit checkResult(int(BackupResult::WRITE_STORAGEINFO_ADD_ITEM_FAIL));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 备份系统
|
|
|
|
|
* @return true,备份成功;false,备份失败
|
|
|
|
|
*/
|
|
|
|
|
bool DataBackupProxy::backupData()
|
|
|
|
|
{
|
|
|
|
|
qDebug() << "DataBackupProxy::backupData invoke begin";
|
|
|
|
|
|
|
|
|
|
QStringList args;
|
|
|
|
|
if (m_backupWrapper.m_bIncrement) {
|
|
|
|
|
// 在原来的备份点上增量备份场景
|
|
|
|
|
args = getRsyncArgs(DataBackupScene::INC_DATA_BACKUP);
|
|
|
|
|
} else {
|
|
|
|
|
// 全量备份场景
|
|
|
|
|
args = getRsyncArgs(DataBackupScene::DATA_BACKUP);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 拼接备份源路径和目标路径
|
|
|
|
|
QString srcPath = Utils::getSysRootPath();
|
|
|
|
|
srcPath += "/";
|
|
|
|
|
srcPath.replace("//", "/");
|
|
|
|
|
args << srcPath;
|
|
|
|
|
QString destPath = m_destPath + "/";
|
|
|
|
|
destPath.replace("//", "/");
|
|
|
|
|
args << destPath;
|
|
|
|
|
|
|
|
|
|
m_p = new RsyncPathToDirProcess(this);
|
|
|
|
|
connect(m_p, &RsyncPathToDirProcess::progress, this, &DataBackupProxy::progress);
|
|
|
|
|
connect(m_p, &RsyncPathToDirProcess::finished, this, [&](bool result) {
|
|
|
|
|
if (result) {
|
|
|
|
|
m_backupPoint.m_state = BACKUP_PARSE_STATE_SUCCESS_STRTING;
|
|
|
|
|
m_backupPoint.m_size = Utils::StringBySize(Utils::getDirOrFileSize(m_destPath));
|
|
|
|
|
QString xmlPath = Utils::getSysRootPath() + BACKUP_XML_PATH;
|
|
|
|
|
xmlPath.replace("//", "/");
|
|
|
|
|
ParseBackupList parse(xmlPath);
|
|
|
|
|
parse.updateItem(m_backupPoint);
|
|
|
|
|
|
|
|
|
|
// Utils::writeBackupLog(time + "," + m_curUuid + "," + QString::number(m_backupWrapper.m_type) + ","+ m_backupWrapper.m_note + "," + m_backupPoint.m_size+ "," + QString::number(m_backupWrapper.m_frontUid));
|
|
|
|
|
Utils::writeBackupLog(m_backupPoint.m_time + ","
|
|
|
|
|
+ m_curUuid + "," + QString::number(m_backupWrapper.m_type) + ","
|
|
|
|
|
+ m_backupWrapper.m_note + "," + m_backupPoint.m_size
|
2021-12-30 10:27:03 +08:00
|
|
|
|
+ "," + QString::number(m_backupWrapper.m_frontUid)
|
|
|
|
|
+ "," + m_backupWrapper.m_backupName);
|
2021-12-26 18:15:21 +08:00
|
|
|
|
Utils::update_backup_unique_settings(m_curUuid, m_backupPoint.m_backupName);
|
|
|
|
|
m_bSuccess = true;
|
|
|
|
|
}
|
|
|
|
|
emit this->workResult(result);
|
|
|
|
|
});
|
|
|
|
|
m_p->start(args, false);
|
|
|
|
|
|
|
|
|
|
do_kylin_security(m_destPath);
|
|
|
|
|
|
|
|
|
|
qDebug() << "DataBackupProxy::backupData invoke end";
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DataBackupProxy::do_kylin_security(const QString& dstDir)
|
|
|
|
|
{
|
|
|
|
|
int ret = 0;
|
|
|
|
|
ret = kysec_getstatus();
|
|
|
|
|
if (ret > 0) {
|
|
|
|
|
QString seFilePath(dstDir + "/.exectl");
|
|
|
|
|
QFile file(seFilePath);
|
|
|
|
|
file.open(QIODevice::WriteOnly);
|
|
|
|
|
file.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|