yhkylin-backup-tools/backup-daemon/systembackupproxy.cpp

127 lines
3.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "systembackupproxy.h"
#include <QStorageInfo>
#include <QDebug>
#include "../common/utils.h"
#include "../common/mydusizetool.h"
#include "mymountproxy.h"
#include "parsebackuplist.h"
IMPLEMENT_DYNCREATE(SystemBackupProxy)
SystemBackupProxy::SystemBackupProxy()
{}
SystemBackupProxy::~SystemBackupProxy()
{}
/**
* @brief 环境检测
*/
void SystemBackupProxy::checkEnvEx()
{
qDebug() << "SystemBackupProxy::checkEnv invoke begin";
// 1、检查/backup分区是否挂载上(不管是本地磁盘还是u盘设备都得保证/backup挂载上); 若没挂载,挂载
MyMountProxy mountProxy;
if ( MyMountProxy::MountResult::MOUNTED != mountProxy.mountBackupPartition() ) {
emit checkResult(int(BackupResult::BACKUP_PARTITION_MOUNT_FAIL));
return ;
}
// 2、检测备份是否增量备份
bool bInc = checkIsIncBackup();
m_backupWrapper.m_bIncrement = bInc;
// 3、检测空间是否满足备份
checkFreeCapacity(bInc);
qDebug() << "SystemBackupProxy::checkEnv invoke end";
}
void SystemBackupProxy::doWorkEx()
{}
void SystemBackupProxy::cancelEx()
{}
/**
* @brief 校验是否增量备份
* @return true,增量备份; false全量备份
*/
bool SystemBackupProxy::checkIsIncBackup()
{
QString backupPath;
if (m_backupWrapper.m_uuid.isEmpty()) {
QString xmlPath(Utils::getSysRootPath() + BACKUP_XML_PATH);
xmlPath.replace("//", "/");
ParseBackupList parser(xmlPath);
ParseBackupList::BackupPoint point = parser.getLastSysBackupPoint();
if (point.m_uuid.isEmpty())
return false;
backupPath = Utils::getSysRootPath() + BACKUP_SNAPSHOTS_PATH + "/" + point.m_uuid + "/data";
} else {
backupPath = Utils::getSysRootPath() + BACKUP_SNAPSHOTS_PATH + "/" + m_backupWrapper.m_uuid + "/data";
}
backupPath.replace("//", "/");
return Utils::isDirExist(backupPath);
}
/**
* @brief 校验剩余空间是否满足备份
* @param bInc是否增量备份
* @note 增量备份和全量备份计算空间方法不同
*/
void SystemBackupProxy::checkFreeCapacity(bool bInc)
{
// 1、计算待备份数据的大小
MyDuSizeTool du;
qint64 itotalSize = du.Do(m_backupWrapper.m_backupPaths, m_backupWrapper.m_backupExcludePaths, true);
if (-1 == itotalSize) {
qCritical() << "du system backup failed";
emit checkResult(int(BackupResult::DU_ERR));
return ;
}
// 2、计算备份分区剩余空间大小
QString backupPath(Utils::getSysRootPath() + BACKUP_PATH);
backupPath.replace("//", "/");
QStorageInfo backupDisk(backupPath);
qint64 freeSize = backupDisk.bytesFree();
}
/**
* @brief 根据场景获取rsync命令参数
* @param scene场景
* @return rsync的参数信息
*/
QStringList SystemBackupProxy::getRsyncArgs(SystemBackupScene scene)
{
QStringList args;
switch (scene) {
case SystemBackupScene::SYSTEM_BACKUP :
args << "-avAXW";
args << "--progress";
break ;
case SystemBackupScene::INC_SYSTEM_BACKUP :
args << "-avAXr";
args << "--progress";
args << "--link-dest";
break ;
case SystemBackupScene::TRY_SYSTEM_BACKUP :
args << "-avAXWn";
args << "--stats";
break ;
case SystemBackupScene::TRY_INC_SYSTEM_BACKUP :
args << "-avAXrn";
args << "--stats";
args << "--link-dest";
break ;
default:
}
return args;
}