A backup is restored at boot, and never undoes a protective act
PrivaPub admin restore <id> (and soon the administrator's page) checks the backup (same host, a format and newest migration this build reads, every hash) and writes restore.json; the running service sees it within seconds and stops, and the next start restores it in MaintenanceGate, before migrations, indexes and hosted services: a pre-restore backup taken once, every collection dropped and imported raw with its indexes, the media the live directory lacks brought back, then the protective merge from the pre-restore backup. Followers and follows are the live ones; blocks, mutes, domain blocks, reserved names, tombstones, reports, filters and OAuth applications are the union; deletions win; accounts made since become tombstones and local posts made since answer 410; every session ends. Each attempt redoes everything; one refused before any change is abandoned and recorded, one failed midway exits 1 for systemd to retry, and after three it exits 75, which the unit no longer restarts. Commands wait (exit 75) while a restore is pending. RestoreRecord tells what happened (admin restore --status). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LsXgEaXee4GCU1hwYgPJXw
This commit is contained in:
1 parent
12bb75809f
commit
fba57318fa
13 files changed
+1050
-11
No files matched your search
@@ -3,6 +3,7 @@ using MongoDB.Entities;
|
||||
using PrivaPub.ClientModels;
|
||||
using PrivaPub.ClientModels.User;
|
||||
using PrivaPub.ClientModels.User.Avatar;
|
||||
using PrivaPub.Infrastructure.Backup;
|
||||
using PrivaPub.Models.User;
|
||||
using PrivaPub.Services;
|
||||
using PrivaPub.Services.ClientToServer.Private;
|
||||
@@ -29,10 +30,15 @@ namespace PrivaPub.Infrastructure.Cli
|
||||
linking them (the deploy's, which can't link www-data's files)
|
||||
PrivaPub admin backups the backups kept, newest first
|
||||
PrivaPub admin backup verify <id> whether a backup's files are what its manifest says
|
||||
PrivaPub admin restore <id> restores a backup: the service stops within seconds and
|
||||
restores it when systemd starts it again; everyone signs
|
||||
in again, and what was made since is lost, except what
|
||||
protects (blocks, deletions, ...)
|
||||
PrivaPub admin restore --status the restore waiting, and the last one done
|
||||
""";
|
||||
|
||||
/// <summary>Whether a command runs before migrations: backups are of the database as it was.</summary>
|
||||
public static bool BeforeMigrations(string[] args) => args is ["backup", ..] or ["backups"];
|
||||
public static bool BeforeMigrations(string[] args) => args is ["backup", ..] or ["backups"] or ["restore", ..];
|
||||
|
||||
public static async Task<int> Run(string[] args, IServiceProvider services, TextReader input = default, TextWriter output = default)
|
||||
{
|
||||
@@ -51,9 +57,13 @@ namespace PrivaPub.Infrastructure.Cli
|
||||
case ["backup", "verify", var id]:
|
||||
return await VerifyBackup(services, id, output);
|
||||
case ["backup", .. var flags] when BackupFlags(flags, out var kind, out var dbOnly):
|
||||
return await Backup(services, kind, dbOnly, output);
|
||||
return await BackUp(services, kind, dbOnly, output);
|
||||
case ["backups"]:
|
||||
return ListBackups(services, output);
|
||||
case ["restore", "--status"]:
|
||||
return await RestoreStatus(services, output);
|
||||
case ["restore", var id] when !id.StartsWith('-'):
|
||||
return await Restore(services, id, output);
|
||||
default:
|
||||
Console.Error.WriteLine(Usage);
|
||||
return 2;
|
||||
@@ -81,9 +91,9 @@ namespace PrivaPub.Infrastructure.Cli
|
||||
return true;
|
||||
}
|
||||
|
||||
static async Task<int> Backup(IServiceProvider services, string kind, bool dbOnly, TextWriter output)
|
||||
static async Task<int> BackUp(IServiceProvider services, string kind, bool dbOnly, TextWriter output)
|
||||
{
|
||||
var (made, error) = await services.GetRequiredService<Backup.Backups>().Create(kind, dbOnly, CancellationToken.None);
|
||||
var (made, error) = await services.GetRequiredService<Backups>().Create(kind, dbOnly, CancellationToken.None);
|
||||
if (made == default)
|
||||
{
|
||||
Console.Error.WriteLine(error);
|
||||
@@ -101,14 +111,14 @@ namespace PrivaPub.Infrastructure.Cli
|
||||
|
||||
static int ListBackups(IServiceProvider services, TextWriter output)
|
||||
{
|
||||
foreach (var backup in services.GetRequiredService<Backup.Backups>().List())
|
||||
foreach (var backup in services.GetRequiredService<Backups>().List())
|
||||
output.WriteLine($"{backup.Id}\t{backup.Kind}\t{backup.CreatedAt:u}\t{backup.Bytes / 1024} KiB\t{backup.Manifest?.Media.Files ?? 0} media{(backup.Manifest?.DbOnly == true ? " (listed)" : string.Empty)}");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static async Task<int> VerifyBackup(IServiceProvider services, string id, TextWriter output)
|
||||
{
|
||||
var problems = await services.GetRequiredService<Backup.Backups>().Verify(id, CancellationToken.None);
|
||||
var problems = await services.GetRequiredService<Backups>().Verify(id, CancellationToken.None);
|
||||
foreach (var problem in problems)
|
||||
output.WriteLine(problem);
|
||||
if (problems.Count > 0)
|
||||
@@ -117,6 +127,48 @@ namespace PrivaPub.Infrastructure.Cli
|
||||
return 0;
|
||||
}
|
||||
|
||||
static async Task<int> Restore(IServiceProvider services, string id, TextWriter output)
|
||||
{
|
||||
var backups = services.GetRequiredService<Backups>();
|
||||
var refused = await ServerRestore.Request(backups.Context(), id, "cli", CancellationToken.None);
|
||||
if (refused != default)
|
||||
{
|
||||
Console.Error.WriteLine(refused);
|
||||
return 1;
|
||||
}
|
||||
output.WriteLine($"{id} will be restored: the running service stops within seconds and restores it when it starts again");
|
||||
output.WriteLine("(systemd starts it again; follow it with journalctl -u privapub, then PrivaPub admin restore --status)");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static async Task<int> RestoreStatus(IServiceProvider services, TextWriter output)
|
||||
{
|
||||
var waiting = RestoreMarker.Read(services.GetRequiredService<Backups>().Root);
|
||||
if (waiting != default)
|
||||
output.WriteLine($"waiting: {waiting.Backup}, {waiting.State}, attempt {waiting.Attempts} of {RestoreMarker.MaxAttempts}, asked by {waiting.RequestedBy} at {waiting.RequestedAt:u}{(waiting.Error == default ? string.Empty : $": {waiting.Error}")}");
|
||||
var last = await DB.Default.Find<RestoreRecord>().Sort(r => r.RestoredAt, Order.Descending).ExecuteFirstAsync();
|
||||
if (last == default)
|
||||
{
|
||||
if (waiting == default)
|
||||
output.WriteLine("no restore asked for, none done");
|
||||
return 0;
|
||||
}
|
||||
if (last.Abandoned)
|
||||
{
|
||||
output.WriteLine($"last: {last.Backup} abandoned at {last.RestoredAt:u}, nothing changed: {last.Error}");
|
||||
return 0;
|
||||
}
|
||||
var report = last.Report;
|
||||
output.WriteLine($"last: {last.Backup} (made {last.BackupCreatedAt:u}) restored at {last.RestoredAt:u}, asked by {last.RequestedBy}; before it: {last.PreRestore}");
|
||||
output.WriteLine($" {report.Documents} documents in {report.Collections} collections, {report.MediaRestored} media files brought back, {report.MediaMissing} missing");
|
||||
output.WriteLine($" deleted again: {report.RootsDeleted} roots, {report.PersonasDeleted} personas, {report.GroupsDeleted} groups, {report.PostsDeleted} posts");
|
||||
output.WriteLine($" made since, now deleted: roots {Names(report.RootsTombstoned)}, personas {Names(report.PersonasTombstoned)}, groups {Names(report.GroupsTombstoned)}; {report.PostsGone} posts gone");
|
||||
output.WriteLine($" {report.ProtectiveKept} protective rows kept, {report.MediaTrashed} media trashed, {report.SessionsEnded} sessions ended");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static string Names(List<string> names) => names.Count == 0 ? "none" : string.Join(", ", names);
|
||||
|
||||
static async Task<int> AuditMedia(IServiceProvider services, bool fix, TextWriter output)
|
||||
{
|
||||
var report = await Domain.Media.MediaAudit.Run(services.GetRequiredService<Domain.Media.IMediaService>(), fix, CancellationToken.None);
|
||||
|
||||
Reference in new issue
Block a user