/// <summary> /// Backup writes a snapshot of the underlying database to dst /// /// If leader is true, this operation is performed with a read consistency /// level equivalent to "weak". Otherwise no guarantees are made about the /// read consistency level. /// </summary> public void Backup(bool leader, BackupFormat fmt, StreamWriter dst) { restoreMu.EnterReadLock(); try { if (leader && !IsLeader) { throw new NotLeaderException(); } switch (fmt) { case BackupFormat.BackupBinary: database(leader, dst); break; case BackupFormat.BackupSQL: dbConn.Dump(dst); break; default: throw new InvalidBackupFormatException(); } stats.NumBackups.WriteMetric(1); } finally { restoreMu.ExitReadLock(); } }
/// <summary> /// Creates a backup format for use with VnManager /// </summary> /// <param name="outputFile"></param> /// <returns></returns> public static bool Compact(string outputFile) { try { var cred = CredentialManager.GetCredentials(App.CredDb); if (cred == null || cred.UserName.Length < 1) { return(false); } File.Copy(Path.Combine(App.ConfigDirPath, App.DbPath), @$ "{App.AssetDirPath}\Import.db"); var fileName = @$ "{App.AssetDirPath}\Import.db"; using (var db = new LiteDatabase($"Filename={fileName};Password='******'")) { db.Rebuild(new RebuildOptions { Password = App.ImportExportDbKey }); } var backup = new BackupFormat(); ZipFile.CreateFromDirectory(@$ "{App.AssetDirPath}\sources", @$ "{App.AssetDirPath}\Images.zip"); using (ZipArchive archive = ZipFile.Open(@$ "{App.AssetDirPath}\output.zip", ZipArchiveMode.Create)) { archive.CreateEntryFromFile(@$ "{App.AssetDirPath}\Images.zip", "Images.zip"); archive.CreateEntryFromFile(@$ "{App.AssetDirPath}\Import.db", "Import.db"); } File.Delete(@$ "{App.AssetDirPath}\Images.zip"); File.Delete(@$ "{App.AssetDirPath}\Import.db"); byte[] originalBytes = File.ReadAllBytes(@$ "{App.AssetDirPath}\output.zip"); File.Delete(@$ "{App.AssetDirPath}\output.zip"); backup.ZippedData = originalBytes; if (File.Exists(outputFile)) { File.Delete(outputFile); } using (var fs = new FileStream(outputFile, FileMode.CreateNew, FileAccess.Write, FileShare.Write)) { using (var writer = new BinaryWriter(fs, Encoding.UTF8, true)) { writer.Write(backup.HeaderBytes); writer.Write(backup.ZippedData); } } return(true); } catch (Exception e) { App.Logger.Error(e, "Failed to compact into backup"); SentryHelper.SendException(e, null, SentryLevel.Error); throw; } }
private void RunBackup() { LevelDesigner level = GetComponent<LevelDesigner>(); if (level == null || (level.Snapshot == null && level.LevelTemplates == null && level.SharedTemplates == null)) { Debug.Log("No backup ran; nothing to backup"); } BackupFormat backup = new BackupFormat() { SnapshotJson = LevelManager.SaveSnapshot(level.Snapshot), LevelTemplateJson = LevelManager.SaveTemplateGroup(level.LevelTemplates), SharedTemplateJson = LevelManager.SaveTemplateGroup(level.SharedTemplates) }; string backupPath = GetBackupPath(); EnsureBackupDirectory(); File.WriteAllText(backupPath, SerializationHelpers.Serialize(backup)); TrimDirectory(NumberOfBackups); }