BackupService.cs 22 KB

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