mcde: backup by BorgBackup to a disk of its own, with a watcher for space
McdeSys gets the backup (lib/McdeSys/backup.c): a partition becomes the backup disk with an encrypted repository (borg init -e repokey-blake2); every hour root's crontab runs dtbackup run: borg create of / (and /home) on its own file systems, lz4, then prune -- 24 hourly, 30 daily, 52 weekly, every monthly unless set otherwise (keep_* in /etc/mcde-backup.conf, -1 keeps every one) -- and compact. The watcher: before and after each run, while the backup disk has less than min_free percent free (10 unless set), the oldest backup is deleted and the space given back; the newest is never deleted. Listing (borg list --json) and restoring a path into a folder (borg extract). The passphrase lives in /etc/mcde-backup.pass, root's alone, handed over by BORG_PASSCOMMAND. docs/reference/borg.txt from borg 1.4.5's own documentation. dtbackup run | status | list: the same from a shell and for cron, no X. Measured: builds on the Linux host and compiles in the build VM; as a user dtbackup status says no disk is set up and dtbackup run refuses. Not measured: a real backup -- borg is not on the host and goes into the set with the next userland. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5eee8d5d8f
commit
c8e8587f9b
9 files changed
+624
-3
No files matched your search
@@ -144,6 +144,16 @@ place of CDE; pkgsrc builds it on any platform from `pkgsrc/`.
|
||||
libpng, the rest through XmGetPixmap -- Motif 2.3.8 draws an RGB PNG
|
||||
without alpha as its blue channel only (docs/reference/motif-png.txt in
|
||||
the Makronix tree). Give such pixmaps back with McdeImageFreePixmap.
|
||||
- Backup (`lib/McdeSys/backup.c`, `programs/dtbackup`): BorgBackup to a
|
||||
partition of its own -- borg init -e repokey-blake2, create -x of / (and
|
||||
/home), prune by settings (keep_hourly/daily/weekly/monthly, -1 = every
|
||||
one), compact; the watcher deletes the oldest backups, never the newest,
|
||||
while the disk has less than min_free percent free, before and after a
|
||||
run. /etc/mcde-backup.conf, the passphrase in /etc/mcde-backup.pass
|
||||
(root, 0600: the hourly run needs nobody; the disk is encrypted against
|
||||
whoever takes it, not against this computer's root), the last run in
|
||||
/var/db/mcde-backup.state; hourly by a "# MCDE backup" block in root's
|
||||
crontab running `dtbackup run` (no X). docs/reference/borg.txt.
|
||||
- `programs/dtdiskutility` -- the disks: drives with their partitions on
|
||||
the left; on the right name, model, size, file system, mount point and a
|
||||
bar of used and free; First Aid (Verify Disk: fsck -n; Verify and Repair
|
||||
|
||||
@@ -811,6 +811,7 @@ programs/dtlaunch/Makefile
|
||||
programs/dtbar/Makefile
|
||||
programs/dtsystemsettings/Makefile
|
||||
programs/dtdiskutility/Makefile
|
||||
programs/dtbackup/Makefile
|
||||
|
||||
programs/dtaction/Makefile
|
||||
|
||||
|
||||
+42
-1
@@ -16,7 +16,7 @@
|
||||
*
|
||||
* Status: lib/McdeSys implements mcde_sys_backend, mcde_sys_error,
|
||||
* mcde_sys_info, mcde_sys_power, mcde_sys_interfaces, the sound, Bluetooth,
|
||||
* battery, time zone, sharing, the network browser, disks,
|
||||
* battery, time zone, sharing, the network browser, disks, backup,
|
||||
* user and software update functions (common.c, net.c, sound.c, users.c,
|
||||
* netbsd.c, linux.c); the network setup and the host name setter are the
|
||||
* contract still.
|
||||
@@ -282,6 +282,47 @@ int mcde_sys_disk_erase(const char *name, const char *fstype,
|
||||
*/
|
||||
int mcde_sys_permissions(int repair, char *log, size_t len);
|
||||
|
||||
/*
|
||||
* Backup: the whole system, every hour, to a disk of its own, by BorgBackup
|
||||
* -- encrypted (repokey-blake2), deduplicated; kept are, unless set
|
||||
* otherwise, 24 hourly, 30 daily, 52 weekly and every monthly state, and
|
||||
* when the disk's free space falls below a share (10 %), the oldest are
|
||||
* deleted until it is back. Setting up, running, listing and restoring
|
||||
* need root.
|
||||
*/
|
||||
typedef struct {
|
||||
int configured; /* a disk and a repository are set up */
|
||||
int enabled; /* it runs every hour */
|
||||
char disk[32]; /* the partition it backs up to */
|
||||
char repo[512]; /* the repository on it */
|
||||
long long last; /* the last backup's time (time_t), or 0 */
|
||||
char result[256]; /* how the last one ended */
|
||||
/* how many are kept (-1: every one); and the free space below which
|
||||
* the oldest backups are deleted, in percent of the disk */
|
||||
int keepHourly, keepDaily, keepWeekly, keepMonthly;
|
||||
int minFree;
|
||||
} McdeSysBackup;
|
||||
|
||||
typedef struct {
|
||||
char name[128];
|
||||
char time[32]; /* "2026-11-11T14:00:03" */
|
||||
} McdeSysBackupArchive;
|
||||
|
||||
int mcde_sys_backup_get(McdeSysBackup *b);
|
||||
/* make PARTITION (mounted, or mountable) the backup disk: a repository on
|
||||
* it, encrypted with PASSPHRASE, which is kept for the hourly runs */
|
||||
int mcde_sys_backup_setup(const char *partition, const char *passphrase);
|
||||
int mcde_sys_backup_enable(int on);
|
||||
/* how many are kept and the free space to hold, from B (root) */
|
||||
int mcde_sys_backup_policy(const McdeSysBackup *b);
|
||||
/* one backup now (the hourly run does this); borg's report into LOG */
|
||||
int mcde_sys_backup_run(char *log, size_t len);
|
||||
/* the backups there are, oldest first; how many */
|
||||
int mcde_sys_backup_archives(McdeSysBackupArchive *a, size_t max);
|
||||
/* PATH (absolute, as it was) of backup ARCHIVE, into the folder DEST */
|
||||
int mcde_sys_backup_restore(const char *archive, const char *path,
|
||||
const char *dest);
|
||||
|
||||
/*
|
||||
* A process: the command line it was started with, as one line a shell
|
||||
* reads back (each argument in single quotes). For the front panel, which
|
||||
|
||||
@@ -5,7 +5,7 @@ MAINTAINERCLEANFILES = Makefile.in
|
||||
lib_LTLIBRARIES = libMcdeSys.la
|
||||
|
||||
libMcdeSys_la_CPPFLAGS = -I$(top_srcdir)/include
|
||||
libMcdeSys_la_SOURCES = common.c net.c sound.c users.c share.c netmount.c private.h
|
||||
libMcdeSys_la_SOURCES = common.c net.c sound.c users.c share.c netmount.c backup.c private.h
|
||||
if NETBSD
|
||||
libMcdeSys_la_SOURCES += netbsd.c disk_netbsd.c
|
||||
# crypt(3), for the password hash; proplib(3), for the battery; libdns_sd
|
||||
|
||||
@@ -0,0 +1,483 @@
|
||||
/*
|
||||
* backup.c -- MCDE: the whole system, every hour, to a disk of its own
|
||||
*
|
||||
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
|
||||
*
|
||||
* BorgBackup does the work (docs/reference/borg.txt in the Makronix tree):
|
||||
*
|
||||
* borg init -e repokey-blake2 REPO encrypted; the key lives in the
|
||||
* repository, behind the passphrase
|
||||
* borg create -x --exclude-caches --compression lz4
|
||||
* REPO::{hostname}-{now:%Y-%m-%dT%H:%M} / [/home]
|
||||
* -x: no other file systems, so not
|
||||
* /proc, /dev, /tmp nor the backup
|
||||
* disk itself
|
||||
* borg prune -H 24 -d 30 -w 52 -m -1 24 hourly, 30 daily, 52 weekly,
|
||||
* every monthly (negative: no limit);
|
||||
* the numbers are settings
|
||||
* borg compact gives the space back
|
||||
* borg delete REPO::OLDEST when the disk's free space is
|
||||
* below min_free percent, before and
|
||||
* after a backup -- the oldest first,
|
||||
* never the newest
|
||||
* borg list --json, borg extract
|
||||
*
|
||||
* The passphrase is kept in /etc/mcde-backup.pass (root, 0600) and handed
|
||||
* to borg by BORG_PASSCOMMAND, so the hourly run needs nobody; the disk is
|
||||
* encrypted against whoever takes it away, not against root of this
|
||||
* computer. /etc/mcde-backup.conf names the partition and the repository;
|
||||
* /var/db/mcde-backup.state the last run. The hourly run is a line in
|
||||
* root's crontab(5), between "# MCDE backup" markers: dtbackup run.
|
||||
*
|
||||
* POSIX; the system's part (mounting the disk) is the backend's
|
||||
* (mcde_sys_disk_mount).
|
||||
*/
|
||||
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/statvfs.h>
|
||||
|
||||
#include <Mcde/McdeSys.h>
|
||||
|
||||
#include "private.h"
|
||||
|
||||
#define CONF "/etc/mcde-backup.conf"
|
||||
#define PASS "/etc/mcde-backup.pass"
|
||||
#define STATE "/var/db/mcde-backup.state"
|
||||
#define PASSCMD "BORG_PASSCOMMAND=cat " PASS
|
||||
|
||||
static const char *const borgPaths[] = { "/usr/pkg/bin/borg",
|
||||
"/usr/bin/borg", "/usr/local/bin/borg", NULL };
|
||||
static const char *const crontabPaths[] = { "/usr/bin/crontab", NULL };
|
||||
static const char *const dtbackupPaths[] = { "/usr/pkg/dt/bin/dtbackup",
|
||||
"/usr/dt/bin/dtbackup", NULL };
|
||||
|
||||
static void
|
||||
Value(const char *file, const char *key, char *buf, size_t len)
|
||||
{
|
||||
buf[0] = '\0';
|
||||
(void) mcde_sys_file_value(file, key, buf, len);
|
||||
}
|
||||
|
||||
int
|
||||
mcde_sys_backup_get(McdeSysBackup *b)
|
||||
{
|
||||
char v[64];
|
||||
|
||||
memset(b, 0, sizeof *b);
|
||||
Value(CONF, "disk=", b->disk, sizeof b->disk);
|
||||
Value(CONF, "repo=", b->repo, sizeof b->repo);
|
||||
Value(CONF, "enabled=", v, sizeof v);
|
||||
b->configured = b->disk[0] != '\0' && b->repo[0] != '\0';
|
||||
b->enabled = b->configured && strcmp(v, "1") == 0;
|
||||
Value(STATE, "last=", v, sizeof v);
|
||||
b->last = atoll(v);
|
||||
Value(STATE, "result=", b->result, sizeof b->result);
|
||||
/* the settings, or their defaults */
|
||||
b->keepHourly = 24;
|
||||
b->keepDaily = 30;
|
||||
b->keepWeekly = 52;
|
||||
b->keepMonthly = -1;
|
||||
b->minFree = 10;
|
||||
Value(CONF, "keep_hourly=", v, sizeof v);
|
||||
if (v[0] != '\0')
|
||||
b->keepHourly = atoi(v);
|
||||
Value(CONF, "keep_daily=", v, sizeof v);
|
||||
if (v[0] != '\0')
|
||||
b->keepDaily = atoi(v);
|
||||
Value(CONF, "keep_weekly=", v, sizeof v);
|
||||
if (v[0] != '\0')
|
||||
b->keepWeekly = atoi(v);
|
||||
Value(CONF, "keep_monthly=", v, sizeof v);
|
||||
if (v[0] != '\0')
|
||||
b->keepMonthly = atoi(v);
|
||||
Value(CONF, "min_free=", v, sizeof v);
|
||||
if (v[0] != '\0')
|
||||
b->minFree = atoi(v);
|
||||
return MCDE_SYS_OK;
|
||||
}
|
||||
|
||||
static int
|
||||
WriteConf(const McdeSysBackup *b)
|
||||
{
|
||||
FILE *fp = fopen(CONF ".new", "w");
|
||||
|
||||
if (fp == NULL)
|
||||
return mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot write %s.", CONF);
|
||||
fprintf(fp, "# MCDE backup (System Settings, Backup)\ndisk=%s\nrepo=%s\n"
|
||||
"enabled=%d\n# kept: a number, -1 for every one\nkeep_hourly=%d\n"
|
||||
"keep_daily=%d\nkeep_weekly=%d\nkeep_monthly=%d\n"
|
||||
"# below this many percent free the oldest backups go\n"
|
||||
"min_free=%d\n", b->disk, b->repo, b->enabled, b->keepHourly,
|
||||
b->keepDaily, b->keepWeekly, b->keepMonthly, b->minFree);
|
||||
if (fclose(fp) != 0 || rename(CONF ".new", CONF) != 0)
|
||||
return mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot write %s.", CONF);
|
||||
return MCDE_SYS_OK;
|
||||
}
|
||||
|
||||
static void
|
||||
WriteState(const char *result)
|
||||
{
|
||||
FILE *fp = fopen(STATE, "w");
|
||||
|
||||
if (fp == NULL)
|
||||
return;
|
||||
fprintf(fp, "last=%lld\nresult=%s\n", (long long) time(NULL), result);
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
/* the backup disk mounted; the repository's folder made */
|
||||
static int
|
||||
Ready(McdeSysBackup *b)
|
||||
{
|
||||
char where[300];
|
||||
|
||||
if (mcde_sys_disk_mount(b->disk, where, sizeof where) != MCDE_SYS_OK)
|
||||
return MCDE_SYS_EFAIL;
|
||||
if (strncmp(b->repo, where, strlen(where)) != 0)
|
||||
return mcde_sys_fail(MCDE_SYS_EFAIL, "The backup disk is mounted "
|
||||
"on %s, not where the backups are (%s).", where, b->repo);
|
||||
return MCDE_SYS_OK;
|
||||
}
|
||||
|
||||
int
|
||||
mcde_sys_backup_setup(const char *partition, const char *passphrase)
|
||||
{
|
||||
McdeSysBackup b;
|
||||
const char *borg = mcde_sys_which(borgPaths);
|
||||
char where[300], host[128];
|
||||
FILE *fp;
|
||||
int fd;
|
||||
|
||||
if (geteuid() != 0)
|
||||
return mcde_sys_fail(MCDE_SYS_EPERM, "Only the administrator "
|
||||
"can set up the backup.");
|
||||
if (borg == NULL)
|
||||
return mcde_sys_fail(MCDE_SYS_ENOTSUP, "BorgBackup is not "
|
||||
"installed.");
|
||||
if (passphrase == NULL || strlen(passphrase) < 8 ||
|
||||
strchr(passphrase, '\n') != NULL)
|
||||
return mcde_sys_fail(MCDE_SYS_EINVAL, "The password needs 8 "
|
||||
"characters or more.");
|
||||
if (mcde_sys_disk_mount(partition, where, sizeof where) != MCDE_SYS_OK)
|
||||
return MCDE_SYS_EFAIL;
|
||||
if (mcde_sys_hostname_get(host, sizeof host) != MCDE_SYS_OK)
|
||||
snprintf(host, sizeof host, "computer");
|
||||
host[strcspn(host, ".")] = '\0';
|
||||
memset(&b, 0, sizeof b);
|
||||
snprintf(b.disk, sizeof b.disk, "%s", partition);
|
||||
snprintf(b.repo, sizeof b.repo, "%s/Backups.%s", where, host);
|
||||
/* the passphrase for the hourly runs: root alone reads it */
|
||||
if ((fd = open(PASS ".new", O_WRONLY | O_CREAT | O_TRUNC, 0600)) < 0 ||
|
||||
(fp = fdopen(fd, "w")) == NULL)
|
||||
return mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot write %s.", PASS);
|
||||
fprintf(fp, "%s\n", passphrase);
|
||||
if (fclose(fp) != 0 || rename(PASS ".new", PASS) != 0)
|
||||
return mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot write %s.", PASS);
|
||||
{
|
||||
char *argv[] = { "/usr/bin/env", PASSCMD, (char *) borg,
|
||||
"init", "--encryption", "repokey-blake2", b.repo, NULL };
|
||||
struct stat st;
|
||||
|
||||
/* a repository there already (the same disk, set up again)
|
||||
* is taken as it is */
|
||||
if (stat(b.repo, &st) != 0 && mcde_sys_run(argv) != MCDE_SYS_OK)
|
||||
return MCDE_SYS_EFAIL;
|
||||
}
|
||||
b.enabled = 0;
|
||||
b.keepHourly = 24;
|
||||
b.keepDaily = 30;
|
||||
b.keepWeekly = 52;
|
||||
b.keepMonthly = -1;
|
||||
b.minFree = 10;
|
||||
return WriteConf(&b);
|
||||
}
|
||||
|
||||
int
|
||||
mcde_sys_backup_policy(const McdeSysBackup *p)
|
||||
{
|
||||
McdeSysBackup b;
|
||||
|
||||
if (geteuid() != 0)
|
||||
return mcde_sys_fail(MCDE_SYS_EPERM, "Only the administrator "
|
||||
"can change the backup.");
|
||||
(void) mcde_sys_backup_get(&b);
|
||||
if (!b.configured)
|
||||
return mcde_sys_fail(MCDE_SYS_EINVAL, "Choose a backup disk "
|
||||
"first.");
|
||||
if (p->minFree < 1 || p->minFree > 90 || p->keepHourly < -1 ||
|
||||
p->keepDaily < -1 || p->keepWeekly < -1 || p->keepMonthly < -1)
|
||||
return mcde_sys_fail(MCDE_SYS_EINVAL, "Those numbers cannot "
|
||||
"be used.");
|
||||
b.keepHourly = p->keepHourly;
|
||||
b.keepDaily = p->keepDaily;
|
||||
b.keepWeekly = p->keepWeekly;
|
||||
b.keepMonthly = p->keepMonthly;
|
||||
b.minFree = p->minFree;
|
||||
return WriteConf(&b);
|
||||
}
|
||||
|
||||
/* the free share of the disk REPO is on, in percent; -1 when unknown */
|
||||
static int
|
||||
FreePercent(const char *repo)
|
||||
{
|
||||
struct statvfs sv;
|
||||
|
||||
if (statvfs(repo, &sv) != 0 || sv.f_blocks == 0)
|
||||
return -1;
|
||||
return (int) ((double) sv.f_bavail * 100.0 / (double) sv.f_blocks);
|
||||
}
|
||||
|
||||
/* the watcher: while the disk has less than MINFREE percent free, the
|
||||
* oldest backup is deleted and the space given back -- the newest stays.
|
||||
* How many went into *GONE. */
|
||||
static void
|
||||
Thin(const char *borg, const char *repo, int minFree, int *gone)
|
||||
{
|
||||
static McdeSysBackupArchive a[4096];
|
||||
char what[800];
|
||||
int n, i = 0;
|
||||
|
||||
*gone = 0;
|
||||
if (FreePercent(repo) < 0 || FreePercent(repo) >= minFree)
|
||||
return;
|
||||
n = mcde_sys_backup_archives(a, sizeof a / sizeof a[0]);
|
||||
/* borg list gives them oldest first */
|
||||
while (n - i > 1 && FreePercent(repo) < minFree) {
|
||||
char *del[] = { "/usr/bin/env", PASSCMD, (char *) borg,
|
||||
"delete", what, NULL };
|
||||
char *compact[] = { "/usr/bin/env", PASSCMD, (char *) borg,
|
||||
"compact", (char *) repo, NULL };
|
||||
|
||||
snprintf(what, sizeof what, "%s::%s", repo, a[i].name);
|
||||
if (mcde_sys_run(del) != MCDE_SYS_OK)
|
||||
break;
|
||||
(void) mcde_sys_run(compact);
|
||||
(*gone)++;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
mcde_sys_backup_enable(int on)
|
||||
{
|
||||
McdeSysBackup b;
|
||||
const char *ct = mcde_sys_which(crontabPaths);
|
||||
const char *db = mcde_sys_which(dtbackupPaths);
|
||||
static char out[16384], in[17000];
|
||||
char *line, *last = NULL;
|
||||
size_t n = 0;
|
||||
int skip = 0;
|
||||
|
||||
if (geteuid() != 0)
|
||||
return mcde_sys_fail(MCDE_SYS_EPERM, "Only the administrator "
|
||||
"can switch the backup.");
|
||||
(void) mcde_sys_backup_get(&b);
|
||||
if (on && !b.configured)
|
||||
return mcde_sys_fail(MCDE_SYS_EINVAL, "Choose a backup disk "
|
||||
"first.");
|
||||
if (ct == NULL)
|
||||
return mcde_sys_fail(MCDE_SYS_ENOTSUP, "crontab is not there.");
|
||||
if (on && db == NULL)
|
||||
return mcde_sys_fail(MCDE_SYS_ENOTSUP, "dtbackup is not there.");
|
||||
/* root's crontab with the MCDE lines replaced (crontab(1) -l, -) */
|
||||
{
|
||||
char *argv[] = { (char *) ct, "-l", NULL };
|
||||
|
||||
if (mcde_sys_capture(argv, out, sizeof out) != MCDE_SYS_OK)
|
||||
out[0] = '\0'; /* no crontab yet */
|
||||
}
|
||||
in[0] = '\0';
|
||||
for (line = strtok_r(out, "\n", &last); line != NULL;
|
||||
line = strtok_r(NULL, "\n", &last)) {
|
||||
if (strcmp(line, "# MCDE backup begin") == 0) {
|
||||
skip = 1;
|
||||
continue;
|
||||
}
|
||||
if (strcmp(line, "# MCDE backup end") == 0) {
|
||||
skip = 0;
|
||||
continue;
|
||||
}
|
||||
if (!skip && n + strlen(line) + 2 < sizeof in)
|
||||
n += (size_t) snprintf(in + n, sizeof in - n, "%s\n",
|
||||
line);
|
||||
}
|
||||
if (on)
|
||||
n += (size_t) snprintf(in + n, sizeof in - n,
|
||||
"# MCDE backup begin\n17 * * * * %s run >/dev/null 2>&1\n"
|
||||
"# MCDE backup end\n", db);
|
||||
{
|
||||
char *argv[] = { (char *) ct, "-", NULL };
|
||||
|
||||
if (mcde_sys_run_input(argv, in) != MCDE_SYS_OK)
|
||||
return MCDE_SYS_EFAIL;
|
||||
}
|
||||
b.enabled = on;
|
||||
return WriteConf(&b);
|
||||
}
|
||||
|
||||
int
|
||||
mcde_sys_backup_run(char *log, size_t len)
|
||||
{
|
||||
McdeSysBackup b;
|
||||
const char *borg = mcde_sys_which(borgPaths);
|
||||
char archive[700];
|
||||
int r;
|
||||
|
||||
log[0] = '\0';
|
||||
if (geteuid() != 0)
|
||||
return mcde_sys_fail(MCDE_SYS_EPERM, "Only the administrator "
|
||||
"can back up the system.");
|
||||
(void) mcde_sys_backup_get(&b);
|
||||
if (!b.configured)
|
||||
return mcde_sys_fail(MCDE_SYS_EINVAL, "No backup disk is set "
|
||||
"up.");
|
||||
if (borg == NULL)
|
||||
return mcde_sys_fail(MCDE_SYS_ENOTSUP, "BorgBackup is not "
|
||||
"installed.");
|
||||
if (Ready(&b) != MCDE_SYS_OK) {
|
||||
WriteState(mcde_sys_error());
|
||||
return MCDE_SYS_EFAIL;
|
||||
}
|
||||
/* room first: a full disk would stop the backup halfway */
|
||||
{
|
||||
int gone;
|
||||
|
||||
Thin(borg, b.repo, b.minFree, &gone);
|
||||
}
|
||||
snprintf(archive, sizeof archive, "%s::{hostname}-{now:%%Y-%%m-%%dT%%H:%%M}",
|
||||
b.repo);
|
||||
{
|
||||
struct stat st;
|
||||
char *argv[] = { "/usr/bin/env", PASSCMD, (char *) borg,
|
||||
"create", "--one-file-system", "--exclude-caches",
|
||||
"--compression", "lz4", "--stats", archive, "/",
|
||||
stat("/home", &st) == 0 ? "/home" : NULL, NULL };
|
||||
|
||||
r = mcde_sys_capture(argv, log, len);
|
||||
}
|
||||
if (r != MCDE_SYS_OK) {
|
||||
WriteState("the backup did not succeed");
|
||||
return r;
|
||||
}
|
||||
{
|
||||
char h[16], d[16], w[16], m[16];
|
||||
char *prune[] = { "/usr/bin/env", PASSCMD, (char *) borg,
|
||||
"prune", "--keep-hourly", h, "--keep-daily", d,
|
||||
"--keep-weekly", w, "--keep-monthly", m, b.repo, NULL };
|
||||
char *compact[] = { "/usr/bin/env", PASSCMD, (char *) borg,
|
||||
"compact", b.repo, NULL };
|
||||
|
||||
snprintf(h, sizeof h, "%d", b.keepHourly);
|
||||
snprintf(d, sizeof d, "%d", b.keepDaily);
|
||||
snprintf(w, sizeof w, "%d", b.keepWeekly);
|
||||
snprintf(m, sizeof m, "%d", b.keepMonthly);
|
||||
(void) mcde_sys_run(prune);
|
||||
(void) mcde_sys_run(compact);
|
||||
}
|
||||
{
|
||||
int gone;
|
||||
char text[128];
|
||||
|
||||
Thin(borg, b.repo, b.minFree, &gone);
|
||||
if (gone > 0) {
|
||||
snprintf(text, sizeof text, "ok; %d of the oldest "
|
||||
"backups deleted for space", gone);
|
||||
WriteState(text);
|
||||
return MCDE_SYS_OK;
|
||||
}
|
||||
}
|
||||
WriteState("ok");
|
||||
return MCDE_SYS_OK;
|
||||
}
|
||||
|
||||
int
|
||||
mcde_sys_backup_archives(McdeSysBackupArchive *a, size_t max)
|
||||
{
|
||||
McdeSysBackup b;
|
||||
const char *borg = mcde_sys_which(borgPaths);
|
||||
static char out[262144];
|
||||
const char *p;
|
||||
size_t n = 0;
|
||||
|
||||
(void) mcde_sys_backup_get(&b);
|
||||
if (!b.configured || borg == NULL)
|
||||
return 0;
|
||||
if (geteuid() != 0)
|
||||
return mcde_sys_fail(MCDE_SYS_EPERM, "Only the administrator "
|
||||
"can look into the backups.");
|
||||
if (Ready(&b) != MCDE_SYS_OK)
|
||||
return -1;
|
||||
{
|
||||
char *argv[] = { "/usr/bin/env", PASSCMD, (char *) borg, "list",
|
||||
"--json", b.repo, NULL };
|
||||
|
||||
if (mcde_sys_capture(argv, out, sizeof out) != MCDE_SYS_OK)
|
||||
return -1;
|
||||
}
|
||||
/* {"archives": [{"name": "...", ..., "time": "..."}, ...]} -- the
|
||||
* keys borg always gives; read in order, without a JSON library */
|
||||
for (p = strstr(out, "\"name\": \""); p != NULL && n < max;
|
||||
p = strstr(p, "\"name\": \"")) {
|
||||
const char *e, *t;
|
||||
|
||||
p += 9;
|
||||
if ((e = strchr(p, '"')) == NULL)
|
||||
break;
|
||||
snprintf(a[n].name, sizeof a[n].name, "%.*s", (int) (e - p), p);
|
||||
a[n].time[0] = '\0';
|
||||
if ((t = strstr(e, "\"time\": \"")) != NULL &&
|
||||
(t = t + 9) != NULL)
|
||||
snprintf(a[n].time, sizeof a[n].time, "%.19s", t);
|
||||
n++;
|
||||
p = e;
|
||||
}
|
||||
return (int) n;
|
||||
}
|
||||
|
||||
int
|
||||
mcde_sys_backup_restore(const char *archive, const char *path,
|
||||
const char *dest)
|
||||
{
|
||||
McdeSysBackup b;
|
||||
const char *borg = mcde_sys_which(borgPaths);
|
||||
char what[800], where[1100], cwd[1100];
|
||||
int r;
|
||||
|
||||
if (geteuid() != 0)
|
||||
return mcde_sys_fail(MCDE_SYS_EPERM, "Only the administrator "
|
||||
"can restore from the backups.");
|
||||
if (archive == NULL || path == NULL || path[0] != '/' ||
|
||||
dest == NULL || dest[0] != '/' || strstr(archive, "::") != NULL)
|
||||
return mcde_sys_fail(MCDE_SYS_EINVAL, "Nothing to restore.");
|
||||
(void) mcde_sys_backup_get(&b);
|
||||
if (!b.configured || borg == NULL)
|
||||
return mcde_sys_fail(MCDE_SYS_EINVAL, "No backup disk is set "
|
||||
"up.");
|
||||
if (Ready(&b) != MCDE_SYS_OK)
|
||||
return MCDE_SYS_EFAIL;
|
||||
snprintf(what, sizeof what, "%s::%s", b.repo, archive);
|
||||
/* borg extract writes below the current folder, the path without its
|
||||
* leading slash */
|
||||
snprintf(where, sizeof where, "%s", dest);
|
||||
(void) mkdir(where, 0700);
|
||||
if (getcwd(cwd, sizeof cwd) == NULL || chdir(where) != 0)
|
||||
return mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot go to %s: %s.",
|
||||
where, strerror(errno));
|
||||
{
|
||||
char *argv[] = { "/usr/bin/env", PASSCMD, (char *) borg,
|
||||
"extract", what, (char *) path + 1, NULL };
|
||||
|
||||
r = mcde_sys_run(argv);
|
||||
}
|
||||
(void) chdir(cwd);
|
||||
return r;
|
||||
}
|
||||
@@ -2511,6 +2511,7 @@ dt/bin/dsdm
|
||||
dt/bin/dtaction
|
||||
dt/bin/dtappgather
|
||||
dt/bin/dtappintegrate
|
||||
dt/bin/dtbackup
|
||||
dt/bin/dtbar
|
||||
dt/bin/dtbuilder
|
||||
dt/bin/dtcalc
|
||||
|
||||
@@ -6,7 +6,7 @@ SUBDIRS = backdrops icons dsdm dtmail dtpad dtfile dtwm dtlogin \
|
||||
dtappbuilder palettes dtappintegrate dtprintegrate dtconfig \
|
||||
dtcreate dtprintinfo fontaliases dtdspmsg ttsnoop dtimsstart \
|
||||
dtpdm dtsr dtpdmd dtinfo types tttypes util dtopen localized \
|
||||
dthelp dtdocbook dtlaunch dtbar dtsystemsettings dtdiskutility
|
||||
dthelp dtdocbook dtlaunch dtbar dtsystemsettings dtdiskutility dtbackup
|
||||
|
||||
install-data-hook:
|
||||
-if [ $(CDE_INSTALLATION_TOP)/bin != $(bindir) ];then \
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
MAINTAINERCLEANFILES = Makefile.in
|
||||
|
||||
# dtbackup: the backup from the command line and for cron (dtbackup.c);
|
||||
# the work is McdeSys's (lib/McdeSys/backup.c). No X.
|
||||
bin_PROGRAMS = dtbackup
|
||||
|
||||
dtbackup_SOURCES = dtbackup.c
|
||||
dtbackup_CPPFLAGS = -I$(top_srcdir)/include
|
||||
dtbackup_LDADD = $(LIBMCDESYS)
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* dtbackup.c -- MCDE: the backup from the command line, and for cron
|
||||
*
|
||||
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
|
||||
*
|
||||
* dtbackup run one backup now (root's crontab runs this every hour,
|
||||
* System Settings, Backup)
|
||||
* dtbackup status the backup disk, whether it runs, the last backup
|
||||
* dtbackup list the backups there are
|
||||
*
|
||||
* The work is McdeSys's (lib/McdeSys/backup.c, BorgBackup). No X: cron
|
||||
* has no display.
|
||||
*/
|
||||
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <Mcde/McdeSys.h>
|
||||
|
||||
static int
|
||||
Usage(void)
|
||||
{
|
||||
fprintf(stderr, "usage: dtbackup run | status | list\n");
|
||||
return 2;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
static char log[65536];
|
||||
static McdeSysBackupArchive a[2048];
|
||||
McdeSysBackup b;
|
||||
int n, i;
|
||||
|
||||
if (argc != 2)
|
||||
return Usage();
|
||||
if (strcmp(argv[1], "run") == 0) {
|
||||
if (mcde_sys_backup_run(log, sizeof log) != MCDE_SYS_OK) {
|
||||
fprintf(stderr, "dtbackup: %s\n", mcde_sys_error());
|
||||
fputs(log, stderr);
|
||||
return 1;
|
||||
}
|
||||
fputs(log, stdout);
|
||||
return 0;
|
||||
}
|
||||
if (strcmp(argv[1], "status") == 0) {
|
||||
char when[64] = "never";
|
||||
|
||||
(void) mcde_sys_backup_get(&b);
|
||||
if (b.last > 0) {
|
||||
time_t t = (time_t) b.last;
|
||||
|
||||
strftime(when, sizeof when, "%Y-%m-%d %H:%M",
|
||||
localtime(&t));
|
||||
}
|
||||
printf("disk: %s\nbackups: %s\nhourly: %s\nlast: %s "
|
||||
"(%s)\n", b.configured ? b.disk : "none",
|
||||
b.configured ? b.repo : "-", b.enabled ? "on" : "off", when,
|
||||
b.result[0] != '\0' ? b.result : "-");
|
||||
return 0;
|
||||
}
|
||||
if (strcmp(argv[1], "list") == 0) {
|
||||
n = mcde_sys_backup_archives(a, sizeof a / sizeof a[0]);
|
||||
if (n < 0) {
|
||||
fprintf(stderr, "dtbackup: %s\n", mcde_sys_error());
|
||||
return 1;
|
||||
}
|
||||
for (i = 0; i < n; i++)
|
||||
printf("%s %s\n", a[i].time, a[i].name);
|
||||
return 0;
|
||||
}
|
||||
return Usage();
|
||||
}
|
||||
Reference in new issue
Block a user