Skip to main content

ServerBackup

A static class in TavernLib, TavernLib.Backend.Server.

Timed zip backups of the server's world folder (Servers\-1) into its Backups folder, for servers not run by the launcher. Server; started from Tavern unless /launcherauth is given.

Fields​

8 private and internal fields
MemberWhat it is
private static int _busySet to 1 while a backup runs, so two never run at once.
private static string _lastSettingsErrorThe last settings read error that was logged, so the same one isn't logged every minute.
private static Timer _timerThe timer that calls Tick once a minute.
private const string BackupFolderNameName of the folder inside the server data folder that holds the backup zips.
private static readonly TimeSpan CheckIntervalHow often the timer checks whether a backup is due (60 seconds).
private static readonly string[] ExcludedFoldersTop-level folders left out of a backup: Backups and Logs.
private const string NamePrefixStart of every backup file name (ServerBackup_).
private const string StampFormatDate and time format used in a backup file name.

Properties​

MemberWhat it is
static string ServerDirectory { get; }A Township Tale\Servers\-1, the Tavern server's world folder.

Methods​

10 private and internal methods
MemberWhat it is
internal static ServerBackup.BackupResult CreateBackup(string serverDir, DateTime now)Internal: zips the folder (leaving out Backups and Logs) to ServerBackup_<time>.zip. Files in use are skipped; any other failure leaves no zip.
private static IEnumerable<string> EnumerateBackupFiles(string root) (iterator)Yields every file under the server folder, skipping the excluded top-level folders and anything it can't list.
private static string FormatSize(long bytes)Turns a byte count into a short MB or KB text for the log.
internal static bool IsDue(string serverDir, float hours, DateTime nowUtc)Internal: true when backups are on and the newest backup is at least that many hours old, or there is none yet.
internal static string[] ListBackups(string serverDir)Internal: the finished backup zips in the Backups folder, oldest first.
internal static int Prune(string serverDir, int maxBackups)Internal: deletes the oldest backups so at most that many remain (0 keeps all). Returns how many it removed.
internal static void Start()Internal: starts a timer that checks once a minute whether a backup is due.
internal static void Tick()Internal: one check. Re-reads server_backup_time and server_backup_max_backups, backs up if due, then prunes. Never runs two at once.
private static bool TryReadSettings(out float hours, out int maxBackups)Reads the backup interval in hours and the number of backups to keep from server_settings.json; false if it can't be read.
private static DateTimeOffset ZipTime(DateTime time)Clamps a file time into the 1980 to 2107 range a zip can store.