Merge pull request #12410 from huffman/feat/ds-backup-apis

DS backup cleanup
This commit is contained in:
Stephen Birarda 2018-02-15 12:21:56 -07:00 committed by GitHub
commit 84761c003d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 24 deletions

View file

@ -77,6 +77,8 @@ private:
}; };
#include <quazip5/quazipfile.h> #include <quazip5/quazipfile.h>
#include <OctreeUtils.h>
class EntitiesBackupHandler { class EntitiesBackupHandler {
public: public:
EntitiesBackupHandler(QString entitiesFilePath, QString entitiesReplacementFilePath) : EntitiesBackupHandler(QString entitiesFilePath, QString entitiesReplacementFilePath) :
@ -111,18 +113,27 @@ public:
qCritical() << "Failed to open models.json.gz in backup"; qCritical() << "Failed to open models.json.gz in backup";
return; return;
} }
auto data = zipFile.readAll(); auto rawData = zipFile.readAll();
zipFile.close();
OctreeUtils::RawOctreeData data;
if (!OctreeUtils::readOctreeDataInfoFromData(rawData, &data)) {
qCritical() << "Unable to parse octree data during backup recovery";
return;
}
data.resetIdAndVersion();
if (zipFile.getZipError() != UNZ_OK) {
qCritical() << "Failed to unzip models.json.gz: " << zipFile.getZipError();
return;
}
QFile entitiesFile { _entitiesReplacementFilePath }; QFile entitiesFile { _entitiesReplacementFilePath };
if (entitiesFile.open(QIODevice::WriteOnly)) { if (entitiesFile.open(QIODevice::WriteOnly)) {
entitiesFile.write(data); entitiesFile.write(data.toGzippedByteArray());
}
zipFile.close();
if (zipFile.getZipError() != UNZ_OK) {
qCritical() << "Failed to zip models.json.gz: " << zipFile.getZipError();
} }
} }

View file

@ -295,18 +295,21 @@ void DomainContentBackupManager::removeOldBackupVersions(const BackupRule& rule)
backupDir.entryInfoList({ AUTOMATIC_BACKUP_PREFIX + rule.extensionFormat + "*.zip" }, QDir::Files | QDir::NoSymLinks, QDir::Name); backupDir.entryInfoList({ AUTOMATIC_BACKUP_PREFIX + rule.extensionFormat + "*.zip" }, QDir::Files | QDir::NoSymLinks, QDir::Name);
int backupsToDelete = matchingFiles.length() - rule.maxBackupVersions; int backupsToDelete = matchingFiles.length() - rule.maxBackupVersions;
qCDebug(domain_server) << "Found" << matchingFiles.length() << "backups, deleting " << backupsToDelete << "backup(s)"; if (backupsToDelete <= 0) {
for (int i = 0; i < backupsToDelete; ++i) { qCDebug(domain_server) << "Found" << matchingFiles.length() << "backups, no backups need to be deleted";
auto fileInfo = matchingFiles[i].absoluteFilePath(); } else {
QFile backupFile(fileInfo); qCDebug(domain_server) << "Found" << matchingFiles.length() << "backups, deleting " << backupsToDelete << "backup(s)";
if (backupFile.remove()) { for (int i = 0; i < backupsToDelete; ++i) {
qCDebug(domain_server) << "Removed old backup: " << backupFile.fileName(); auto fileInfo = matchingFiles[i].absoluteFilePath();
} else { QFile backupFile(fileInfo);
qCDebug(domain_server) << "Failed to remove old backup: " << backupFile.fileName(); if (backupFile.remove()) {
qCDebug(domain_server) << "Removed old backup: " << backupFile.fileName();
} else {
qCDebug(domain_server) << "Failed to remove old backup: " << backupFile.fileName();
}
} }
qCDebug(domain_server) << "Done removing old backup versions";
} }
qCDebug(domain_server) << "Done removing old backup versions";
} else { } else {
qCDebug(domain_server) << "Rolling backups for rule" << rule.name << "." qCDebug(domain_server) << "Rolling backups for rule" << rule.name << "."
<< " Max Rolled Backup Versions less than 1 [" << rule.maxBackupVersions << "]." << " Max Rolled Backup Versions less than 1 [" << rule.maxBackupVersions << "]."
@ -406,8 +409,20 @@ void DomainContentBackupManager::consolidate(QString fileName) {
} }
} }
void DomainContentBackupManager::createManualBackup(const QString& name) { void DomainContentBackupManager::createManualBackup(MiniPromise::Promise promise, const QString& name) {
createBackup(MANUAL_BACKUP_PREFIX, name); if (QThread::currentThread() != thread()) {
QMetaObject::invokeMethod(this, "createManualBackup", Q_ARG(MiniPromise::Promise, promise),
Q_ARG(const QString&, name));
return;
}
bool success;
QString path;
std::tie(success, path) = createBackup(MANUAL_BACKUP_PREFIX, name);
promise->resolve({
{ "success", success }
});
} }
std::pair<bool, QString> DomainContentBackupManager::createBackup(const QString& prefix, const QString& name) { std::pair<bool, QString> DomainContentBackupManager::createBackup(const QString& prefix, const QString& name) {

View file

@ -57,9 +57,8 @@ public:
void replaceData(QByteArray data); void replaceData(QByteArray data);
void createManualBackup(const QString& name);
public slots: public slots:
void createManualBackup(MiniPromise::Promise promise, const QString& name);
void recoverFromBackup(MiniPromise::Promise promise, const QString& backupName); void recoverFromBackup(MiniPromise::Promise promise, const QString& backupName);
void deleteBackup(MiniPromise::Promise promise, const QString& backupName); void deleteBackup(MiniPromise::Promise promise, const QString& backupName);

View file

@ -2253,9 +2253,17 @@ bool DomainServer::handleHTTPRequest(HTTPConnection* connection, const QUrl& url
return true; return true;
} }
_contentManager->createManualBackup(it.value()); auto deferred = makePromise("createManualBackup");
deferred->then([connection, JSON_MIME_TYPE](QString error, QVariantMap result) {
QJsonObject rootJSON;
auto success = result["success"].toBool();
rootJSON["success"] = success;
QJsonDocument docJSON(rootJSON);
connection->respond(success ? HTTPConnection::StatusCode200 : HTTPConnection::StatusCode400, docJSON.toJson(),
JSON_MIME_TYPE.toUtf8());
});
_contentManager->createManualBackup(deferred, it.value());
connection->respond(HTTPConnection::StatusCode200);
return true; return true;
} else if (url.path() == "/domain_settings") { } else if (url.path() == "/domain_settings") {