2021-12-11 09:49:09 +08:00
|
|
|
|
#include "udisksystemrestoreproxy.h"
|
|
|
|
|
#include <QDateTime>
|
|
|
|
|
#include <QDir>
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QTimer>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <sys/reboot.h>
|
|
|
|
|
#include "../common/utils.h"
|
|
|
|
|
#include "mymountproxy.h"
|
|
|
|
|
#include "myprocess/mksquashfsprocess.h"
|
|
|
|
|
|
|
|
|
|
IMPLEMENT_DYNCREATE(UDiskSystemRestoreProxy)
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 构造函数
|
|
|
|
|
*/
|
|
|
|
|
UDiskSystemRestoreProxy::UDiskSystemRestoreProxy()
|
|
|
|
|
{
|
|
|
|
|
m_isFinished = false;
|
|
|
|
|
m_p = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 析构函数
|
|
|
|
|
*/
|
|
|
|
|
UDiskSystemRestoreProxy::~UDiskSystemRestoreProxy()
|
|
|
|
|
{
|
|
|
|
|
delete m_p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 环境检测
|
|
|
|
|
* @return false,检测失败;true,检测成功
|
|
|
|
|
*/
|
|
|
|
|
bool UDiskSystemRestoreProxy::checkEnvEx()
|
|
|
|
|
{
|
|
|
|
|
qDebug() << "UDiskSystemRestoreProxy::checkEnvEx invoke begin";
|
|
|
|
|
|
|
|
|
|
// 1、检测.user.txt是否存在
|
|
|
|
|
m_userFile = m_backupWrapper.m_prefixDestPath + BACKUP_SNAPSHOTS_PATH + "/" + m_backupWrapper.m_uuid + "/" + PATHS_USER_FILE;
|
|
|
|
|
m_userFile.replace("//", "/");
|
|
|
|
|
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;
|
|
|
|
|
m_excludeUserFile.replace("//", "/");
|
|
|
|
|
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";
|
|
|
|
|
m_backupPath.replace("//", "/");
|
|
|
|
|
if (Utils::isDirEmpty(m_backupPath)) {
|
|
|
|
|
qCritical("还原点{uuid}/data目录不存在");
|
|
|
|
|
emit checkResult(int(BackupResult::INC_NOT_FOUND_DIR));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-11 15:10:58 +08:00
|
|
|
|
emit checkResult(int(BackupResult::CHECK_ENV_SUCCESS));
|
|
|
|
|
|
2021-12-11 09:49:09 +08:00
|
|
|
|
qDebug() << "UDiskSystemRestoreProxy::checkEnvEx invoke end";
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 执行还原逻辑
|
|
|
|
|
*/
|
|
|
|
|
void UDiskSystemRestoreProxy::doWorkEx()
|
|
|
|
|
{
|
|
|
|
|
qDebug() << "UDiskSystemRestoreProxy::doWorkEx invoke begin";
|
|
|
|
|
|
|
|
|
|
// 1、校验
|
|
|
|
|
if (!checkEnvEx())
|
|
|
|
|
return ;
|
|
|
|
|
|
|
|
|
|
// 2、还原efi(兼容旧版本的备份)
|
|
|
|
|
if (!restoreEfi()) {
|
|
|
|
|
qCritical("/boot/efi目录同步失败");
|
|
|
|
|
emit checkResult(int(BackupResult::EFI_RSYNC_FAIL));
|
|
|
|
|
return ;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 3、还原系统
|
|
|
|
|
if (doPrepare())
|
|
|
|
|
restoreSystem();
|
|
|
|
|
|
|
|
|
|
qDebug() << "UDiskSystemRestoreProxy::doWorkEx invoke end";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 还原efi(兼容旧版本的备份)
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
bool UDiskSystemRestoreProxy::restoreEfi()
|
|
|
|
|
{
|
|
|
|
|
qDebug() << "UDiskSystemRestoreProxy::restoreEfi invoke begin";
|
|
|
|
|
|
|
|
|
|
// 是否有/boot/efi目录
|
|
|
|
|
QString efiPath = Utils::getSysRootPath() + "/boot/efi";
|
|
|
|
|
efiPath.replace("//", "/");
|
|
|
|
|
if (!Utils::isDirEmpty(efiPath)) {
|
|
|
|
|
// 1、修复源数据
|
|
|
|
|
repairEfi();
|
|
|
|
|
|
|
|
|
|
// 2、重新rw读写挂载
|
|
|
|
|
remountEfi();
|
|
|
|
|
|
|
|
|
|
// 3、同步efi
|
|
|
|
|
return rsyncEfi();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
qDebug() << "UDiskSystemRestoreProxy::restoreEfi invoke end";
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 修复efi目录
|
|
|
|
|
*/
|
|
|
|
|
void UDiskSystemRestoreProxy::repairEfi()
|
|
|
|
|
{
|
|
|
|
|
QString qsEfiPath = m_backupPath + "/efi";
|
|
|
|
|
if (!Utils::isDirEmpty(qsEfiPath)) {
|
|
|
|
|
// 存在/efi说明是老备份数据,尽量修正老数据
|
|
|
|
|
QStringList args;
|
|
|
|
|
args << "-f";
|
|
|
|
|
args << qsEfiPath;
|
|
|
|
|
QString newPath = m_backupPath + "/boot";
|
|
|
|
|
args << newPath;
|
|
|
|
|
QProcess::execute("mv", args);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 重新rw读写挂载efi分区
|
|
|
|
|
*/
|
|
|
|
|
void UDiskSystemRestoreProxy::remountEfi()
|
|
|
|
|
{
|
|
|
|
|
QString mountPath = Utils::getSysRootPath() + "/boot/efi";
|
|
|
|
|
mountPath.replace("//", "/");
|
|
|
|
|
QStringList args;
|
|
|
|
|
args << "-o"
|
|
|
|
|
<< "rw,remount"
|
|
|
|
|
<< mountPath;
|
|
|
|
|
QProcess::execute("mount", args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 重新rw读写挂载boot分区
|
|
|
|
|
*/
|
|
|
|
|
void UDiskSystemRestoreProxy::remountBoot()
|
|
|
|
|
{
|
|
|
|
|
QString mountPath = Utils::getSysRootPath() + "/boot";
|
|
|
|
|
mountPath.replace("//", "/");
|
|
|
|
|
QStringList args;
|
|
|
|
|
args << "-o"
|
|
|
|
|
<< "rw,remount"
|
|
|
|
|
<< mountPath;
|
|
|
|
|
QProcess::execute("mount", args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 同步efi
|
|
|
|
|
*/
|
|
|
|
|
bool UDiskSystemRestoreProxy::rsyncEfi()
|
|
|
|
|
{
|
|
|
|
|
QString efiPath = m_backupPath + "/boot/efi/";
|
|
|
|
|
if (Utils::isDirEmpty(efiPath))
|
|
|
|
|
efiPath = efiPath = m_backupPath + "/efi/";
|
|
|
|
|
if (Utils::isDirEmpty(efiPath))
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
QStringList args = getRsyncArgs(SystemRestoreScene::EFI_RESTORE);
|
|
|
|
|
QString mountPath = Utils::getSysRootPath() + "/boot/efi/";
|
|
|
|
|
mountPath.replace("//", "/");
|
|
|
|
|
|
|
|
|
|
args << efiPath << mountPath;
|
|
|
|
|
|
|
|
|
|
m_p = new RsyncPathToDirProcess(this);
|
|
|
|
|
bool result = m_p->start(args);
|
|
|
|
|
delete m_p;
|
|
|
|
|
m_p = nullptr;
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 根据场景获取rsync命令参数
|
|
|
|
|
* @param scene,场景
|
|
|
|
|
* @return 组装好的rsync的参数信息
|
|
|
|
|
*/
|
|
|
|
|
QStringList UDiskSystemRestoreProxy::getRsyncArgs(SystemRestoreScene scene)
|
|
|
|
|
{
|
|
|
|
|
QStringList args;
|
|
|
|
|
args << "-avAHXr";
|
|
|
|
|
args << "--info=progress2";
|
|
|
|
|
args << "--no-inc-recursive";
|
|
|
|
|
args << "--ignore-missing-args";
|
|
|
|
|
args << "--delete";
|
|
|
|
|
|
|
|
|
|
QDir dataDir(m_srcPath + "/data");
|
|
|
|
|
QFile file(m_srcPath + "/etc/uid_list");
|
|
|
|
|
QDir efiDir(m_srcPath + "/boot/efi");
|
|
|
|
|
QStringList excludes;
|
|
|
|
|
|
|
|
|
|
switch (scene) {
|
|
|
|
|
case SystemRestoreScene::SYSTEM_RESTORE :
|
|
|
|
|
// 还原工具不还原自身
|
|
|
|
|
args << "--exclude=/usr/bin/backup-daemon";
|
|
|
|
|
args << "--exclude=/usr/bin/kybackup";
|
|
|
|
|
args << "--exclude=/usr/bin/mount_fstab_efi";
|
|
|
|
|
args << "--exclude=/usr/bin/backup-auto-efi";
|
|
|
|
|
args << "--exclude=/usr/bin/backup-auto";
|
|
|
|
|
args << "--exclude=/usr/bin/rsync";
|
|
|
|
|
args << "--exclude=/usr/share/rsync";
|
|
|
|
|
args << "--exclude=/usr/share/initramfs-tools/hooks/kybackup-hooks";
|
|
|
|
|
args << "--exclude=/usr/share/initramfs-tools/scripts/local-bottom/kybackup";
|
|
|
|
|
|
|
|
|
|
// 以前的出厂备份和grub备份没有备份/data,还原时需要判断/data是否存在,如不存在需要屏蔽掉,不然会将主机上的/data删除,造成问题
|
|
|
|
|
// 此为兼容以前备份的老数据而改,等以后老的备份估计不存在了可已去掉
|
|
|
|
|
if (!dataDir.exists()) {
|
|
|
|
|
args << "--exclude=/data";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!file.exists()) {
|
|
|
|
|
args << "--exclude=/etc/uid_list";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 为兼容以前的老备份数据,增加下面几行
|
|
|
|
|
if (efiDir.isEmpty()) {
|
|
|
|
|
args << QString("--exclude=/boot/efi");
|
|
|
|
|
}
|
|
|
|
|
// 系统安装后有的会将/data/home /data/root挂载到的/home /root上,实际文件是存放在/data/home /data/root下面
|
|
|
|
|
Utils::excludeFstabBindPath(excludes);
|
|
|
|
|
for (const QString& item : excludes) {
|
|
|
|
|
QDir itemDir(m_srcPath + item);
|
|
|
|
|
// 以后统一用/home /root这种路径, 兼容老备份数据(原来的U盘备份,在mksquashfs时排除bind挂载的任意一方时,都备份不上)
|
|
|
|
|
if (item == "/data/home") {
|
|
|
|
|
QDir homeDir(m_srcPath + "/home");
|
|
|
|
|
if (!homeDir.isEmpty()) {
|
|
|
|
|
args << QString("--exclude=/data/home");
|
|
|
|
|
} else if (!itemDir.isEmpty()) {
|
|
|
|
|
args << QString("--exclude=/home");
|
|
|
|
|
} else {
|
|
|
|
|
args << QString("--exclude=/data/home");
|
|
|
|
|
args << QString("--exclude=/home");
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
} else if (item == "/data/root") {
|
|
|
|
|
QDir homeDir(m_srcPath + "/root");
|
|
|
|
|
if (!homeDir.isEmpty()) {
|
|
|
|
|
args << QString("--exclude=/data/root");
|
|
|
|
|
} else if (!itemDir.isEmpty()) {
|
|
|
|
|
args << QString("--exclude=/root");
|
|
|
|
|
} else {
|
|
|
|
|
args << QString("--exclude=/data/root");
|
|
|
|
|
args << QString("--exclude=/root");
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
args << QString("--exclude=") + item;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
args << "--exclude-from" << m_excludeUserFile;
|
|
|
|
|
args << "--files-from" << m_userFile;
|
|
|
|
|
|
|
|
|
|
break ;
|
|
|
|
|
case SystemRestoreScene::EFI_RESTORE :
|
|
|
|
|
break ;
|
|
|
|
|
default:
|
|
|
|
|
return args;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return args;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 还原前准备
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
bool UDiskSystemRestoreProxy::doPrepare()
|
|
|
|
|
{
|
|
|
|
|
// 移动设备系统备份如果有img,则需要先将img挂载到/backup/imgbackup目录
|
|
|
|
|
QString imgPath = m_backupPath + UDISK_MKSQUASHFS_IMG_NAME;
|
|
|
|
|
if (Utils::filsExists(imgPath)) {
|
|
|
|
|
// 1、检测目录/backup/imgbackup是否存在,不存在则创建此目录
|
|
|
|
|
QString dstImgMountPath = Utils::getSysRootPath() + BACKUP_IMGBACKUP_PATH;
|
|
|
|
|
dstImgMountPath.replace("//", "/");
|
|
|
|
|
Utils::mkpath(dstImgMountPath);
|
|
|
|
|
|
|
|
|
|
// 2、先卸载/backup/imgbackup上的mount
|
|
|
|
|
MountBackupProcess::umount(dstImgMountPath);
|
|
|
|
|
|
|
|
|
|
// 3、将img文件挂载到/backup/imgbackup上
|
|
|
|
|
MountBackupProcess *processMount = new MountBackupProcess;
|
|
|
|
|
if (processMount->mount(imgPath, dstImgMountPath)) {
|
|
|
|
|
emit checkResult(int(BackupResult::RESTOREDIR_PREPARE_FAILED));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_srcPath = dstImgMountPath;
|
|
|
|
|
} else
|
|
|
|
|
m_srcPath = m_backupPath;
|
|
|
|
|
|
|
|
|
|
// 以读写方式重新挂载boot分区,因为有的机器默认以只读挂载
|
|
|
|
|
remountBoot();
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 系统还原
|
|
|
|
|
*/
|
|
|
|
|
void UDiskSystemRestoreProxy::restoreSystem()
|
|
|
|
|
{
|
|
|
|
|
QString destPath = Utils::getSysRootPath();
|
|
|
|
|
|
|
|
|
|
QStringList args = getRsyncArgs(SystemRestoreScene::SYSTEM_RESTORE);
|
|
|
|
|
|
|
|
|
|
args << m_srcPath + "/";
|
|
|
|
|
args << destPath + "/";
|
|
|
|
|
|
|
|
|
|
m_p = new RsyncPathToDirProcess(this);
|
|
|
|
|
connect(m_p, &RsyncPathToDirProcess::progress, this, &UDiskSystemRestoreProxy::progress);
|
|
|
|
|
connect(m_p, &RsyncPathToDirProcess::finished, this, [&](bool result) {
|
|
|
|
|
if (result) {
|
|
|
|
|
QString time = QDateTime::currentDateTime().toString("yy-MM-dd hh:mm:ss");
|
2021-12-11 15:10:58 +08:00
|
|
|
|
// Utils::writeBackupLog(time + "," + m_curUuid + "," + QString::number(m_backupWrapper.m_type) + ",,," + QString::number(m_backupWrapper.m_frontUid));
|
|
|
|
|
Utils::writeBackupLog(time + "," + m_curUuid + "," + QString::number(m_backupWrapper.m_type) + ",,,");
|
2021-12-11 09:49:09 +08:00
|
|
|
|
|
|
|
|
|
Utils::updateSyncFile();
|
|
|
|
|
QString fileIfSync = Utils::getSysRootPath() + FILE_IF_SYNC;
|
|
|
|
|
fileIfSync.replace("//", "/");
|
|
|
|
|
QFileInfo file(fileIfSync);
|
|
|
|
|
QDateTime beginTime = file.fileTime(QFileDevice::FileModificationTime);
|
|
|
|
|
Utils::wait(10);
|
|
|
|
|
Utils::updateSyncFile();
|
|
|
|
|
while (1) {
|
|
|
|
|
Utils::wait(2);
|
|
|
|
|
QFileInfo file1(fileIfSync);
|
|
|
|
|
QDateTime UpdateTime = file1.fileTime(QFileDevice::FileModificationTime);
|
|
|
|
|
if (UpdateTime > beginTime)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_backupWrapper.m_isOtherMachine) {
|
|
|
|
|
Utils::wait(20);
|
|
|
|
|
updateGrubUUid();
|
|
|
|
|
sync();
|
|
|
|
|
Utils::wait(5);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Utils::isDirEmpty(m_backupPath))
|
|
|
|
|
result = false;
|
|
|
|
|
emit this->workResult(result);
|
|
|
|
|
m_isFinished = true;
|
2021-12-11 15:10:58 +08:00
|
|
|
|
|
|
|
|
|
if (result) {
|
|
|
|
|
Utils::wait(2);
|
|
|
|
|
reboot(RB_AUTOBOOT);
|
|
|
|
|
}
|
2021-12-11 09:49:09 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
QTimer::singleShot(1*1000, this, &UDiskSystemRestoreProxy::checkUdiskExists);
|
|
|
|
|
m_isFinished = false;
|
|
|
|
|
m_p->start(args, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 异机还原时更新grub.cfg中的分区UUID
|
|
|
|
|
*/
|
|
|
|
|
void UDiskSystemRestoreProxy::updateGrubUUid()
|
|
|
|
|
{
|
|
|
|
|
QString srcFstab = Utils::getSysRootPath() + BACKUP_IMGBACKUP_PATH;
|
|
|
|
|
srcFstab += FSTAB_PATH;
|
|
|
|
|
srcFstab.replace("//", "/");
|
|
|
|
|
QHash<QString, QString> srcPartToUuid = Utils::getPartUuidMap(srcFstab);
|
|
|
|
|
QString destFstab = Utils::getSysRootPath() + FSTAB_PATH;
|
|
|
|
|
QHash<QString, QString> destPartToUuid = Utils::getPartUuidMap(destFstab);
|
|
|
|
|
|
|
|
|
|
QString findGrub = Utils::executeCmd("find /boot -name grub.cfg");
|
|
|
|
|
QStringList grubs = findGrub.split("\n");
|
|
|
|
|
for (const QString &grub : grubs) {
|
|
|
|
|
if (grub.isEmpty())
|
|
|
|
|
continue;
|
|
|
|
|
QString root = QString("sed -i 's/%1/%2/g' %3").arg(srcPartToUuid.value("/")).arg(destPartToUuid.value("/")).arg(grub);
|
|
|
|
|
qDebug() << Utils::executeCmd(root);
|
|
|
|
|
|
|
|
|
|
QString boot = QString("sed -i 's/%1/%2/g' %3").arg(srcPartToUuid.value("/boot")).arg(destPartToUuid.value("/boot")).arg(grub);
|
|
|
|
|
qDebug() << Utils::executeCmd(boot);
|
|
|
|
|
|
|
|
|
|
QString swap = QString("sed -i 's/%1/%2/g' %3").arg(srcPartToUuid.value("swap")).arg(destPartToUuid.value("swap")).arg(grub);
|
|
|
|
|
qDebug() << Utils::executeCmd(swap);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 监控移动设备是否还在
|
|
|
|
|
* @return true-在;false-不在
|
|
|
|
|
*/
|
|
|
|
|
bool UDiskSystemRestoreProxy::checkUdiskExists()
|
|
|
|
|
{
|
|
|
|
|
if (!m_isFinished) {
|
|
|
|
|
if (Utils::isDirEmpty(m_backupPath) && m_p != nullptr)
|
|
|
|
|
m_p->stop();
|
|
|
|
|
else
|
|
|
|
|
QTimer::singleShot(1*1000, this, &UDiskSystemRestoreProxy::checkUdiskExists);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|