2021-12-09 10:00:07 +08:00
|
|
|
|
#include "systemrestoreproxy.h"
|
|
|
|
|
#include <QDateTime>
|
|
|
|
|
#include <QDir>
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <sys/reboot.h>
|
|
|
|
|
#include "../common/utils.h"
|
|
|
|
|
#include "mymountproxy.h"
|
|
|
|
|
|
|
|
|
|
IMPLEMENT_DYNCREATE(SystemRestoreProxy)
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 构造函数
|
|
|
|
|
*/
|
|
|
|
|
SystemRestoreProxy::SystemRestoreProxy()
|
|
|
|
|
{
|
|
|
|
|
m_bSuccess = false;
|
|
|
|
|
m_p = nullptr;
|
2022-09-13 13:56:16 +08:00
|
|
|
|
m_bRetainUserData = false;
|
2021-12-09 10:00:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 析构函数
|
|
|
|
|
*/
|
|
|
|
|
SystemRestoreProxy::~SystemRestoreProxy()
|
|
|
|
|
{
|
|
|
|
|
delete m_p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 环境检测
|
|
|
|
|
* @return false,检测失败;true,检测成功
|
|
|
|
|
*/
|
|
|
|
|
bool SystemRestoreProxy::checkEnvEx()
|
|
|
|
|
{
|
|
|
|
|
qDebug() << "SystemRestoreProxy::checkEnvEx invoke begin";
|
|
|
|
|
|
|
|
|
|
// 1、检测.user.txt是否存在
|
|
|
|
|
m_userFile = Utils::getSysRootPath() + BACKUP_SNAPSHOTS_PATH + "/" + m_backupWrapper.m_uuid + "/" + PATHS_USER_FILE;
|
|
|
|
|
m_userFile.replace("//", "/");
|
2023-03-08 11:28:29 +08:00
|
|
|
|
if (!Utils::fileExists(m_userFile)) {
|
2021-12-09 10:00:07 +08:00
|
|
|
|
qCritical(".user.txt文件不存在");
|
|
|
|
|
emit checkResult(int(BackupResult::WRITE_BACKUP_PATHS_TO_USER_FAILED));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2、检测.exclude.user.txt是否存在
|
|
|
|
|
m_excludeUserFile = Utils::getSysRootPath() + BACKUP_SNAPSHOTS_PATH + "/" + m_backupWrapper.m_uuid + "/" + EXCLUDE_PATHS_USER_FILE;
|
|
|
|
|
m_excludeUserFile.replace("//", "/");
|
2023-03-08 11:28:29 +08:00
|
|
|
|
if (!Utils::fileExists(m_excludeUserFile)) {
|
2021-12-09 10:00:07 +08:00
|
|
|
|
qCritical(".exclude.user.txt文件不存在");
|
|
|
|
|
emit checkResult(int(BackupResult::WRITE_EXCLUDE_BACKUP_PATHS_TO_USER_FAILED));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 3、检测还原点是否存在
|
|
|
|
|
m_backupPath = Utils::getSysRootPath() + 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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-09 10:27:29 +08:00
|
|
|
|
// 4、检测xml中的还原点是否还存在
|
|
|
|
|
QString xmlPath = Utils::getSysRootPath() + BACKUP_XML_PATH;
|
|
|
|
|
xmlPath.replace("//", "/");
|
|
|
|
|
ParseBackupList parse(xmlPath);
|
|
|
|
|
m_backupPoint = parse.findBackupPointByUuid(m_backupWrapper.m_uuid);
|
|
|
|
|
if (m_backupPoint.m_uuid.isEmpty()) {
|
|
|
|
|
qCritical("xml中还原点不存在");
|
|
|
|
|
emit checkResult(int(BackupResult::INC_NOT_FOUND_DIR));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-24 15:45:07 +08:00
|
|
|
|
m_curUuid = m_backupWrapper.m_uuid;
|
|
|
|
|
|
2021-12-11 15:10:58 +08:00
|
|
|
|
emit checkResult(int(BackupResult::CHECK_ENV_SUCCESS));
|
|
|
|
|
|
2021-12-09 10:00:07 +08:00
|
|
|
|
qDebug() << "SystemRestoreProxy::checkEnvEx invoke end";
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 执行还原逻辑
|
|
|
|
|
*/
|
|
|
|
|
void SystemRestoreProxy::doWorkEx()
|
|
|
|
|
{
|
|
|
|
|
qDebug() << "SystemRestoreProxy::doWorkEx invoke begin";
|
|
|
|
|
|
|
|
|
|
// 1、校验
|
|
|
|
|
if (!checkEnvEx())
|
|
|
|
|
return ;
|
|
|
|
|
|
|
|
|
|
// 2、还原efi(兼容旧版本的备份)
|
|
|
|
|
if (!restoreEfi()) {
|
|
|
|
|
qCritical("/boot/efi目录同步失败");
|
|
|
|
|
emit checkResult(int(BackupResult::EFI_RSYNC_FAIL));
|
|
|
|
|
return ;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 3、还原系统
|
|
|
|
|
restoreSystem();
|
|
|
|
|
|
|
|
|
|
qDebug() << "SystemRestoreProxy::doWorkEx invoke end";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 还原efi(兼容旧版本的备份)
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
bool SystemRestoreProxy::restoreEfi()
|
|
|
|
|
{
|
|
|
|
|
qDebug() << "SystemRestoreProxy::restoreEfi invoke begin";
|
|
|
|
|
|
2023-09-05 09:07:25 +08:00
|
|
|
|
// 以读写方式重新挂载boot分区,因为有的机器默认以只读挂载
|
|
|
|
|
Utils::remountBoot();
|
|
|
|
|
|
2021-12-09 10:00:07 +08:00
|
|
|
|
// 是否有/boot/efi目录
|
|
|
|
|
QString efiPath = Utils::getSysRootPath() + "/boot/efi";
|
|
|
|
|
efiPath.replace("//", "/");
|
|
|
|
|
if (!Utils::isDirEmpty(efiPath)) {
|
|
|
|
|
// 1、修复源数据
|
|
|
|
|
repairEfi();
|
2022-09-17 09:57:21 +08:00
|
|
|
|
sync();
|
2021-12-09 10:00:07 +08:00
|
|
|
|
|
|
|
|
|
// 2、重新rw读写挂载
|
2023-09-05 09:07:25 +08:00
|
|
|
|
Utils::remountEfi();
|
2021-12-09 10:00:07 +08:00
|
|
|
|
|
|
|
|
|
// 3、同步efi
|
|
|
|
|
return rsyncEfi();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
qDebug() << "SystemRestoreProxy::restoreEfi invoke end";
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 修复efi目录
|
|
|
|
|
*/
|
|
|
|
|
void SystemRestoreProxy::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 同步efi
|
|
|
|
|
*/
|
|
|
|
|
bool SystemRestoreProxy::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 SystemRestoreProxy::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::RESTORE_SYSTEM_WITH_DATA :
|
|
|
|
|
args << "--exclude=/home";
|
|
|
|
|
args << "--exclude=/root";
|
|
|
|
|
if (Utils::isHuawei990()) {
|
|
|
|
|
args << "--exclude=/data";
|
|
|
|
|
} else {
|
|
|
|
|
args << "--exclude=/data/usershare";
|
|
|
|
|
}
|
|
|
|
|
// 保留指纹数据,用户密码、角色、权限、生物识别等信息不需要改变
|
|
|
|
|
args << "--exclude=/var/lib/biometric-auth";
|
|
|
|
|
args << "--exclude=/data/sec_storage_data";
|
|
|
|
|
args << "--exclude=/etc/passwd";
|
|
|
|
|
args << "--exclude=/etc/shadow";
|
|
|
|
|
args << "--exclude=/etc/group";
|
|
|
|
|
args << "--exclude=/etc/gshadow";
|
|
|
|
|
args << "--exclude=/etc/sudoers";
|
|
|
|
|
args << "--exclude=/data/home";
|
|
|
|
|
args << "--exclude=/data/root";
|
|
|
|
|
|
2022-07-14 09:34:07 +08:00
|
|
|
|
// 云图片作为桌面背景的路径属于用户数据
|
2022-01-25 16:15:41 +08:00
|
|
|
|
args << "--exclude=/var/lib/AccountsService";
|
2022-10-09 11:21:56 +08:00
|
|
|
|
|
|
|
|
|
// 域用户相关信息,还原后保持不退域
|
|
|
|
|
args << "--exclude=/etc/sssd";
|
|
|
|
|
args << "--exclude=/var/lib/sss";
|
|
|
|
|
args << "--exclude=/usr/share/sssd";
|
|
|
|
|
args << "--exclude=/etc/ipa";
|
|
|
|
|
args << "--exclude=/etc/krb5.keytab";
|
|
|
|
|
args << "--exclude=/etc/krb5.conf";
|
|
|
|
|
args << "--exclude=/var/lib/ipa-client";
|
|
|
|
|
args << "--exclude=/etc/nsswitch.conf";
|
|
|
|
|
args << "--exclude=/etc/pam.d";
|
|
|
|
|
args << "--exclude=/etc/hosts";
|
|
|
|
|
args << "--exclude=/etc/hostname";
|
|
|
|
|
args << "--exclude=/etc/hedron";
|
|
|
|
|
args << "--exclude=/etc/kcm";
|
|
|
|
|
args << "--exclude=/usr/hedron/hedronagent";
|
2022-10-09 16:50:33 +08:00
|
|
|
|
args << "--exclude=/etc/.kyinfo";
|
|
|
|
|
args << "--exclude=/etc/LICENSE";
|
|
|
|
|
args << "--exclude=/etc/ssl/certs";
|
|
|
|
|
args << "--exclude=/usr/share/ca-certificates";
|
2022-10-09 18:45:26 +08:00
|
|
|
|
args << "--exclude=/etc/NetworkManager";
|
2023-01-04 17:38:12 +08:00
|
|
|
|
args << "--exclude=/var/lib/pam";
|
2022-10-09 11:21:56 +08:00
|
|
|
|
|
2023-01-10 17:51:15 +08:00
|
|
|
|
// 安装kylin
|
|
|
|
|
args << "--exclude=/usr/share/applications/kylin-os-installer.desktop";
|
|
|
|
|
args << "--exclude=*/.local/share/applications/kylin-os-installer.desktop";
|
|
|
|
|
args << "--exclude=/etc/xdg/autostart/kylin-os-installer.desktop";
|
|
|
|
|
|
2021-12-09 10:00:07 +08:00
|
|
|
|
// 此处不要break,因为还需要排除SYSTEM_RESTORE中的项
|
|
|
|
|
|
|
|
|
|
case SystemRestoreScene::SYSTEM_RESTORE :
|
|
|
|
|
// 还原工具不还原自身
|
2023-03-26 14:24:58 +08:00
|
|
|
|
if (Utils::getSysRootPath() != CASPER_ROOT_PATH) {
|
|
|
|
|
// 试用模式时可以还原/target下面的备份还原工具
|
|
|
|
|
args << "--exclude=/usr/bin/backup-daemon";
|
|
|
|
|
args << "--exclude=/usr/bin/kybackup";
|
|
|
|
|
}
|
2021-12-09 10:00:07 +08:00
|
|
|
|
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";
|
2023-09-05 09:07:25 +08:00
|
|
|
|
args << "--exclude=/usr/share/initramfs-tools/scripts/*/kybackup";
|
2022-11-24 17:01:55 +08:00
|
|
|
|
|
2023-03-01 15:55:34 +08:00
|
|
|
|
// 还原后仍然保持激活状态
|
|
|
|
|
args << "--exclude=/etc/LICENSE";
|
|
|
|
|
args << "--exclude=/etc/.kyinfo";
|
|
|
|
|
args << "--exclude=/etc/.kyactivation";
|
|
|
|
|
args << "--exclude=/etc/.kyhwid";
|
|
|
|
|
|
2022-11-24 17:01:55 +08:00
|
|
|
|
// 文件安全箱
|
2022-11-16 14:00:35 +08:00
|
|
|
|
args << "--exclude=/data/security-dir";
|
2021-12-09 10:00:07 +08:00
|
|
|
|
|
|
|
|
|
// 以前的出厂备份和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);
|
2022-04-26 16:06:54 +08:00
|
|
|
|
// 自定义备份的路径也需要跳过,不进行还原
|
|
|
|
|
Utils::excludeCustomizePath(excludes);
|
2021-12-09 10:00:07 +08:00
|
|
|
|
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 系统还原
|
|
|
|
|
*/
|
|
|
|
|
void SystemRestoreProxy::restoreSystem()
|
|
|
|
|
{
|
2023-09-05 09:07:25 +08:00
|
|
|
|
// 停止安全初始化服务,以防过高的CPU占有率,因为还原时安全初始化服务会逐个文件打标记,造成cpu占有率超高系统卡顿
|
|
|
|
|
Utils::stopKysecInit();
|
|
|
|
|
// 停止网络服务,以防网络更新
|
|
|
|
|
Utils::stopNetwork();
|
2022-02-16 14:52:11 +08:00
|
|
|
|
|
2021-12-09 10:00:07 +08:00
|
|
|
|
// 本地系统备份没有img挂载,故下面两个路径相等
|
|
|
|
|
m_srcPath = m_backupPath;
|
|
|
|
|
QString destPath = Utils::getSysRootPath();
|
|
|
|
|
|
|
|
|
|
QStringList args;
|
|
|
|
|
// 自动更新的备份还原时保留用户数据
|
|
|
|
|
if (m_curUuid == AUTO_BACKUP_UUID || m_backupWrapper.m_type == BackupType::RESTORE_SYSTEM_WITH_DATA) {
|
|
|
|
|
args = getRsyncArgs(SystemRestoreScene::RESTORE_SYSTEM_WITH_DATA);
|
2022-09-13 13:56:16 +08:00
|
|
|
|
m_bRetainUserData = true;
|
2021-12-09 10:00:07 +08:00
|
|
|
|
} else {
|
|
|
|
|
args = getRsyncArgs(SystemRestoreScene::SYSTEM_RESTORE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
args << m_srcPath + "/";
|
2021-12-30 10:27:03 +08:00
|
|
|
|
destPath += "/";
|
|
|
|
|
destPath.replace("//", "/");
|
|
|
|
|
args << destPath;
|
2021-12-09 10:00:07 +08:00
|
|
|
|
|
|
|
|
|
m_p = new RsyncPathToDirProcess(this);
|
|
|
|
|
connect(m_p, &RsyncPathToDirProcess::progress, this, &SystemRestoreProxy::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));
|
2022-03-09 10:27:29 +08:00
|
|
|
|
Utils::writeBackupLog(time + "," + m_curUuid + "," + QString::number(m_backupWrapper.m_type) + ",,,," + m_backupPoint.m_backupName);
|
2021-12-09 10:00:07 +08:00
|
|
|
|
|
2023-09-05 09:07:25 +08:00
|
|
|
|
// 华为9a0等机型设定1分钟后关机重启,原因:运行时还原备份还原工具本身可能会造成运行中的备份还原崩溃,从而无法执行到后面的自动重启代码,故在此预设。
|
|
|
|
|
// 不再只华为机型使用此方法,将全部机型都改为此方法,缩短操作链
|
|
|
|
|
// if (Utils::isHuawei990())
|
|
|
|
|
{
|
|
|
|
|
QString msg;
|
|
|
|
|
Utils::executeCMD("shutdown -r +1", msg);
|
|
|
|
|
qDebug() << msg;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-09 10:00:07 +08:00
|
|
|
|
Utils::updateSyncFile();
|
2022-07-13 10:53:36 +08:00
|
|
|
|
Utils::wait(2);
|
2021-12-09 10:00:07 +08:00
|
|
|
|
QString fileIfSync = Utils::getSysRootPath() + FILE_IF_SYNC;
|
|
|
|
|
fileIfSync.replace("//", "/");
|
|
|
|
|
QFileInfo file(fileIfSync);
|
|
|
|
|
QDateTime beginTime = file.fileTime(QFileDevice::FileModificationTime);
|
2022-08-17 10:50:15 +08:00
|
|
|
|
QProcess::execute("sync");
|
|
|
|
|
Utils::wait(20);
|
2021-12-09 10:00:07 +08:00
|
|
|
|
Utils::updateSyncFile();
|
|
|
|
|
while (1) {
|
|
|
|
|
Utils::wait(2);
|
|
|
|
|
QFileInfo file1(fileIfSync);
|
|
|
|
|
QDateTime UpdateTime = file1.fileTime(QFileDevice::FileModificationTime);
|
|
|
|
|
if (UpdateTime > beginTime)
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-09-15 15:11:01 +08:00
|
|
|
|
if (FACTORY_BACKUP_UUID == m_curUuid && m_backupWrapper.m_type == BackupType::RESTORE_SYSTEM) {
|
|
|
|
|
// 出厂还原有的机器上删除/home/xxx有残留,故在此再强制删除一下,sudo rm -rf命令一遍还删除不了(报错:无法删除/home/xx/.config:目录非空,应该是删除后又自动生成了),多删除几次还不是非常干净,^_^
|
|
|
|
|
removeHome(QString(Utils::getSysRootPath() + "/home").replace("//", "/"));
|
|
|
|
|
removeHome(QString(Utils::getSysRootPath() + "/data/home").replace("//", "/"));
|
|
|
|
|
}
|
2021-12-09 10:00:07 +08:00
|
|
|
|
|
2023-09-05 09:07:25 +08:00
|
|
|
|
// 最后,再一次还原home目录以查缺补漏,因为系统还原过程中~/.config等目录中的部分文件可能会更新,会引起部分bug(如任务栏固定图标还原不完整等)
|
|
|
|
|
this->rsyncAgain();
|
|
|
|
|
::sync();
|
2023-02-03 13:56:58 +08:00
|
|
|
|
// 2209后新增了一些依赖,还原到以前的4.0.13版后缺少依赖无法运行,故此最后还得还原backup-daemon和kybackup本身
|
2023-03-08 11:28:29 +08:00
|
|
|
|
// 兼容Qt库从5.12.8升级到5.15,系统还原的最后也将备份还原工具本身还原
|
|
|
|
|
// QString version = Utils::getBackupVersion();
|
|
|
|
|
// if (version.contains("4.0.13"))
|
2023-03-26 14:24:58 +08:00
|
|
|
|
if (Utils::getSysRootPath() != CASPER_ROOT_PATH)
|
2023-03-08 11:28:29 +08:00
|
|
|
|
{
|
|
|
|
|
// initrd.img已经还原为旧状态,需要将新版本的backup-auto-efi等脚本文件更新到initrd.img中,这样我们修改后的新逻辑才能生效
|
2023-09-05 09:07:25 +08:00
|
|
|
|
// QString msg;
|
|
|
|
|
// Utils::executeCMD("update-initramfs -u", msg);
|
|
|
|
|
// qDebug() << msg;
|
|
|
|
|
|
|
|
|
|
// 解决bug:182576 【2203自适应升级2309】【2303自适应升级2309】升级成功还原系统失败,应该是有更新包修改了UEFI固件查找引导器文件的引导地址
|
|
|
|
|
// 故系统还原完成后,重新执行下grub-install命令
|
2023-03-08 11:28:29 +08:00
|
|
|
|
QString msg;
|
2023-09-05 09:07:25 +08:00
|
|
|
|
Utils::executeCMD("grub-install", msg);
|
2023-03-08 11:28:29 +08:00
|
|
|
|
qDebug() << msg;
|
|
|
|
|
|
|
|
|
|
// 写入标记:rsync_backup_self:${UUID}到文件/etc/file_if_sync中,表示需要还原backup-daemon和kybackup本身
|
2023-09-05 09:07:25 +08:00
|
|
|
|
// QString line("rsync_backup_self:");
|
|
|
|
|
// line += m_curUuid;
|
|
|
|
|
// Utils::syncWriteFile(fileIfSync, line);
|
|
|
|
|
// this->undoRestoreData();
|
|
|
|
|
|
|
|
|
|
// 加一层保险(因为有些机型上使用"update-initramfs -u"命令会失败,如:华为9006c的2203版等)
|
|
|
|
|
// QString version = Utils::getBackupVersion();
|
|
|
|
|
// if (Utils::isHuawei990() && version.contains("4.0.13"))
|
|
|
|
|
this->rsyncBackupSelf();
|
|
|
|
|
|
2023-03-08 11:28:29 +08:00
|
|
|
|
::sync();
|
|
|
|
|
}
|
2023-02-03 13:56:58 +08:00
|
|
|
|
|
2022-09-17 11:19:56 +08:00
|
|
|
|
emit this->workResult(result);
|
2023-09-05 09:07:25 +08:00
|
|
|
|
Utils::wait(3);
|
2021-12-09 10:00:07 +08:00
|
|
|
|
reboot(RB_AUTOBOOT);
|
2022-09-17 09:57:21 +08:00
|
|
|
|
} else {
|
|
|
|
|
emit this->workResult(result);
|
2021-12-09 10:00:07 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
m_p->start(args, false);
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-13 13:56:16 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief 获取备份点里存在家目录的用户列表
|
|
|
|
|
* @param void
|
|
|
|
|
* @return 获取备份点里存在家目录的用户列表
|
|
|
|
|
*/
|
|
|
|
|
QStringList SystemRestoreProxy::getBackupPointUsers()
|
|
|
|
|
{
|
|
|
|
|
// 根据备份点里的/home或/data/home的子目录进行统计
|
2022-09-13 14:56:06 +08:00
|
|
|
|
QString homePath = m_backupPath + "/home";
|
|
|
|
|
homePath.replace("//","/");
|
|
|
|
|
QStringList users = getBackupPointUsers(homePath);
|
|
|
|
|
QString dataHomePath = m_backupPath + "/data/home";
|
|
|
|
|
dataHomePath.replace("//","/");
|
|
|
|
|
users.append(getBackupPointUsers(dataHomePath));
|
2022-09-13 13:56:16 +08:00
|
|
|
|
return users;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QStringList SystemRestoreProxy::getBackupPointUsers(const QString& path)
|
|
|
|
|
{
|
|
|
|
|
QDir dir(path);
|
|
|
|
|
if (dir.exists()) {
|
|
|
|
|
return dir.entryList(QDir::NoDotAndDotDot | QDir::Dirs);
|
|
|
|
|
} else {
|
|
|
|
|
return QStringList();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-09 10:00:07 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief 删除home子目录,此处主要用于删除用户残留信息
|
|
|
|
|
* @param path
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
void SystemRestoreProxy::removeHome(const QString& path)
|
|
|
|
|
{
|
2022-09-13 14:56:06 +08:00
|
|
|
|
qDebug() << "SystemRestoreProxy::removeHome invoke begin";
|
|
|
|
|
|
2021-12-09 10:00:07 +08:00
|
|
|
|
QDir dir(path);
|
|
|
|
|
if (dir.exists()) {
|
2022-09-13 14:56:06 +08:00
|
|
|
|
QStringList retainUsers = getBackupPointUsers();
|
|
|
|
|
qDebug() << retainUsers;
|
2022-09-13 13:56:16 +08:00
|
|
|
|
|
2021-12-09 10:00:07 +08:00
|
|
|
|
QString subcmd("rm -rf ");
|
|
|
|
|
QStringList list = dir.entryList(QDir::NoDotAndDotDot | QDir::Dirs);
|
|
|
|
|
for (const QString& item : list) {
|
2022-09-13 13:56:16 +08:00
|
|
|
|
// 出厂备份里面的/home/oem等保留
|
2022-09-13 14:56:06 +08:00
|
|
|
|
if (item == "oem")
|
|
|
|
|
continue ;
|
2022-09-13 13:56:16 +08:00
|
|
|
|
if (retainUsers.contains(item))
|
2021-12-09 10:00:07 +08:00
|
|
|
|
continue ;
|
|
|
|
|
|
|
|
|
|
QString subPath = path;
|
|
|
|
|
subPath += "/";
|
|
|
|
|
subPath += item;
|
|
|
|
|
QDir subDir(subPath);
|
|
|
|
|
subDir.removeRecursively();
|
|
|
|
|
|
2022-09-15 15:11:01 +08:00
|
|
|
|
qDebug() << (subcmd + subPath);
|
|
|
|
|
QProcess::execute(subcmd + subPath);
|
2021-12-09 10:00:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-15 15:11:01 +08:00
|
|
|
|
// bool needRm = false;
|
|
|
|
|
// QString subcmd("rm -rf ");
|
|
|
|
|
// QString delDirs;
|
|
|
|
|
// QStringList list = dir.entryList(QDir::NoDotAndDotDot | QDir::Dirs);
|
|
|
|
|
// for (const QString& item : list) {
|
|
|
|
|
// // 出厂备份里面的/home/oem等保留
|
|
|
|
|
// if (item == "oem")
|
|
|
|
|
// continue ;
|
|
|
|
|
// if (retainUsers.contains(item))
|
|
|
|
|
// continue ;
|
|
|
|
|
|
|
|
|
|
// QString subPath = path;
|
|
|
|
|
// subPath += "/";
|
|
|
|
|
// subPath += item;
|
|
|
|
|
// QDir subDir(subPath);
|
|
|
|
|
// subDir.removeRecursively();
|
|
|
|
|
|
|
|
|
|
// delDirs += "${rootmnt}";
|
|
|
|
|
// delDirs += path;
|
|
|
|
|
// delDirs += "/";
|
|
|
|
|
// delDirs += item;
|
|
|
|
|
// delDirs += " ";
|
|
|
|
|
|
|
|
|
|
// needRm = true;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// if (needRm) {
|
|
|
|
|
// QString cmd = QString("bash -c \"%1\" ").arg(subcmd + delDirs);
|
|
|
|
|
// cmd.replace("${rootmnt}", "");
|
|
|
|
|
// QProcess::execute(cmd);
|
|
|
|
|
// }
|
2021-12-09 10:00:07 +08:00
|
|
|
|
}
|
2022-09-13 14:56:06 +08:00
|
|
|
|
|
|
|
|
|
qDebug() << "SystemRestoreProxy::removeHome invoke end";
|
2021-12-09 10:00:07 +08:00
|
|
|
|
}
|
2023-09-05 09:07:25 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 再一次同步
|
|
|
|
|
* @note 意图通过多次同步查缺补漏,如:
|
|
|
|
|
* 1、因为系统还原过程中~/.config等目录中的部分文件可能会更新,会引起部分bug:
|
|
|
|
|
* a. 配网,打开奇安信浏览器,触发自动更新(或者软件商店手动更新),且奇安信默认固定在任务栏;
|
|
|
|
|
* b. 下载安装微信,固定任务栏(新装任意第三方软件均可),会删除desktop文件
|
|
|
|
|
* c. 上面两种操作,都会触发任务栏配置更新;通过监控任务栏配置文件,在系统还原过程中,可以明显的看到,在还原1%的时候,配置文件还原到历史文件,桌面图标还原,然后下一秒配置文件就又更新成当前最新的了
|
|
|
|
|
*/
|
|
|
|
|
void SystemRestoreProxy::rsyncAgain()
|
|
|
|
|
{
|
|
|
|
|
// 保留用户数据还原直接
|
|
|
|
|
if (m_bRetainUserData)
|
|
|
|
|
return ;
|
|
|
|
|
|
|
|
|
|
QString destPath = Utils::getSysRootPath() + "/home/";
|
|
|
|
|
destPath.replace("//", "/");
|
|
|
|
|
Utils::mkpath(destPath);
|
|
|
|
|
|
|
|
|
|
RsyncPathToDirProcess * process = new RsyncPathToDirProcess(this);
|
|
|
|
|
QStringList args;
|
|
|
|
|
args << "-avAHXr";
|
|
|
|
|
args << m_srcPath + "/home/";
|
|
|
|
|
args << destPath;
|
|
|
|
|
process->start(args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 未做完的还原,需要在重启后initrd阶段执行备份还原脚本时再进行还原
|
|
|
|
|
* @note 当前只有备份工具自身未还原
|
|
|
|
|
*/
|
|
|
|
|
void SystemRestoreProxy::undoRestoreData()
|
|
|
|
|
{
|
|
|
|
|
QString destPath = Utils::getSysRootPath() + "/var/log/yhkylin-backup-tools/";
|
|
|
|
|
destPath.replace("//", "/");
|
|
|
|
|
Utils::mkpath(destPath);
|
|
|
|
|
|
|
|
|
|
RsyncPathToDirProcess * process = new RsyncPathToDirProcess(this);
|
|
|
|
|
QStringList args;
|
|
|
|
|
args << "-avAHXr";
|
|
|
|
|
args << m_srcPath + "/usr/bin/backup-daemon";
|
|
|
|
|
args << destPath;
|
|
|
|
|
process->start(args);
|
|
|
|
|
|
|
|
|
|
args.clear();
|
|
|
|
|
args << "-avAHXr";
|
|
|
|
|
args << m_srcPath + "/usr/bin/kybackup";
|
|
|
|
|
args << destPath;
|
|
|
|
|
process->start(args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 还原备份还原工具自身
|
|
|
|
|
* @note 在undoRestoreData基础上增加的一层保险(因为有些机型上使用"update-initramfs -u"命令会失败,如:华为9006c的2203版等)
|
|
|
|
|
* 本来想在/etc/profile.d/目录或/etc/init.d/目录等下面增加一个.sh脚本文件以开机时自动执行,但试验了几种方式均都未执行,原因未知。
|
|
|
|
|
*/
|
|
|
|
|
void SystemRestoreProxy::rsyncBackupSelf()
|
|
|
|
|
{
|
|
|
|
|
QString destPath = Utils::getSysRootPath() + "/usr/bin/";
|
|
|
|
|
destPath.replace("//", "/");
|
|
|
|
|
Utils::mkpath(destPath);
|
|
|
|
|
|
|
|
|
|
RsyncPathToDirProcess * process = new RsyncPathToDirProcess(this);
|
|
|
|
|
QStringList args;
|
|
|
|
|
args << "-avAHXr";
|
|
|
|
|
args << m_srcPath + "/usr/bin/kybackup";
|
|
|
|
|
args << destPath;
|
|
|
|
|
process->start(args);
|
|
|
|
|
|
|
|
|
|
args.clear();
|
|
|
|
|
args << "-avAHXr";
|
|
|
|
|
args << m_srcPath + "/usr/bin/backup-daemon";
|
|
|
|
|
args << destPath;
|
|
|
|
|
process->start(args);
|
|
|
|
|
}
|