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

604 lines
19 KiB
C++
Raw Normal View History

2021-11-11 16:03:58 +08:00
#include "udisksystembackupproxy.h"
#include <QStorageInfo>
#include <QDateTime>
#include <QDebug>
2021-11-15 13:36:00 +08:00
#include <QTimer>
2021-11-11 16:03:58 +08:00
#include <kysec/status.h>
#include "../common/utils.h"
#include "../common/mydusizetool.h"
#include "mymountproxy.h"
IMPLEMENT_DYNCREATE(UDiskSystemBackupProxy)
UDiskSystemBackupProxy::UDiskSystemBackupProxy()
{
m_bSuccess = false;
2021-12-26 18:15:21 +08:00
m_isFinished = false;
2021-11-11 16:03:58 +08:00
m_p = nullptr;
m_size = 0;
m_calc = new CalcBackupSize(this);
m_isOnlyCheck = true;
2021-11-20 12:09:26 +08:00
m_mksquashfs = nullptr;
m_isForce = false;
2022-01-18 17:35:26 +08:00
connect(this, &UDiskSystemBackupProxy::cancel, this, &UDiskSystemBackupProxy::cancelEx);
2021-11-11 16:03:58 +08:00
}
UDiskSystemBackupProxy::~UDiskSystemBackupProxy()
{
delete m_p;
m_p = nullptr;
delete m_calc;
m_calc = nullptr;
2021-11-20 12:09:26 +08:00
delete m_mksquashfs;
m_mksquashfs = nullptr;
QString rm("rm -rf ");
rm += m_imgPath;
QProcess::execute(rm);
2021-11-11 16:03:58 +08:00
}
/**
* @brief
2022-01-18 17:35:26 +08:00
* @return
2021-11-11 16:03:58 +08:00
*/
bool UDiskSystemBackupProxy::checkEnvEx()
{
qDebug() << "UDiskSystemBackupProxy::checkEnv invoke begin";
// 1、检查/backup分区是否挂载上(不管是本地磁盘还是u盘设备都得保证/backup挂载上); 若没挂载,挂载
MyMountProxy mountProxy;
if ( MountResult::MOUNTED != mountProxy.mountBackupPartition() ) {
emit checkResult(int(BackupResult::BACKUP_PARTITION_MOUNT_FAIL));
return false;
}
2021-12-01 15:12:38 +08:00
QString backupPath(m_backupWrapper.m_prefixDestPath);
backupPath.replace("//", "/");
QStorageInfo udisk(backupPath);
QString udisk_type = udisk.fileSystemType();
if (udisk_type == "vfat") {
qCritical() << m_backupWrapper.m_prefixDestPath + " udisk's filesystemtype is vfat";
emit checkResult(int(BackupResult::UDISK_FILESYSTEM_TYPE_IS_VFAT));
return false;
}
if (udisk.isReadOnly()) {
// 只读挂载的U盘
qCritical() << QString("udisk(%s) is readonly filesystem").arg(m_backupWrapper.m_prefixDestPath);
emit checkResult(int(BackupResult::UDISK_FILESYSTEM_IS_READONLY));
return false;
}
2021-11-15 13:36:00 +08:00
QTimer::singleShot(1*1000, this, &UDiskSystemBackupProxy::checkDestDirExists);
2021-11-11 16:03:58 +08:00
// 2、计算备份大小
calcSizeForBackup();
qDebug() << "UDiskSystemBackupProxy::checkEnv invoke end";
return true;
}
/**
* @brief
*/
void UDiskSystemBackupProxy::doWorkEx()
{
qDebug() << "UDiskSystemBackupProxy::doWorkEx invoke begin";
m_isOnlyCheck = false;
// 环境检测
checkEnvEx();
qDebug() << "UDiskSystemBackupProxy::doWorkEx invoke end";
}
void UDiskSystemBackupProxy::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_mksquashfs)
m_mksquashfs->stop();
if (m_p)
m_p->stop();
QProcess::execute("sync");
Utils::wait(5);
deleteFailedData();
emit this->checkResult(int(BackupResult::CANCEL_SUCCESS));
}
}
/**
* @brief
*/
void UDiskSystemBackupProxy::deleteFailedData()
{
if (m_curUuid.isEmpty())
return;
2022-01-18 17:35:26 +08:00
// 1、删除备份目录
2022-01-21 11:26:40 +08:00
QString destPath = m_backupWrapper.m_prefixDestPath + BACKUP_SNAPSHOTS_PATH + "/" + m_curUuid;
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文件中的备份项
2022-01-21 11:26:40 +08:00
QString xmlPath = m_backupWrapper.m_prefixDestPath + BACKUP_XML_PATH;
2022-01-18 17:35:26 +08:00
xmlPath.replace("//", "/");
ParseBackupList parse(xmlPath);
parse.deleteItem(m_curUuid);
}
2021-11-11 16:03:58 +08:00
/**
* @brief
* @return true, false
*/
bool UDiskSystemBackupProxy::isIncBackup()
{
QString backupPath;
ParseBackupList::BackupPoint point;
if (m_backupWrapper.m_uuid.isEmpty()) {
QString xmlPath(m_backupWrapper.m_prefixDestPath + BACKUP_XML_PATH);
xmlPath.replace("//", "/");
ParseBackupList parser(xmlPath);
point = parser.getLastSysBackupPoint();
if (point.m_uuid.isEmpty())
return false;
backupPath = m_backupWrapper.m_prefixDestPath + BACKUP_SNAPSHOTS_PATH + "/" + point.m_uuid + "/data";
} else {
backupPath = m_backupWrapper.m_prefixDestPath + BACKUP_SNAPSHOTS_PATH + "/" + m_backupWrapper.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;
}
return false;
}
/**
* @brief
*/
void UDiskSystemBackupProxy::checkFreeCapacity(qint64 itotalSize)
{
qDebug() << "UDiskSystemBackupProxy::checkFreeCapacity invoke begin";
2022-01-18 17:35:26 +08:00
// 如果是取消了操作,则不再发送其它信息
if (m_bCancel)
return ;
// 拔掉U盘的场景
if (m_isForce) {
emit this->checkResult(int(BackupResult::WRITE_BACKUP_PATHS_TO_USER_FAILED));
2022-04-02 14:42:07 +08:00
return ;
}
2021-11-11 16:03:58 +08:00
// 1、计算待备份数据的大小
m_size = itotalSize;
// 备份过程中会有一些临时文件产生会占用一部分空间故我们预留500M的空间
itotalSize += 500 * MB;
// 2、计算备份分区剩余空间大小
QString backupPath(m_backupWrapper.m_prefixDestPath);
backupPath.replace("//", "/");
QStorageInfo backupDisk(backupPath);
2022-01-13 14:06:40 +08:00
qint64 freeSize = backupDisk.bytesAvailable();
2021-11-11 16:03:58 +08:00
// 3、校验空间是否足够
if (itotalSize > freeSize) {
emit checkResult(int(BackupResult::BACKUP_CAPACITY_IS_NOT_ENOUGH));
} else {
emit checkResult(int(BackupResult::CHECK_ENV_SUCCESS));
}
if (m_isOnlyCheck)
return ;
2021-11-20 12:09:26 +08:00
// 4、判断是否需要先压缩成img文件压缩后一般小于原大小的70%
itotalSize = itotalSize * 7 / 10;
QHash<QString, QString> hash = Utils::getLeftSizeOfPartitions();
for (QHash<QString, QString>::const_iterator it = hash.constBegin(); it != hash.constEnd(); ++it) {
QString path = it.key();
QString size = it.value();
if (size.endsWith("G")) {
size.replace("G", "");
qint64 leftSize = size.toLongLong() * 1000 * 1000 * 1000;
if (itotalSize < leftSize) {
m_imgPath = path + IMGBACKUP_PATH;
m_imgPath.replace("//", "/");
break ;
}
}
}
// 5、开始制作img或开始备份
if (m_imgPath.isEmpty()) {
doBackup();
} else {
doMksqushfs();
}
2021-11-11 16:03:58 +08:00
qDebug() << "UDiskSystemBackupProxy::checkFreeCapacity invoke end";
}
/**
* @brief
* @return ,
*/
void UDiskSystemBackupProxy::calcSizeForBackup()
{
// 拼接备份源路径和目标路径,测试所需备份空间大小;目标路径为一虚拟路径
QString srcPath = Utils::getSysRootPath();
srcPath += "/";
srcPath.replace("//", "/");
QString destPath = Utils::getSysRootPath();
destPath += CHECK_PATH;
destPath.replace("//", "/");
Utils::mkpath(destPath);
QStringList args = getRsyncArgs(UDiskSystemBackupScene::TRY_SYSTEM_BACKUP);
args << srcPath;
args << destPath;
connect(m_calc, &CalcBackupSize::finished, this, &UDiskSystemBackupProxy::checkFreeCapacity);
m_calc->start(args, false);
}
/**
* @brief rsync命令参数
* @param scene
* @return rsync的参数信息
*/
QStringList UDiskSystemBackupProxy::getRsyncArgs(UDiskSystemBackupScene scene)
{
QStringList args;
2021-11-20 12:09:26 +08:00
QStringList excludes;
2021-11-11 16:03:58 +08:00
switch (scene) {
case UDiskSystemBackupScene::SYSTEM_BACKUP :
2021-11-17 11:47:15 +08:00
args << "-avAHXr";
args << "--info=progress2";
2021-11-11 16:03:58 +08:00
args << "--no-inc-recursive";
args << "--ignore-missing-args";
break ;
case UDiskSystemBackupScene::TRY_SYSTEM_BACKUP :
2021-11-17 11:47:15 +08:00
args << "-aAHXrn";
2021-11-11 16:03:58 +08:00
args << "--stats";
args << "--ignore-missing-args";
break ;
2021-11-20 12:09:26 +08:00
case UDiskSystemBackupScene::MKSQUASHFS :
Utils::excludeFstabBindPath(excludes);
// --exclude=排除路径设置
for (QString item : m_backupWrapper.m_backupExcludePaths) {
if (excludes.contains(item))
continue;
if (item.endsWith("/*")) {
item.replace("/*", "");
}
args << "-e" << item;
}
2021-11-20 17:48:23 +08:00
args << "-e" << m_imgPath;
2021-11-20 12:09:26 +08:00
return args;
case UDiskSystemBackupScene::IMG_BACKUP :
2021-11-20 17:48:23 +08:00
args << "-avAHXr";
args << "--info=progress2";
args << "--no-inc-recursive";
args << "--ignore-missing-args";
return args;
2021-11-11 16:03:58 +08:00
default:
return args;
}
2021-11-17 11:47:15 +08:00
// --exclude=排除路径设置
for (const QString & item : m_backupWrapper.m_backupExcludePaths) {
args << QString("--exclude=%1").arg(item);
}
2021-11-11 16:03:58 +08:00
return args;
}
2021-11-20 12:09:26 +08:00
/**
* @brief mksqushfs
*/
void UDiskSystemBackupProxy::doMksqushfs()
{
qDebug() << "UDiskSystemBackupProxy::doMksqushfs invoke begin";
m_mksquashfs = new MkSquashFSProcess(this);
2021-11-20 17:48:23 +08:00
connect(m_mksquashfs, &MkSquashFSProcess::progress, this, &UDiskSystemBackupProxy::progress);
2021-11-20 12:09:26 +08:00
connect(m_mksquashfs, &MkSquashFSProcess::finished, this, [=](bool result) {
2022-01-18 17:35:26 +08:00
// 如果是取消了操作,则不再发送其它信息
if (m_bCancel)
return ;
if (result && !m_isForce) {
2021-11-20 12:09:26 +08:00
// 开始备份
doBackup();
} else {
2022-01-25 16:23:42 +08:00
m_isFinished = true;
2021-11-20 12:09:26 +08:00
emit checkResult(int(BackupResult::MKSQUASHFS_DO_FAIL));
}
});
Utils::mkpath(m_imgPath);
QString srcPath = Utils::getSysRootPath();
srcPath += "/";
srcPath.replace("//", "/");
QString dstImg = m_imgPath + "/" + UDISK_MKSQUASHFS_IMG_NAME;
QStringList args;
args << srcPath << dstImg;
args.append(getRsyncArgs(UDiskSystemBackupScene::MKSQUASHFS));
2021-11-20 17:48:23 +08:00
if (m_mksquashfs->start(args)) {
emit checkResult(int(BackupResult::MKSQUASHFS_START_SUCCESS));
} else {
2021-11-20 12:09:26 +08:00
emit checkResult(int(BackupResult::MKSQUASHFS_DO_FAIL));
}
qDebug() << "UDiskSystemBackupProxy::doMksqushfs invoke end";
}
2021-11-11 16:03:58 +08:00
/**
* @brief
*/
void UDiskSystemBackupProxy::doBackup()
{
qDebug() << "UDiskSystemBackupProxy::doBackup invoke begin";
// 准备
if (!doPrepare())
return ;
// 启动备份efi, 修改为和其它目录统一备份,不再单独进行备份
// if (!backupEfi()) {
// emit checkResult(int(BackupResult::EFI_RSYNC_FAIL));
// return ;
// }
2021-11-20 12:09:26 +08:00
if (m_imgPath.isEmpty()) {
// 启动系统备份
backupSystem();
} else {
// 备份img文件
backupImg();
}
2021-11-11 16:03:58 +08:00
qDebug() << "UDiskSystemBackupProxy::doBackup invoke end";
}
/**
* @brief
* @return true,false
*/
bool UDiskSystemBackupProxy::doPrepare()
{
qDebug() << "UDiskSystemBackupProxy::doPrepare invoke begin";
m_bSuccess = false;
// 1、设置当前备份的Uuid
m_curUuid += Utils::createUuid();
// 2、准备备份目录及文件
m_destPath = m_backupWrapper.m_prefixDestPath + BACKUP_SNAPSHOTS_PATH + "/" + m_curUuid + "/data";
m_destPath.replace("//", "/");
if (!Utils::mkpath(m_destPath)) {
qCritical() << QString("mkdir %1 failed !").arg(m_destPath) ;
2021-11-20 12:09:26 +08:00
emit checkResult(int(BackupResult::WRITE_BACKUP_PATHS_TO_USER_FAILED));
2021-11-11 16:03:58 +08:00
return false;
}
QString userFile = m_backupWrapper.m_prefixDestPath + BACKUP_SNAPSHOTS_PATH + "/" + m_curUuid + "/" + PATHS_USER_FILE;
userFile.replace("//", "/");
if (!Utils::writeFileByLines(userFile, m_backupWrapper.m_backupPaths)) {
2021-11-20 12:09:26 +08:00
qCritical() << QString("create file %1 failed !").arg(userFile);
2021-11-11 16:03:58 +08:00
emit checkResult(int(BackupResult::WRITE_BACKUP_PATHS_TO_USER_FAILED));
return false;
}
QString excludeUserFile = m_backupWrapper.m_prefixDestPath + BACKUP_SNAPSHOTS_PATH + "/" + m_curUuid + "/" + EXCLUDE_PATHS_USER_FILE;
excludeUserFile.replace("//", "/");
if (!Utils::writeFileByLines(excludeUserFile, m_backupWrapper.m_backupExcludePaths)) {
2021-11-20 12:09:26 +08:00
qCritical() << QString("create file %1 failed !").arg(excludeUserFile);
2021-11-11 16:03:58 +08:00
emit checkResult(int(BackupResult::WRITE_BACKUP_PATHS_TO_USER_FAILED));
return false;
}
// 3、记录/backup/snapshots/backuplist.xml文件
if (!recordBackupPoint()) {
qCritical() << "add or update item to backuplist.xml failed !";
return false;
}
qDebug() << "UDiskSystemBackupProxy::doPrepare invoke end";
return true;
}
/**
* @brief /backup/snapshots/backuplist.xml文件
* @return true-false-
*/
bool UDiskSystemBackupProxy::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 = m_backupWrapper.m_prefixDestPath + 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;
}
/**
* @brief
2021-11-20 12:09:26 +08:00
* @return truefalse
2021-11-11 16:03:58 +08:00
*/
bool UDiskSystemBackupProxy::backupSystem()
{
qDebug() << "UDiskSystemBackupProxy::backupSystem invoke begin";
// 全量备份场景
QStringList args = getRsyncArgs(UDiskSystemBackupScene::SYSTEM_BACKUP);
// 拼接备份源路径和目标路径
QString srcPath = Utils::getSysRootPath();
srcPath += "/";
srcPath.replace("//", "/");
args << srcPath;
QString destPath = m_destPath + "/";
destPath.replace("//", "/");
args << destPath;
2021-11-20 12:09:26 +08:00
return backup(args);
}
/**
* @brief img文件
* @return truefalse
*/
bool UDiskSystemBackupProxy::backupImg()
{
qDebug() << "UDiskSystemBackupProxy::backupImg invoke";
QStringList args;
QString srcPath = m_imgPath + "/" + UDISK_MKSQUASHFS_IMG_NAME;
QString destPath = m_destPath + "/";
destPath.replace("//", "/");
2021-11-20 17:48:23 +08:00
args << srcPath << destPath;
2021-11-20 12:09:26 +08:00
return backup(args);
}
/**
* @brief
* @param args
* @return truefalse
*/
bool UDiskSystemBackupProxy::backup(const QStringList &args)
{
qDebug() << "UDiskSystemBackupProxy::backup invoke begin";
2021-11-11 16:03:58 +08:00
m_p = new RsyncPathToDirProcess(this);
connect(m_p, &RsyncPathToDirProcess::progress, this, &UDiskSystemBackupProxy::progress);
connect(m_p, &RsyncPathToDirProcess::finished, this, [&](bool result) {
2022-01-18 17:35:26 +08:00
// 如果是取消了操作,则不再发送其它信息
if (m_bCancel)
return ;
m_isForce = false;
2021-12-26 18:15:21 +08:00
m_isFinished = true;
2021-11-11 16:03:58 +08:00
if (result) {
m_backupPoint.m_state = BACKUP_PARSE_STATE_SUCCESS_STRTING;
2021-11-20 17:50:17 +08:00
m_backupPoint.m_size = Utils::StringBySize(Utils::getDirOrFileSize(m_destPath));
2021-11-11 16:03:58 +08:00
QString xmlPath = m_backupWrapper.m_prefixDestPath + BACKUP_XML_PATH;
xmlPath.replace("//", "/");
ParseBackupList parse(xmlPath);
2021-12-26 18:15:21 +08:00
if (ParseBackupList::ParseResult::SUCCESS != parse.updateItem(m_backupPoint)) {
qCritical() << "update backuplist.xml error in sendBackupResult";
result = false;
} else {
// 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-12-26 18:15:21 +08:00
Utils::update_backup_unique_settings(m_curUuid, m_backupPoint.m_backupName);
m_bSuccess = true;
}
2021-11-11 16:03:58 +08:00
}
emit this->workResult(result);
});
m_p->start(args, false);
2021-11-20 12:09:26 +08:00
emit checkResult(int(BackupResult::BACKUP_START_SUCCESS));
2021-11-11 16:03:58 +08:00
do_kylin_security(m_destPath);
2021-11-20 12:09:26 +08:00
qDebug() << "UDiskSystemBackupProxy::backup invoke end";
2021-11-11 16:03:58 +08:00
return true;
}
void UDiskSystemBackupProxy::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();
}
}
2021-11-15 13:36:00 +08:00
/**
* @brief
* @return: bool,truefalse
* @author: zhaominyong
* @since: 2021/05/24
* @note:
* add by zhaominyong at 2021/05/24 for bug:54377 U盘的过程中拔出U盘
*/
bool UDiskSystemBackupProxy::checkDestDirExists()
{
2021-12-26 18:15:21 +08:00
if (!m_isFinished)
2021-11-15 13:36:00 +08:00
{
// 拔掉U盘后没有响应的场景怀疑可能是某应用程序关闭引起希望不是dbus服务关掉了
if (m_isForce) {
emit this->workResult(false);
return false;
}
if (Utils::isDirEmpty(m_backupWrapper.m_prefixDestPath)) {
qCritical() << QString("dstDir %s is not exist!").arg(m_backupWrapper.m_prefixDestPath);
m_isForce = true;
if (m_calc != nullptr)
m_calc->stop();
if (m_mksquashfs != nullptr)
m_mksquashfs->stop();
if (m_p != nullptr)
m_p->stop();
// 10s钟后如果还没有退出则强制退出
QTimer::singleShot(10*1000, this, &UDiskSystemBackupProxy::checkDestDirExists);
} else {
QTimer::singleShot(1*1000, this, &UDiskSystemBackupProxy::checkDestDirExists);
}
2021-11-15 13:36:00 +08:00
}
return true;
}
2021-11-11 16:03:58 +08:00