/* * dtbackup.c -- MCDE: the backup from the command line, and for cron * * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. * * 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 #include #include #include 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(); }