diff --git a/README.md b/README.md index 58c2bb3..8e939a2 100644 --- a/README.md +++ b/README.md @@ -172,14 +172,21 @@ Log in once with `~/bin/quak`, as the user the cron job will run as: The session is saved in that user's data directory (see "The session file"). Then add the backup to that user's crontab with `crontab -e`. These two lines -back up the account to `~/photos-backup` at 03:30 every night and append all -output to `~/quak-backup.log`. Sunday's run adds `--verify`: +back up the account to `~/photos-backup` at 03:30 every night and send all +output to the system log with `logger`. Sunday's run adds `--verify`: ``` -30 3 * * 1-6 $HOME/bin/quak backup $HOME/photos-backup >> $HOME/quak-backup.log 2>&1 -30 3 * * 0 $HOME/bin/quak backup --verify $HOME/photos-backup >> $HOME/quak-backup.log 2>&1 +30 3 * * 1-6 $HOME/bin/quak backup $HOME/photos-backup 2>&1 | logger -t quak +30 3 * * 0 $HOME/bin/quak backup --verify $HOME/photos-backup 2>&1 | logger -t quak ``` +On Linux with systemd, `logger` writes to the journal, and `journalctl -t quak` +shows the runs. The system log adds the time to each line. + +To keep the output in a file instead, end each line with +`>> $HOME/quak-backup.log 2>&1` in place of the pipe. Each run's `Started at` +and `Finished at` lines then show when it ran. + A run with `--verify` does all a plain run does. It also hashes each original already in the backup and compares the result with the content hash. The content hash is the hash of the file's bytes that Ente recorded at upload. The run @@ -363,7 +370,9 @@ once, however many collections hold it. An original the cache already holds is copied from there instead. Progress goes to stderr: one line per original fetched, and a `FAILED` line for -each original or album that failed. The run ends with a summary of counts and, +each original or album that failed. The run starts with a `Started at` line +giving the local date and time, such as `Started at 2026-11-11 03:30:00 +0100`. +It ends with a summary: a `Finished at` line in the same form, the counts and, when any file failed, a `Failed files:` list of every failed file. With `--json`, the summary and the list are replaced by one JSON object on stdout. Its fields are: @@ -555,12 +564,12 @@ output, run `yarn --silent quak files --json` or `node dist/bin/quak.js files --json` instead. With `--json`, a command's JSON on stdout takes the place of its summary on -stderr. For `backup`, that is the `--- Backup complete ---` line, the counts and -the `Failed files:` list. For `list-missing-thumbnails`, it is -`No missing thumbnails found.` or ` file(s) with missing thumbnails:`. For -`fix-missing-thumbnails`, it is the `--- Done ---` line, the counts, the -`Skipped:` list and the `Failed files:` list. Every other line is logged as -without `--json`. +stderr. For `backup`, that is the `--- Backup complete ---` line, the +`Finished at` line, the counts and the `Failed files:` list. For +`list-missing-thumbnails`, it is `No missing thumbnails found.` or +` file(s) with missing thumbnails:`. For `fix-missing-thumbnails`, it is the +`--- Done ---` line, the counts, the `Skipped:` list and the `Failed files:` +list. Every other line is logged as without `--json`. ### Exit codes diff --git a/bin/quak.ts b/bin/quak.ts index cc37e37..2e3460c 100644 --- a/bin/quak.ts +++ b/bin/quak.ts @@ -81,6 +81,7 @@ const context = (): CliContext => ({ }), prompt: (message) => input({ message }), promptSecret: (message) => password({ message, mask: true }), + now: () => new Date(), }); const run = (command: Promise): Promise => diff --git a/src/cli-commands.ts b/src/cli-commands.ts index b123ce1..41c75a9 100644 --- a/src/cli-commands.ts +++ b/src/cli-commands.ts @@ -68,11 +68,29 @@ export interface CliContext { prompt: (message: string) => Promise; // Like `prompt`, but the answer is masked as it is typed. promptSecret: (message: string) => Promise; + // The current time. The CLI passes `() => new Date()`; tests pass fixed + // times. + now: () => Date; } const sessionPath = (ctx: CliContext): string => join(ctx.sessionDir, "session.json"); +const pad = (n: number): string => String(n).padStart(2, "0"); + +// `date` in local time with its offset from UTC, such as +// `2026-11-11 03:30:00 +0100`. +const localTime = (date: Date): string => { + const offset = -date.getTimezoneOffset(); + const sign = offset < 0 ? "-" : "+"; + const abs = Math.abs(offset); + return ( + `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ` + + `${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())} ` + + `${sign}${pad(Math.floor(abs / 60))}${pad(abs % 60)}` + ); +}; + // Write the session readable by its owner only, in a directory only its owner // can enter. export const saveSession = ( @@ -468,6 +486,7 @@ export const backupCommand = async ( if (!client) return 3; ctx.log("info", "Starting backup..."); + ctx.log("info", `Started at ${localTime(ctx.now())}`); // The lock is taken before the library opens and starts its refresh, so a // run refused while another backup of `dir` runs sends no request. let lock: BackupLock; @@ -504,6 +523,7 @@ export const backupCommand = async ( ctx.stdout.write(JSON.stringify(result, null, 2) + "\n"); } else { ctx.log("info", "\n--- Backup complete ---"); + ctx.log("info", `Finished at ${localTime(ctx.now())}`); ctx.log("info", ` Total files: ${result.totalFiles}`); ctx.log("info", ` Downloaded: ${result.downloaded}`); ctx.log("info", ` Skipped: ${result.skipped}`); diff --git a/test/cli/backup-end-to-end.test.ts b/test/cli/backup-end-to-end.test.ts index 6b2e432..e00b7a3 100644 --- a/test/cli/backup-end-to-end.test.ts +++ b/test/cli/backup-end-to-end.test.ts @@ -77,8 +77,17 @@ const PASSWORD = "backup-password"; const USER_ID = 42; const FRIEND_ID = 99; -// Noon on 1 March 2024 in the time zone of the machine running the test, the -// one quak dates save paths in, in microseconds, as Ente records times. +// quak prints the time a backup starts, and dates save paths, in local time. +// The zone is pinned here, before any fixture is dated, and restored after +// the last test. +const savedTZ = process.env.TZ; +process.env.TZ = "Europe/Berlin"; +// The time every backup's clock reads, 03:30 in Berlin. +const NOW = new Date(Date.UTC(2026, 10, 11, 2, 30)); +const STARTED = "Started at 2026-11-11 03:30:00 +0100\n"; + +// Noon on 1 March 2024 in local time, the one quak dates save paths in, in +// microseconds, as Ente records times. const TAKEN = new Date(2024, 2, 1, 12).getTime() * 1000; // The date folder every original is saved in. const DAY = "2024/2024-03/2024-03-01"; @@ -278,6 +287,8 @@ beforeAll(async () => { afterAll(() => { rmSync(root, { recursive: true, force: true }); + if (savedTZ === undefined) delete process.env.TZ; + else process.env.TZ = savedTZ; }); // Run `quak backup ` as `bin/quak.ts` runs it, sending its requests @@ -326,6 +337,7 @@ const backup = async ( login: prompted, prompt: prompted, promptSecret: prompted, + now: () => NOW, }; const code = await new Promise((resolve) => { void run(backupCommand(ctx, dir, opts), stdout, stderr, log, resolve); @@ -698,6 +710,7 @@ describe("quak backup", () => { expect(code).toBe(3); expect(stderr).toBe( "Starting backup...\n" + + STARTED + "Refreshing library...\n" + `quak: the saved session is no longer valid; run "quak login"\n`, ); @@ -725,6 +738,7 @@ describe("quak backup", () => { expect(code).toBe(1); expect(stderr).toBe( "Starting backup...\n" + + STARTED + "Refreshing library...\n" + `quak: ${reason}\n`, ); @@ -1117,6 +1131,7 @@ describe("a backup whose lock is lost", () => { expect(code).toBe(1); expect(stderr).toBe( "Starting backup...\n" + + STARTED + "Fetched the files of 1 of 3 collections\n" + "Fetched the files of 2 of 3 collections\n" + "Fetched the files of 3 of 3 collections\n" + diff --git a/test/cli/backup-undecryptable.test.ts b/test/cli/backup-undecryptable.test.ts index 665becb..bfb9301 100644 --- a/test/cli/backup-undecryptable.test.ts +++ b/test/cli/backup-undecryptable.test.ts @@ -319,6 +319,7 @@ const context = (ente: FakeEnte): CliContext => { promptSecret: async () => { throw new Error("prompt not expected"); }, + now: () => new Date(Date.UTC(2026, 10, 11, 2, 30)), }; }; diff --git a/test/cli/commands.test.ts b/test/cli/commands.test.ts index 39f3042..8c6fd5d 100644 --- a/test/cli/commands.test.ts +++ b/test/cli/commands.test.ts @@ -33,6 +33,7 @@ import { vi, beforeAll, beforeEach, + afterAll, afterEach, } from "vitest"; @@ -194,6 +195,12 @@ const FETCHED_BOTH_ALBUMS = "Fetched the files of 1 of 2 collections\n" + "Fetched the files of 2 of 2 collections\n"; +// The time every context's clock reads. `backup` prints it in local time, and +// the tests pin the zone to Europe/Berlin, where it is 03:30. +const NOW = new Date(Date.UTC(2026, 10, 11, 2, 30)); +const STARTED = "Started at 2026-11-11 03:30:00 +0100\n"; +const FINISHED = "Finished at 2026-11-11 03:30:00 +0100\n"; + // Collects everything written to it. class Output { text = ""; @@ -223,11 +230,18 @@ const context = (client: Client | null = fakeClient()): CliContext => ({ promptSecret: async () => { throw new Error("prompt not expected"); }, + now: () => NOW, }); +const savedTZ = process.env.TZ; beforeAll(async () => { + process.env.TZ = "Europe/Berlin"; await init(); }); +afterAll(() => { + if (savedTZ === undefined) delete process.env.TZ; + else process.env.TZ = savedTZ; +}); beforeEach(() => { root = mkdtempSync(join(tmpdir(), "quak-cli-test-")); @@ -959,6 +973,7 @@ describe("backup", () => { expect(await backupCommand(context(), dir, {})).toBe(0); expect(stderr.text).toContain( "\n--- Backup complete ---\n" + + FINISHED + " Total files: 3\n" + " Downloaded: 3\n" + " Skipped: 0\n" + @@ -968,6 +983,43 @@ describe("backup", () => { expect(existsSync(join(dir, "backup.lock"))).toBe(false); }); + // A log of many nights' runs shows when each started and finished, in + // local time with the offset from UTC. + it("prints the local date and time the run started and finished", async () => { + const start = new Date(Date.UTC(2026, 10, 11, 2, 30, 5)); + const end = new Date(Date.UTC(2026, 10, 11, 3, 4, 9)); + const zones = [ + { + zone: "Europe/Berlin", + started: "2026-11-11 03:30:05 +0100", + finished: "2026-11-11 04:04:09 +0100", + }, + { + zone: "Asia/Kolkata", + started: "2026-11-11 08:00:05 +0530", + finished: "2026-11-11 08:34:09 +0530", + }, + { + zone: "America/New_York", + started: "2026-11-10 21:30:05 -0500", + finished: "2026-11-10 22:04:09 -0500", + }, + ]; + for (const { zone, started, finished } of zones) { + process.env.TZ = zone; + stderr.text = ""; + const times = [start, end]; + const ctx = { ...context(), now: () => times.shift()! }; + expect(await backupCommand(ctx, join(root, zone), {})).toBe(0); + const begin = `Starting backup...\nStarted at ${started}\n`; + expect(stderr.text.slice(0, begin.length)).toBe(begin); + expect(stderr.text).toContain( + `--- Backup complete ---\nFinished at ${finished}\n`, + ); + } + process.env.TZ = "Europe/Berlin"; + }); + // The backup opens its library with the precache off: it fetches the // originals it needs into the backup, and must not also fetch every // thumbnail in the account, or keep originals, in the per-user cache. @@ -1049,6 +1101,7 @@ describe("backup", () => { it("counts on stderr each album whose files its first refresh fetched, with --json too", async () => { const start = "Starting backup...\n" + + STARTED + FETCHED_BOTH_ALBUMS + "Refreshing library...\n"; for (const json of [false, true]) { @@ -1168,7 +1221,9 @@ describe("backup", () => { const { code, runText } = await backupThroughRun(context(client), dir); expect(code).toBe(1); expect(runText).toBe("quak: HTTP 503 from server\n"); - expect(stderr.text).toBe("Starting backup...\nRefreshing library...\n"); + expect(stderr.text).toBe( + "Starting backup...\n" + STARTED + "Refreshing library...\n", + ); expect(readdirSync(dir)).toEqual([]); }); @@ -1199,7 +1254,9 @@ describe("backup", () => { expect(runText).toBe( `quak: the saved session is no longer valid; run "quak login"\n`, ); - expect(stderr.text).toBe("Starting backup...\nRefreshing library...\n"); + expect(stderr.text).toBe( + "Starting backup...\n" + STARTED + "Refreshing library...\n", + ); expect(readdirSync(dir)).toEqual([]); }); @@ -1227,7 +1284,9 @@ describe("backup", () => { expect(await backupCommand(context(client), dir, {})).toBe(2); expect(requests).toBe(0); expect(stderr.text).toBe( - `Starting backup...\nquak: another backup of ${dir} is running\n`, + "Starting backup...\n" + + STARTED + + `quak: another backup of ${dir} is running\n`, ); expect(readdirSync(dir)).toEqual(["backup.lock"]); // Opening the library would have made its cache directory.