BackupService.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.IO.Compression;
  5. using System.Linq;
  6. using System.Text.Json;
  7. using System.Text.Json.Nodes;
  8. using System.Text.Json.Serialization;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using Jellyfin.Database.Implementations;
  12. using Jellyfin.Server.Implementations.StorageHelpers;
  13. using Jellyfin.Server.Implementations.SystemBackupService;
  14. using MediaBrowser.Controller;
  15. using MediaBrowser.Controller.SystemBackupService;
  16. using Microsoft.EntityFrameworkCore;
  17. using Microsoft.Extensions.Logging;
  18. namespace Jellyfin.Server.Implementations.FullSystemBackup;
  19. /// <summary>
  20. /// Contains methods for creating and restoring backups.
  21. /// </summary>
  22. public class BackupService : IBackupService
  23. {
  24. private const string ManifestEntryName = "manifest.json";
  25. private readonly ILogger<BackupService> _logger;
  26. private readonly IDbContextFactory<JellyfinDbContext> _dbProvider;
  27. private readonly IServerApplicationHost _applicationHost;
  28. private readonly IServerApplicationPaths _applicationPaths;
  29. private readonly IJellyfinDatabaseProvider _jellyfinDatabaseProvider;
  30. private static readonly JsonSerializerOptions _serializerSettings = new JsonSerializerOptions(JsonSerializerDefaults.General)
  31. {
  32. AllowTrailingCommas = true,
  33. ReferenceHandler = ReferenceHandler.IgnoreCycles,
  34. };
  35. private readonly Version _backupEngineVersion = Version.Parse("0.1.0");
  36. /// <summary>
  37. /// Initializes a new instance of the <see cref="BackupService"/> class.
  38. /// </summary>
  39. /// <param name="logger">A logger.</param>
  40. /// <param name="dbProvider">A Database Factory.</param>
  41. /// <param name="applicationHost">The Application host.</param>
  42. /// <param name="applicationPaths">The application paths.</param>
  43. /// <param name="jellyfinDatabaseProvider">The Jellyfin database Provider in use.</param>
  44. public BackupService(
  45. ILogger<BackupService> logger,
  46. IDbContextFactory<JellyfinDbContext> dbProvider,
  47. IServerApplicationHost applicationHost,
  48. IServerApplicationPaths applicationPaths,
  49. IJellyfinDatabaseProvider jellyfinDatabaseProvider)
  50. {
  51. _logger = logger;
  52. _dbProvider = dbProvider;
  53. _applicationHost = applicationHost;
  54. _applicationPaths = applicationPaths;
  55. _jellyfinDatabaseProvider = jellyfinDatabaseProvider;
  56. }
  57. /// <inheritdoc/>
  58. public void ScheduleRestoreAndRestartServer(string archivePath)
  59. {
  60. _applicationHost.RestoreBackupPath = archivePath;
  61. _applicationHost.ShouldRestart = true;
  62. _applicationHost.NotifyPendingRestart();
  63. }
  64. /// <inheritdoc/>
  65. public async Task RestoreBackupAsync(string archivePath)
  66. {
  67. _logger.LogWarning("Begin restoring system to {BackupArchive}", archivePath); // Info isn't cutting it
  68. if (!File.Exists(archivePath))
  69. {
  70. throw new FileNotFoundException($"Requested backup file '{archivePath}' does not exist.");
  71. }
  72. StorageHelper.TestCommonPathsForStorageCapacity(_applicationPaths, _logger);
  73. var fileStream = File.OpenRead(archivePath);
  74. await using (fileStream.ConfigureAwait(false))
  75. {
  76. using var zipArchive = new ZipArchive(fileStream, ZipArchiveMode.Read, false);
  77. var zipArchiveEntry = zipArchive.GetEntry(ManifestEntryName);
  78. if (zipArchiveEntry is null)
  79. {
  80. throw new NotSupportedException($"The loaded archive '{archivePath}' does not appear to be a Jellyfin backup as its missing the '{ManifestEntryName}'.");
  81. }
  82. BackupManifest? manifest;
  83. var manifestStream = zipArchiveEntry.Open();
  84. await using (manifestStream.ConfigureAwait(false))
  85. {
  86. manifest = await JsonSerializer.DeserializeAsync<BackupManifest>(manifestStream, _serializerSettings).ConfigureAwait(false);
  87. }
  88. if (manifest!.ServerVersion > _applicationHost.ApplicationVersion) // newer versions of Jellyfin should be able to load older versions as we have migrations.
  89. {
  90. throw new NotSupportedException($"The loaded archive '{archivePath}' is made for a newer version of Jellyfin ({manifest.ServerVersion}) and cannot be loaded in this version.");
  91. }
  92. if (!TestBackupVersionCompatibility(manifest.BackupEngineVersion))
  93. {
  94. throw new NotSupportedException($"The loaded archive '{archivePath}' is made for a newer version of Jellyfin ({manifest.ServerVersion}) and cannot be loaded in this version.");
  95. }
  96. void CopyDirectory(string source, string target)
  97. {
  98. source = Path.GetFullPath(source);
  99. Directory.CreateDirectory(source);
  100. foreach (var item in zipArchive.Entries)
  101. {
  102. var sanitizedSourcePath = Path.GetFullPath(item.FullName.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar);
  103. if (!sanitizedSourcePath.StartsWith(target, StringComparison.Ordinal))
  104. {
  105. continue;
  106. }
  107. var targetPath = Path.Combine(source, sanitizedSourcePath[target.Length..].Trim('/'));
  108. _logger.LogInformation("Restore and override {File}", targetPath);
  109. item.ExtractToFile(targetPath);
  110. }
  111. }
  112. CopyDirectory(_applicationPaths.ConfigurationDirectoryPath, "Config/");
  113. CopyDirectory(_applicationPaths.DataPath, "Data/");
  114. CopyDirectory(_applicationPaths.RootFolderPath, "Root/");
  115. _logger.LogInformation("Begin restoring Database");
  116. var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
  117. await using (dbContext.ConfigureAwait(false))
  118. {
  119. dbContext.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
  120. var entityTypes = typeof(JellyfinDbContext).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)
  121. .Where(e => e.PropertyType.IsAssignableTo(typeof(IQueryable)))
  122. .Select(e => (Type: e, Set: e.GetValue(dbContext) as IQueryable))
  123. .ToArray();
  124. var tableNames = entityTypes.Select(f => dbContext.Model.FindEntityType(f.Type.PropertyType.GetGenericArguments()[0])!.GetSchemaQualifiedTableName()!);
  125. _logger.LogInformation("Begin purging database");
  126. await _jellyfinDatabaseProvider.PurgeDatabase(dbContext, tableNames).ConfigureAwait(false);
  127. _logger.LogInformation("Database Purged");
  128. foreach (var entityType in entityTypes)
  129. {
  130. _logger.LogInformation("Read backup of {Table}", entityType.Type.Name);
  131. var zipEntry = zipArchive.GetEntry($"Database\\{entityType.Type.Name}.json");
  132. if (zipEntry is null)
  133. {
  134. _logger.LogInformation("No backup of expected table {Table} is present in backup. Continue anyway.", entityType.Type.Name);
  135. continue;
  136. }
  137. var zipEntryStream = zipEntry.Open();
  138. await using (zipEntryStream.ConfigureAwait(false))
  139. {
  140. _logger.LogInformation("Restore backup of {Table}", entityType.Type.Name);
  141. var records = 0;
  142. await foreach (var item in JsonSerializer.DeserializeAsyncEnumerable<JsonObject>(zipEntryStream, _serializerSettings).ConfigureAwait(false)!)
  143. {
  144. var entity = item.Deserialize(entityType.Type.PropertyType.GetGenericArguments()[0]);
  145. if (entity is null)
  146. {
  147. throw new InvalidOperationException($"Cannot deserialize entity '{item}'");
  148. }
  149. try
  150. {
  151. records++;
  152. dbContext.Add(entity);
  153. }
  154. catch (Exception ex)
  155. {
  156. _logger.LogError(ex, "Could not store entity {Entity} continue anyway.", item);
  157. }
  158. }
  159. _logger.LogInformation("Prepared to restore {Number} entries for {Table}", records, entityType.Type.Name);
  160. }
  161. }
  162. _logger.LogInformation("Try restore Database");
  163. await dbContext.SaveChangesAsync().ConfigureAwait(false);
  164. _logger.LogInformation("Restored database.");
  165. }
  166. _logger.LogInformation("Restored Jellyfin system from {Date}.", manifest.DateCreated);
  167. }
  168. }
  169. private bool TestBackupVersionCompatibility(Version backupEngineVersion)
  170. {
  171. if (backupEngineVersion == _backupEngineVersion)
  172. {
  173. return true;
  174. }
  175. return false;
  176. }
  177. /// <inheritdoc/>
  178. public async Task<BackupManifestDto> CreateBackupAsync(BackupOptionsDto backupOptions)
  179. {
  180. var manifest = new BackupManifest()
  181. {
  182. DateCreated = DateTime.UtcNow,
  183. ServerVersion = _applicationHost.ApplicationVersion,
  184. DatabaseTables = null!,
  185. BackupEngineVersion = _backupEngineVersion,
  186. Options = Map(backupOptions)
  187. };
  188. await _jellyfinDatabaseProvider.RunScheduledOptimisation(CancellationToken.None).ConfigureAwait(false);
  189. var backupFolder = Path.Combine(_applicationPaths.BackupPath);
  190. if (!Directory.Exists(backupFolder))
  191. {
  192. Directory.CreateDirectory(backupFolder);
  193. }
  194. var backupStorageSpace = StorageHelper.GetFreeSpaceOf(_applicationPaths.BackupPath);
  195. const long FiveGigabyte = 5_368_709_115;
  196. if (backupStorageSpace.FreeSpace < FiveGigabyte)
  197. {
  198. throw new InvalidOperationException($"The backup directory '{backupStorageSpace.Path}' does not have at least '{StorageHelper.HumanizeStorageSize(FiveGigabyte)}' free space. Cannot create backup.");
  199. }
  200. var backupPath = Path.Combine(backupFolder, $"jellyfin-backup-{manifest.DateCreated.ToLocalTime():yyyyMMddHHmmss}.zip");
  201. _logger.LogInformation("Attempt to create a new backup at {BackupPath}", backupPath);
  202. var fileStream = File.OpenWrite(backupPath);
  203. await using (fileStream.ConfigureAwait(false))
  204. using (var zipArchive = new ZipArchive(fileStream, ZipArchiveMode.Create, false))
  205. {
  206. _logger.LogInformation("Start backup process.");
  207. var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
  208. await using (dbContext.ConfigureAwait(false))
  209. {
  210. dbContext.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
  211. var entityTypes = typeof(JellyfinDbContext).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)
  212. .Where(e => e.PropertyType.IsAssignableTo(typeof(IQueryable)))
  213. .Select(e => (Type: e, Set: e.GetValue(dbContext) as IQueryable))
  214. .ToArray();
  215. manifest.DatabaseTables = entityTypes.Select(e => e.Type.Name).ToArray();
  216. var transaction = await dbContext.Database.BeginTransactionAsync().ConfigureAwait(false);
  217. await using (transaction.ConfigureAwait(false))
  218. {
  219. _logger.LogInformation("Begin Database backup");
  220. static IAsyncEnumerable<object> GetValues(IQueryable dbSet, Type type)
  221. {
  222. var method = dbSet.GetType().GetMethod(nameof(DbSet<object>.AsAsyncEnumerable))!;
  223. var enumerable = method.Invoke(dbSet, null)!;
  224. return (IAsyncEnumerable<object>)enumerable;
  225. }
  226. foreach (var entityType in entityTypes)
  227. {
  228. _logger.LogInformation("Begin backup of entity {Table}", entityType.Type.Name);
  229. var zipEntry = zipArchive.CreateEntry($"Database\\{entityType.Type.Name}.json");
  230. var entities = 0;
  231. var zipEntryStream = zipEntry.Open();
  232. await using (zipEntryStream.ConfigureAwait(false))
  233. {
  234. var jsonSerializer = new Utf8JsonWriter(zipEntryStream);
  235. await using (jsonSerializer.ConfigureAwait(false))
  236. {
  237. jsonSerializer.WriteStartArray();
  238. var set = GetValues(entityType.Set!, entityType.Type.PropertyType).ConfigureAwait(false);
  239. await foreach (var item in set.ConfigureAwait(false))
  240. {
  241. entities++;
  242. try
  243. {
  244. JsonSerializer.SerializeToDocument(item, _serializerSettings).WriteTo(jsonSerializer);
  245. }
  246. catch (Exception ex)
  247. {
  248. _logger.LogError(ex, "Could not load entity {Entity}", item);
  249. throw;
  250. }
  251. }
  252. jsonSerializer.WriteEndArray();
  253. }
  254. }
  255. _logger.LogInformation("backup of entity {Table} with {Number} created", entityType.Type.Name, entities);
  256. }
  257. }
  258. }
  259. _logger.LogInformation("Backup of folder {Table}", _applicationPaths.ConfigurationDirectoryPath);
  260. foreach (var item in Directory.EnumerateFiles(_applicationPaths.ConfigurationDirectoryPath, "*.xml", SearchOption.TopDirectoryOnly)
  261. .Union(Directory.EnumerateFiles(_applicationPaths.ConfigurationDirectoryPath, "*.json", SearchOption.TopDirectoryOnly)))
  262. {
  263. zipArchive.CreateEntryFromFile(item, Path.Combine("Config", Path.GetFileName(item)));
  264. }
  265. void CopyDirectory(string source, string target, string filter = "*")
  266. {
  267. if (!Directory.Exists(source))
  268. {
  269. return;
  270. }
  271. _logger.LogInformation("Backup of folder {Table}", source);
  272. foreach (var item in Directory.EnumerateFiles(source, filter, SearchOption.AllDirectories))
  273. {
  274. zipArchive.CreateEntryFromFile(item, Path.Combine(target, item[..source.Length].Trim('\\')));
  275. }
  276. }
  277. CopyDirectory(Path.Combine(_applicationPaths.ConfigurationDirectoryPath, "users"), Path.Combine("Config", "users"));
  278. CopyDirectory(Path.Combine(_applicationPaths.ConfigurationDirectoryPath, "ScheduledTasks"), Path.Combine("Config", "ScheduledTasks"));
  279. CopyDirectory(Path.Combine(_applicationPaths.RootFolderPath), "Root");
  280. CopyDirectory(Path.Combine(_applicationPaths.DataPath, "collections"), Path.Combine("Data", "collections"));
  281. CopyDirectory(Path.Combine(_applicationPaths.DataPath, "playlists"), Path.Combine("Data", "playlists"));
  282. CopyDirectory(Path.Combine(_applicationPaths.DataPath, "ScheduledTasks"), Path.Combine("Data", "ScheduledTasks"));
  283. if (backupOptions.Subtitles)
  284. {
  285. CopyDirectory(Path.Combine(_applicationPaths.DataPath, "subtitles"), Path.Combine("Data", "subtitles"));
  286. }
  287. if (backupOptions.Trickplay)
  288. {
  289. CopyDirectory(Path.Combine(_applicationPaths.DataPath, "trickplay"), Path.Combine("Data", "trickplay"));
  290. }
  291. if (backupOptions.Metadata)
  292. {
  293. CopyDirectory(Path.Combine(_applicationPaths.InternalMetadataPath), Path.Combine("Data", "metadata"));
  294. }
  295. var manifestStream = zipArchive.CreateEntry(ManifestEntryName).Open();
  296. await using (manifestStream.ConfigureAwait(false))
  297. {
  298. await JsonSerializer.SerializeAsync(manifestStream, manifest).ConfigureAwait(false);
  299. }
  300. }
  301. _logger.LogInformation("Backup created");
  302. return Map(manifest, backupPath);
  303. }
  304. /// <inheritdoc/>
  305. public async Task<BackupManifestDto?> GetBackupManifest(string archivePath)
  306. {
  307. if (!File.Exists(archivePath))
  308. {
  309. return null;
  310. }
  311. BackupManifest? manifest;
  312. try
  313. {
  314. manifest = await GetManifest(archivePath).ConfigureAwait(false);
  315. }
  316. catch (Exception ex)
  317. {
  318. _logger.LogError(ex, "Tried to load archive from {Path} but failed.", archivePath);
  319. return null;
  320. }
  321. if (manifest is null)
  322. {
  323. return null;
  324. }
  325. return Map(manifest, archivePath);
  326. }
  327. /// <inheritdoc/>
  328. public async Task<BackupManifestDto[]> EnumerateBackups()
  329. {
  330. if (!Directory.Exists(_applicationPaths.BackupPath))
  331. {
  332. return [];
  333. }
  334. var archives = Directory.EnumerateFiles(_applicationPaths.BackupPath, "*.zip");
  335. var manifests = new List<BackupManifestDto>();
  336. foreach (var item in archives)
  337. {
  338. try
  339. {
  340. var manifest = await GetManifest(item).ConfigureAwait(false);
  341. if (manifest is null)
  342. {
  343. continue;
  344. }
  345. manifests.Add(Map(manifest, item));
  346. }
  347. catch (Exception ex)
  348. {
  349. _logger.LogError(ex, "Could not load {BackupArchive} path.", item);
  350. }
  351. }
  352. return manifests.ToArray();
  353. }
  354. private static async ValueTask<BackupManifest?> GetManifest(string archivePath)
  355. {
  356. var archiveStream = File.OpenRead(archivePath);
  357. await using (archiveStream.ConfigureAwait(false))
  358. {
  359. using var zipStream = new ZipArchive(archiveStream, ZipArchiveMode.Read);
  360. var manifestEntry = zipStream.GetEntry(ManifestEntryName);
  361. if (manifestEntry is null)
  362. {
  363. return null;
  364. }
  365. var manifestStream = manifestEntry.Open();
  366. await using (manifestStream.ConfigureAwait(false))
  367. {
  368. return await JsonSerializer.DeserializeAsync<BackupManifest>(manifestStream, _serializerSettings).ConfigureAwait(false);
  369. }
  370. }
  371. }
  372. private static BackupManifestDto Map(BackupManifest manifest, string path)
  373. {
  374. return new BackupManifestDto()
  375. {
  376. BackupEngineVersion = manifest.BackupEngineVersion,
  377. DateCreated = manifest.DateCreated,
  378. ServerVersion = manifest.ServerVersion,
  379. Path = path,
  380. Options = Map(manifest.Options)
  381. };
  382. }
  383. private static BackupOptionsDto Map(BackupOptions options)
  384. {
  385. return new BackupOptionsDto()
  386. {
  387. Metadata = options.Metadata,
  388. Subtitles = options.Subtitles,
  389. Trickplay = options.Trickplay
  390. };
  391. }
  392. private static BackupOptions Map(BackupOptionsDto options)
  393. {
  394. return new BackupOptions()
  395. {
  396. Metadata = options.Metadata,
  397. Subtitles = options.Subtitles,
  398. Trickplay = options.Trickplay
  399. };
  400. }
  401. }