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

522 lines
16 KiB
C++
Raw Normal View History

2021-08-06 10:20:03 +08:00
#include "systembackupproxy.h"
2021-08-17 10:07:35 +08:00
#include <QStorageInfo>
#include <QDateTime>
2021-08-06 10:20:03 +08:00
#include <QDebug>
#include "../common/utils.h"
2021-08-17 10:07:35 +08:00
#include "../common/mydusizetool.h"
#include "mymountproxy.h"
2021-08-19 19:24:49 +08:00
#include "myprocess/calcbackupsize.h"
2021-08-06 10:20:03 +08:00
IMPLEMENT_DYNCREATE(SystemBackupProxy)
2021-08-17 10:07:35 +08:00
SystemBackupProxy::SystemBackupProxy()
2021-08-24 18:08:18 +08:00
{
m_bSuccess = false;
2022-01-18 17:35:26 +08:00
m_isFinished = false;
2021-08-24 18:08:18 +08:00
m_p = nullptr;
m_size = 0;
2022-01-18 17:35:26 +08:00
m_isOnlyCheck = true;
m_calc = new CalcBackupSize(this);
connect(this, &SystemBackupProxy::cancel, this, &SystemBackupProxy::cancelEx);
2021-08-24 18:08:18 +08:00
}
2021-08-06 10:20:03 +08:00
SystemBackupProxy::~SystemBackupProxy()
2021-08-24 18:08:18 +08:00
{
delete m_p;
m_p = nullptr;
2022-01-18 17:35:26 +08:00
delete m_calc;
m_calc = nullptr;
2021-08-24 18:08:18 +08:00
}
2021-08-06 10:20:03 +08:00
2021-08-17 10:07:35 +08:00
/**
* @brief
2022-01-18 17:35:26 +08:00
* @return
2021-08-17 10:07:35 +08:00
*/
2021-08-24 18:08:18 +08:00
bool SystemBackupProxy::checkEnvEx()
2021-08-06 10:20:03 +08:00
{
qDebug() << "SystemBackupProxy::checkEnv invoke begin";
2021-08-17 10:07:35 +08:00
// 1、检查/backup分区是否挂载上(不管是本地磁盘还是u盘设备都得保证/backup挂载上); 若没挂载,挂载
MyMountProxy mountProxy;
2021-10-26 09:24:05 +08:00
if ( MountResult::MOUNTED != mountProxy.mountBackupPartition() ) {
2021-08-17 10:07:35 +08:00
emit checkResult(int(BackupResult::BACKUP_PARTITION_MOUNT_FAIL));
2021-08-24 18:08:18 +08:00
return false;
2021-08-17 10:07:35 +08:00
}
2021-08-24 18:08:18 +08:00
// 2、判断备份是否增量备份
isIncBackup();
2021-08-17 10:07:35 +08:00
// 3、检测空间是否满足备份
2022-01-18 17:35:26 +08:00
calcSizeForBackup();
2021-08-17 10:07:35 +08:00
2021-08-06 10:20:03 +08:00
qDebug() << "SystemBackupProxy::checkEnv invoke end";
2022-01-18 17:35:26 +08:00
return true;
2021-08-06 10:20:03 +08:00
}
2021-08-24 18:08:18 +08:00
/**
* @brief
*/
2021-08-06 10:20:03 +08:00
void SystemBackupProxy::doWorkEx()
2021-08-24 18:08:18 +08:00
{
2021-10-26 09:24:05 +08:00
qDebug() << "SystemBackupProxy::doWorkEx invoke begin";
2022-01-18 17:35:26 +08:00
2022-01-19 14:42:11 +08:00
m_isOnlyCheck = false;
2021-08-24 18:08:18 +08:00
// 环境检测
2022-01-18 17:35:26 +08:00
checkEnvEx();
2021-08-24 18:08:18 +08:00
2021-10-26 09:24:05 +08:00
qDebug() << "SystemBackupProxy::doWorkEx invoke end";
2021-08-24 18:08:18 +08:00
}
2021-08-06 10:20:03 +08:00
2022-01-18 17:35:26 +08:00
/**
* @brief
*/
2021-08-06 10:20:03 +08:00
void SystemBackupProxy::cancelEx()
2022-01-18 17:35:26 +08:00
{
m_bCancel = true;
if (!m_isFinished) {
emit this->checkResult(int(BackupResult::START_CANCEL));
if (m_calc)
m_calc->stop();
if (m_p)
m_p->stop();
QProcess::execute("sync");
Utils::wait(5);
deleteFailedData();
emit this->checkResult(int(BackupResult::CANCEL_SUCCESS));
}
}
/**
* @brief
*/
void SystemBackupProxy::deleteFailedData()
{
if (m_curUuid.isEmpty())
return;
2022-01-18 17:35:26 +08:00
// 1、删除备份目录
2022-01-21 09:51:10 +08:00
QString destPath = Utils::getSysRootPath() + BACKUP_SNAPSHOTS_PATH + "/" + m_curUuid;
2022-01-21 11:26:40 +08:00
destPath.replace("//", "/");
2022-01-18 17:35:26 +08:00
QStringList args;
args << "-rf";
2022-01-21 11:26:40 +08:00
args << destPath;
2022-01-18 17:35:26 +08:00
QProcess::execute("rm", args);
// 2、删除xml文件中的备份项
QString xmlPath = Utils::getSysRootPath() + BACKUP_XML_PATH;
xmlPath.replace("//", "/");
ParseBackupList parse(xmlPath);
parse.deleteItem(m_curUuid);
}
2021-08-06 10:20:03 +08:00
2021-08-17 10:07:35 +08:00
/**
2021-08-24 18:08:18 +08:00
* @brief
2021-08-17 10:07:35 +08:00
* @return true, false
*/
2021-08-24 18:08:18 +08:00
bool SystemBackupProxy::isIncBackup()
2021-08-17 10:07:35 +08:00
{
QString backupPath;
if (m_backupWrapper.m_uuid.isEmpty()) {
QString xmlPath(Utils::getSysRootPath() + BACKUP_XML_PATH);
xmlPath.replace("//", "/");
ParseBackupList parser(xmlPath);
ParseBackupList::BackupPoint point;
2021-08-19 19:24:49 +08:00
point = parser.getLastSysBackupPoint();
2021-08-17 10:07:35 +08:00
if (point.m_uuid.isEmpty())
return false;
backupPath = Utils::getSysRootPath() + BACKUP_SNAPSHOTS_PATH + "/" + point.m_uuid + "/data";
backupPath.replace("//", "/");
if (Utils::isDirExist(backupPath)) {
m_backupWrapper.m_baseUuid = point.m_uuid;
m_backupWrapper.m_bIncrement = true;
m_backupWrapper.m_type = BackupType::INC_BACKUP_SYSTEM;
return true;
}
2021-08-17 10:07:35 +08:00
} else {
2022-12-13 16:09:00 +08:00
QString xmlPath = Utils::getSysRootPath() + BACKUP_XML_PATH;
2022-05-27 17:20:27 +08:00
xmlPath.replace("//", "/");
ParseBackupList parse(xmlPath);
m_backupPoint = parse.findBackupPointByUuid(m_backupWrapper.m_uuid);
2021-08-17 10:07:35 +08:00
backupPath = Utils::getSysRootPath() + BACKUP_SNAPSHOTS_PATH + "/" + m_backupWrapper.m_uuid + "/data";
backupPath.replace("//", "/");
if (!m_backupPoint.m_backupName.isEmpty()) {
m_backupWrapper.m_bIncrement = true;
m_backupWrapper.m_type = BackupType::INC_BACKUP_SYSTEM;
return true;
}
2021-08-17 10:07:35 +08:00
}
2021-08-19 19:24:49 +08:00
return false;
2021-08-17 10:07:35 +08:00
}
/**
* @brief
*/
2022-01-18 17:35:26 +08:00
void SystemBackupProxy::checkFreeCapacity(qint64 itotalSize)
2021-08-17 10:07:35 +08:00
{
2021-08-24 18:08:18 +08:00
qDebug() << "SystemBackupProxy::checkFreeCapacity invoke begin";
2022-01-18 17:35:26 +08:00
// 如果是取消了操作,则不再发送其它信息
if (m_bCancel)
return ;
2021-08-17 10:07:35 +08:00
// 1、计算待备份数据的大小
m_size = itotalSize;
2021-08-19 19:24:49 +08:00
// 备份过程中会有一些临时文件产生会占用一部分空间故我们预留500M的空间
itotalSize += 500 * MB;
2021-08-17 10:07:35 +08:00
// 2、计算备份分区剩余空间大小
QString backupPath(Utils::getSysRootPath() + BACKUP_PATH);
backupPath.replace("//", "/");
QStorageInfo backupDisk(backupPath);
2022-01-13 14:06:40 +08:00
qint64 freeSize = backupDisk.bytesAvailable();
2022-03-10 15:59:33 +08:00
qDebug() << "需要空间" << itotalSize << ", 剩余空间" << freeSize;
2021-08-19 19:24:49 +08:00
// 3、校验空间是否足够
if (itotalSize > freeSize) {
emit checkResult(int(BackupResult::BACKUP_CAPACITY_IS_NOT_ENOUGH));
2022-07-18 15:24:19 +08:00
return ;
2021-08-24 18:08:18 +08:00
} else {
emit checkResult(int(BackupResult::CHECK_ENV_SUCCESS));
2021-08-19 19:24:49 +08:00
}
2021-08-24 18:08:18 +08:00
2022-01-18 17:35:26 +08:00
// 仅仅校验,不进行备份
if (m_isOnlyCheck)
return ;
// 4、开始备份
doBackup();
2021-08-24 18:08:18 +08:00
qDebug() << "SystemBackupProxy::checkFreeCapacity invoke end";
2021-08-19 19:24:49 +08:00
}
/**
* @brief
2022-01-18 17:35:26 +08:00
* @return
2021-08-19 19:24:49 +08:00
*/
2022-01-18 17:35:26 +08:00
void SystemBackupProxy::calcSizeForBackup()
2021-08-19 19:24:49 +08:00
{
2021-08-24 18:08:18 +08:00
QString destPath = Utils::getSysRootPath();
2021-08-19 19:24:49 +08:00
QStringList args;
if (m_backupWrapper.m_bIncrement) {
if (m_backupWrapper.m_uuid.isEmpty()) {
// 新增增量备份点场景
args = getRsyncArgs(SystemBackupScene::TRY_INC_SYSTEM_BACKUP);
2021-08-24 18:08:18 +08:00
destPath += CHECK_PATH;
2021-08-19 19:24:49 +08:00
} else {
// 在原来的备份点上增量备份场景
args = getRsyncArgs(SystemBackupScene::TRY_INC_SYSTEM_BACKUP_AT_BASE);
2021-08-24 18:08:18 +08:00
destPath += BACKUP_SNAPSHOTS_PATH;
destPath += "/";
destPath += m_backupWrapper.m_uuid;
destPath += "/data/";
2021-08-19 19:24:49 +08:00
}
} else {
// 全量备份场景
args = getRsyncArgs(SystemBackupScene::TRY_SYSTEM_BACKUP);
2021-08-24 18:08:18 +08:00
destPath += CHECK_PATH;
2021-08-19 19:24:49 +08:00
}
// 拼接备份源路径和目标路径
QString srcPath = Utils::getSysRootPath();
srcPath += "/";
srcPath.replace("//", "/");
args << srcPath;
destPath.replace("//", "/");
args << destPath;
Utils::mkpath(destPath);
2022-01-18 17:35:26 +08:00
connect(m_calc, &CalcBackupSize::finished, this, &SystemBackupProxy::checkFreeCapacity);
m_calc->start(args, false);
2021-08-17 10:07:35 +08:00
}
/**
* @brief rsync命令参数
* @param scene
* @return rsync的参数信息
*/
QStringList SystemBackupProxy::getRsyncArgs(SystemBackupScene scene)
{
QStringList args;
switch (scene) {
case SystemBackupScene::SYSTEM_BACKUP :
2021-11-17 11:47:15 +08:00
args << "-avAHXr";
args << "--info=progress2";
2021-09-16 16:05:46 +08:00
args << "--no-inc-recursive";
2021-08-19 19:24:49 +08:00
args << "--ignore-missing-args";
2021-12-26 18:15:21 +08:00
args << "--delete";
2021-08-17 10:07:35 +08:00
break ;
case SystemBackupScene::INC_SYSTEM_BACKUP :
args << "-avAXr";
2021-11-17 11:47:15 +08:00
args << "--info=progress2";
2021-09-16 16:05:46 +08:00
args << "--no-inc-recursive";
2021-08-19 19:24:49 +08:00
args << "--ignore-missing-args";
2021-08-24 18:08:18 +08:00
args << QString("--link-dest=../../%1/data").arg(m_backupWrapper.m_baseUuid);
2021-08-19 19:24:49 +08:00
break ;
case SystemBackupScene::INC_SYSTEM_BACKUP_AT_BASE :
2021-10-26 09:24:05 +08:00
args << "-avAHXr";
2021-11-17 11:47:15 +08:00
args << "--info=progress2";
2021-09-16 16:05:46 +08:00
args << "--no-inc-recursive";
2021-08-19 19:24:49 +08:00
args << "--ignore-missing-args";
args << "--delete";
2021-08-17 10:07:35 +08:00
break ;
case SystemBackupScene::TRY_SYSTEM_BACKUP :
2021-11-17 11:47:15 +08:00
args << "-aAHXrn";
2021-08-17 10:07:35 +08:00
args << "--stats";
2021-08-19 19:24:49 +08:00
args << "--ignore-missing-args";
2021-08-17 10:07:35 +08:00
break ;
case SystemBackupScene::TRY_INC_SYSTEM_BACKUP :
2021-08-19 19:24:49 +08:00
args << "-aAXrn";
args << "--stats";
args << "--ignore-missing-args";
2021-08-24 18:08:18 +08:00
args << QString("--link-dest=../../%1/data").arg(m_backupWrapper.m_baseUuid);
2021-08-19 19:24:49 +08:00
break ;
case SystemBackupScene::TRY_INC_SYSTEM_BACKUP_AT_BASE :
2021-10-26 09:24:05 +08:00
args << "-aAHXrn";
2021-08-17 10:07:35 +08:00
args << "--stats";
2021-08-19 19:24:49 +08:00
args << "--ignore-missing-args";
args << "--delete";
2021-08-17 10:07:35 +08:00
break ;
2021-08-24 18:08:18 +08:00
case SystemBackupScene::EFI_BACKUP :
2021-10-26 09:24:05 +08:00
args << "-avAHXr";
2021-11-17 11:47:15 +08:00
args << "--info=progress2";
2021-08-24 18:08:18 +08:00
args << "--ignore-missing-args";
break ;
2021-08-17 10:07:35 +08:00
default:
2021-08-19 19:24:49 +08:00
return args;
}
2021-08-24 18:08:18 +08:00
if (SystemBackupScene::EFI_BACKUP != scene) {
// --exclude=排除路径设置
for (const QString & item : m_backupWrapper.m_backupExcludePaths) {
args << QString("--exclude=%1").arg(item);
}
2021-08-17 10:07:35 +08:00
}
2021-10-26 09:24:05 +08:00
// 系统更新备份不再备份用户数据目录
if (AUTO_BACKUP_UUID == m_curUuid) {
if (Utils::isHuawei990()) {
args << "--exclude=/data";
2021-10-26 09:24:05 +08:00
} else {
args << "--exclude=/data/usershare";
2021-10-26 09:24:05 +08:00
}
args << "--exclude=/home";
args << "--exclude=/data/home";
args << "--exclude=/root";
args << "--exclude=/data/root";
2021-10-26 09:24:05 +08:00
}
2021-08-17 10:07:35 +08:00
return args;
}
2021-08-24 18:08:18 +08:00
/**
* @brief
*/
void SystemBackupProxy::doBackup()
{
qDebug() << "SystemBackupProxy::doBackup invoke begin";
// 准备
if (!doPrepare())
return ;
2021-10-26 09:24:05 +08:00
// 启动备份efi, 修改为和其它目录统一备份,不再单独进行备份
// if (!backupEfi()) {
// emit checkResult(int(BackupResult::EFI_RSYNC_FAIL));
// return ;
// }
2021-08-24 18:08:18 +08:00
// 启动系统备份
backupSystem();
2021-08-24 18:08:18 +08:00
qDebug() << "SystemBackupProxy::doBackup invoke end";
}
/**
* @brief
* @return true,false
*/
bool SystemBackupProxy::doPrepare()
{
qDebug() << "SystemBackupProxy::doPrepare invoke begin";
m_bSuccess = false;
2021-08-24 18:08:18 +08:00
// 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;
}
QString userFile = Utils::getSysRootPath() + BACKUP_SNAPSHOTS_PATH + "/" + m_curUuid + "/" + PATHS_USER_FILE;
userFile.replace("//", "/");
if (!Utils::writeFileByLines(userFile, m_backupWrapper.m_backupPaths)) {
2021-08-24 18:08:18 +08:00
emit checkResult(int(BackupResult::WRITE_BACKUP_PATHS_TO_USER_FAILED));
2022-01-18 17:35:26 +08:00
qCritical() << QString("create file %1 failed !").arg(userFile);
2021-08-24 18:08:18 +08:00
return false;
}
QString excludeUserFile = Utils::getSysRootPath() + BACKUP_SNAPSHOTS_PATH + "/" + m_curUuid + "/" + EXCLUDE_PATHS_USER_FILE;
excludeUserFile.replace("//", "/");
if (!Utils::writeFileByLines(excludeUserFile, m_backupWrapper.m_backupExcludePaths)) {
2021-11-09 15:44:26 +08:00
emit checkResult(int(BackupResult::WRITE_BACKUP_PATHS_TO_USER_FAILED));
2022-01-18 17:35:26 +08:00
qCritical() << QString("create file %1 failed !").arg(excludeUserFile);
2021-08-24 18:08:18 +08:00
return false;
}
// 3、记录/backup/snapshots/backuplist.xml文件
if (!recordBackupPoint()) {
qCritical() << "add or update item to backuplist.xml failed !";
return false;
}
2021-08-24 18:08:18 +08:00
qDebug() << "SystemBackupProxy::doPrepare invoke end";
return true;
}
/**
* @brief /backup/snapshots/backuplist.xml文件
* @return true-false-
*/
bool SystemBackupProxy::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_path = m_backupWrapper.m_prefixDestPath;
QString xmlPath = Utils::getSysRootPath() + BACKUP_XML_PATH;
xmlPath.replace("//", "/");
ParseBackupList parse(xmlPath);
if (m_backupWrapper.m_uuid.isEmpty() /*|| !m_backupWrapper.m_bIncrement*/) {
if (parse.addItem(m_backupPoint) != ParseBackupList::SUCCESS) {
emit checkResult(int(BackupResult::WRITE_STORAGEINFO_ADD_ITEM_FAIL));
return false;
}
} else {
if (parse.updateItem(m_backupPoint) != ParseBackupList::SUCCESS) {
emit checkResult(int(BackupResult::WRITE_STORAGEINFO_UPDATE_ITEM_FAIL));
return false;
}
}
return true;
}
2021-08-24 18:08:18 +08:00
/**
* @brief /boot/efi
* @return truefalse
*/
bool SystemBackupProxy::backupEfi()
{
qDebug() << "SystemBackupProxy::backupEfi invoke begin";
// /boot/efi分区是自动挂载的这里不用去挂载
QString efiPath = Utils::getSysRootPath() + "/boot/efi";
efiPath.replace("//", "/");
QStringList args;
if (Utils::isDirExist(efiPath)) {
args = getRsyncArgs(SystemBackupScene::EFI_BACKUP);
}
args << efiPath;
2021-10-26 09:24:05 +08:00
QString destPath = m_destPath + "/boot/efi";
2021-09-16 16:05:46 +08:00
destPath.replace("//", "/");
Utils::mkpath(destPath);
args << destPath;
2021-08-24 18:08:18 +08:00
m_p = new RsyncPathToDirProcess(this);
bool result = m_p->start(args);
delete m_p;
m_p = nullptr;
qDebug() << "SystemBackupProxy::backupEfi invoke end";
return result;
}
/**
* @brief
* @return truefalse
*/
bool SystemBackupProxy::backupSystem()
{
qDebug() << "SystemBackupProxy::backupSystem invoke begin";
QStringList args;
if (m_backupWrapper.m_bIncrement) {
if (m_backupWrapper.m_uuid.isEmpty()) {
// 新增增量备份点场景
args = getRsyncArgs(SystemBackupScene::INC_SYSTEM_BACKUP);
} else {
// 在原来的备份点上增量备份场景
args = getRsyncArgs(SystemBackupScene::INC_SYSTEM_BACKUP_AT_BASE);
}
} else {
// 全量备份场景
args = getRsyncArgs(SystemBackupScene::SYSTEM_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, &SystemBackupProxy::progress);
connect(m_p, &RsyncPathToDirProcess::finished, this, [&](bool result) {
2022-01-18 17:35:26 +08:00
// 如果是取消了操作,则不再发送其它信息
if (m_bCancel)
return ;
m_isFinished = true;
if (result) {
m_backupPoint.m_state = BACKUP_PARSE_STATE_SUCCESS_STRTING;
2022-01-18 17:35:26 +08:00
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);
2021-12-11 15:10:58 +08:00
// 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) + ","
2021-12-30 10:27:03 +08:00
+ m_backupWrapper.m_note + "," + m_backupPoint.m_size
+ ",," + m_backupWrapper.m_backupName);
2021-11-20 17:50:17 +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);
qDebug() << "SystemBackupProxy::backupSystem invoke end";
return true;
}