public void Backup(BackupInformation backup) { try { //var dump = new MongoDump(backup); //dump.BackupDatabase(); backup.Status = ProcessStatuses.Started; backup.Log("Backup started"); backup.MongoServer.BackupDatabase(backup); if (backup.Compress) { Compression.Zip(backup); Directory.Delete(backup.Directory, true); } if (backup.DropDatabase) { backup.MongoServer.DropDatabase(backup.DatabaseName); } backup.Log("Backup completed"); } catch (Exception ex) { backup.Log(ex.Message, LogLevel.Error); } }
public static void Zip(BackupInformation backup) { var folderName = backup.Directory; var outputFile = backup.File; backup.Log("Compression started."); ZipOutputStream zipStream = null; try { FileStream fsOut = File.Create(outputFile); zipStream = new ZipOutputStream(fsOut); zipStream.SetLevel(3); //0-9, 9 being the highest level of compression zipStream.Password = null; // optional. Null is the same as not setting. Required if using AES. // This setting will strip the leading part of the folder path in the entries, to // make the entries relative to the starting folder. // To include the full path for each entry up to the drive root, assign folderOffset = 0. int folderOffset = folderName.Length + (folderName.EndsWith("\\") ? 0 : 1); CompressFolder(folderName, zipStream, folderOffset); backup.Log("Compression completed."); } catch (Exception ex) { backup.Log(ex.Message, LogLevel.Error); } finally { if (zipStream != null) { zipStream.IsStreamOwner = true; // Makes the Close also Close the underlying stream zipStream.Close(); } } }
public void BackupDatabase(BackupInformation backup) { var currentDB = Client.GetDatabase(backup.DatabaseName); var collections = currentDB.ListCollections().ToListAsync().Result; if (!Directory.Exists(backup.Directory)) { Directory.CreateDirectory(backup.Directory); } if (!Directory.Exists(backup.DatabaseDirectory)) { Directory.CreateDirectory(backup.DatabaseDirectory); } collections.ForEach(c => { var name = c["name"].AsString; var collection = currentDB.GetCollection <BsonDocument>(c["name"].AsString); collection.DumpData(backup.DatabaseDirectory, name); collection.DumpInformation(backup.DatabaseDirectory, name); backup.Log($"{name} has been backed up"); }); }