U盘备份数据还原

This commit is contained in:
zhaominyong 2021-12-29 10:20:15 +08:00
parent 31a86f8b19
commit 72b7e55603
4 changed files with 227 additions and 3 deletions

View File

@ -45,6 +45,7 @@ HEADERS += \
systembackupproxy.h \
systemrestoreproxy.h \
udiskdatabackupproxy.h \
udiskdatarestoreproxy.h \
udisksystembackupproxy.h \
udisksystemrestoreproxy.h \
workerfactory.h
@ -71,6 +72,7 @@ SOURCES += \
systembackupproxy.cpp \
systemrestoreproxy.cpp \
udiskdatabackupproxy.cpp \
udiskdatarestoreproxy.cpp \
udisksystembackupproxy.cpp \
udisksystemrestoreproxy.cpp \
workerfactory.cpp

View File

@ -115,11 +115,11 @@ QStringList DataRestoreProxy::getRsyncArgs(DataRestoreScene scene)
*/
void DataRestoreProxy::restoreData()
{
m_srcPath = m_backupPath;
QString destPath = Utils::getSysRootPath();
QStringList args = getRsyncArgs(DataRestoreScene::DATA_RESTORE);
m_srcPath = m_backupPath;
QString destPath = Utils::getSysRootPath();
destPath.replace("//", "/");
args << m_srcPath + "/";
args << destPath + "/";

View File

@ -0,0 +1,158 @@
#include "udiskdatarestoreproxy.h"
#include <QDateTime>
#include <QDir>
#include <QTimer>
#include <QDebug>
#include <unistd.h>
#include <sys/reboot.h>
#include "../common/utils.h"
#include "mymountproxy.h"
IMPLEMENT_DYNCREATE(UDiskDataRestoreProxy)
/**
* @brief
*/
UDiskDataRestoreProxy::UDiskDataRestoreProxy()
{
m_bSuccess = false;
m_isFinished = false;
m_p = nullptr;
}
/**
* @brief
*/
UDiskDataRestoreProxy::~UDiskDataRestoreProxy()
{
delete m_p;
}
/**
* @brief
* @return false,;true,
*/
bool UDiskDataRestoreProxy::checkEnvEx()
{
qDebug() << "UDiskDataRestoreProxy::checkEnvEx invoke begin";
// 1、检测.user.txt是否存在
m_userFile = m_backupWrapper.m_prefixDestPath + BACKUP_SNAPSHOTS_PATH + "/" + m_backupWrapper.m_uuid + "/" + PATHS_USER_FILE;
if (!Utils::filsExists(m_userFile)) {
qCritical(".user.txt文件不存在");
emit checkResult(int(BackupResult::WRITE_BACKUP_PATHS_TO_USER_FAILED));
return false;
}
// 2、检测.exclude.user.txt是否存在
m_excludeUserFile = m_backupWrapper.m_prefixDestPath + BACKUP_SNAPSHOTS_PATH + "/" + m_backupWrapper.m_uuid + "/" + EXCLUDE_PATHS_USER_FILE;
if (!Utils::filsExists(m_excludeUserFile)) {
qCritical(".exclude.user.txt文件不存在");
emit checkResult(int(BackupResult::WRITE_EXCLUDE_BACKUP_PATHS_TO_USER_FAILED));
return false;
}
// 3、检测还原点是否存在
m_backupPath = m_backupWrapper.m_prefixDestPath + BACKUP_SNAPSHOTS_PATH + "/" + m_backupWrapper.m_uuid + "/data";
if (Utils::isDirEmpty(m_backupPath)) {
qCritical("还原点{uuid}/data目录不存在");
emit checkResult(int(BackupResult::INC_NOT_FOUND_DIR));
return false;
}
emit checkResult(int(BackupResult::CHECK_ENV_SUCCESS));
qDebug() << "UDiskDataRestoreProxy::checkEnvEx invoke end";
return true;
}
/**
* @brief
*/
void UDiskDataRestoreProxy::doWorkEx()
{
qDebug() << "UDiskDataRestoreProxy::doWorkEx invoke begin";
// 1、校验
if (!checkEnvEx())
return ;
// 2、还原数据
restoreData();
qDebug() << "UDiskDataRestoreProxy::doWorkEx invoke end";
}
/**
* @brief rsync命令参数
* @param scene
* @return rsync的参数信息
*/
QStringList UDiskDataRestoreProxy::getRsyncArgs(DataRestoreScene scene)
{
QStringList args;
args << "-avAHXr";
args << "--info=progress2";
args << "--no-inc-recursive";
args << "--ignore-missing-args";
args << "--delete";
switch (scene) {
case DataRestoreScene::DATA_RESTORE :
args << "--files-from" << m_userFile;
break ;
default:
return args;
}
return args;
}
/**
* @brief
*/
void UDiskDataRestoreProxy::restoreData()
{
QStringList args = getRsyncArgs(DataRestoreScene::DATA_RESTORE);
m_srcPath = m_backupPath;
QString destPath = Utils::getSysRootPath();
destPath.replace("//", "/");
args << m_srcPath + "/";
args << destPath + "/";
m_p = new RsyncPathToDirProcess(this);
connect(m_p, &RsyncPathToDirProcess::progress, this, &UDiskDataRestoreProxy::progress);
connect(m_p, &RsyncPathToDirProcess::finished, this, [&](bool result) {
m_bSuccess = result;
m_isFinished = true;
if (result) {
QString time = QDateTime::currentDateTime().toString("yy-MM-dd hh:mm:ss");
Utils::writeBackupLog(time + "," + m_curUuid + "," + QString::number(m_backupWrapper.m_type) + ",,," + QString::number(m_backupWrapper.m_frontUid));
}
emit this->workResult(result);
});
QTimer::singleShot(1*1000, this, &UDiskDataRestoreProxy::checkUdiskExists);
m_isFinished = false;
m_bSuccess = false;
m_p->start(args, false);
}
/**
* @brief
* @return true-false-
*/
bool UDiskDataRestoreProxy::checkUdiskExists()
{
if (!m_isFinished) {
if (Utils::isDirEmpty(m_backupPath) && m_p != nullptr)
m_p->stop();
else
QTimer::singleShot(1*1000, this, &UDiskDataRestoreProxy::checkUdiskExists);
}
return true;
}

View File

@ -0,0 +1,64 @@
#ifndef UDISKDATARESTOREPROXY_H
#define UDISKDATARESTOREPROXY_H
#include "workerfactory.h"
#include "myprocess/rsyncpathtodirprocess.h"
#include "parsebackuplist.h"
class UDiskDataRestoreProxy : public Worker
{
Q_OBJECT
DECLARE_DYNCREATE(UDiskDataRestoreProxy)
public:
// 数据还原的几种场景
enum DataRestoreScene {
DATA_RESTORE, // 数据还原
};
explicit UDiskDataRestoreProxy();
virtual ~UDiskDataRestoreProxy();
public:
// 环境检测
virtual bool checkEnvEx();
// 任务处理
virtual void doWorkEx();
private slots:
bool checkUdiskExists();
private:
// 数据还原
void restoreData();
/**
* @brief rsync命令参数
* @param scene
* @return rsync的参数信息
*/
QStringList getRsyncArgs(DataRestoreScene scene);
// .user.txt文件路径
QString m_userFile;
// .exclude.user.txt文件路径
QString m_excludeUserFile;
// 备份数据所在的data目录
QString m_backupPath;
// 是否还原成功
bool m_bSuccess;
// 是否完成
bool m_isFinished;
// 当前备份uuid
QString m_curUuid;
// 当前还原源目录
QString m_srcPath;
// 还原进程
RsyncPathToDirProcess *m_p;
// 当前备份节点
ParseBackupList::BackupPoint m_backupPoint;
};
#endif // UDISKDATARESTOREPROXY_H