diff --git a/deploy/deploy.sh b/deploy/deploy.sh index 4547c723..34640659 100644 --- a/deploy/deploy.sh +++ b/deploy/deploy.sh @@ -517,14 +517,12 @@ backup_recovery_volumes() { apply_backup_retention() { local daily_days="${BACKUP_DAILY_RETENTION_DAYS:-30}" local monthly_count="${BACKUP_MONTHLY_RETENTION_COUNT:-12}" - local timezone="${BACKUP_TIMEZONE:-Europe/Oslo}" if [[ ! "$daily_days" =~ ^[1-9][0-9]*$ ]] || [[ ! "$monthly_count" =~ ^[1-9][0-9]*$ ]]; then echo "Backup retention must use positive integer day/count values." return 1 fi - local month monthly_root monthly_dir complete_tmp - month="$(TZ="$timezone" date +%Y-%m)" + local month="$1" monthly_root monthly_dir complete_tmp monthly_root="$BACKUP_DIR/monthly" monthly_dir="$monthly_root/$month" if ! mkdir -p "$monthly_root" || ! chmod 700 "$monthly_root"; then @@ -605,12 +603,14 @@ upload_current_backup_set() { fi done - local month="$(TZ="${BACKUP_TIMEZONE:-Europe/Oslo}" date +%Y-%m)" - if [ -d "$BACKUP_DIR/monthly/$month" ]; then - if ! rclone copy "$BACKUP_DIR/monthly/$month" "$remote/monthly/$month" --immutable; then - echo "Monthly off-host backup upload failed. Aborting deploy." - return 1 - fi + local month="$1" + if [ ! -f "$BACKUP_DIR/monthly/$month/.complete" ]; then + echo "Completed monthly recovery set is missing. Aborting deploy." + return 1 + fi + if ! rclone copy "$BACKUP_DIR/monthly/$month" "$remote/monthly/$month" --immutable; then + echo "Monthly off-host backup upload failed. Aborting deploy." + return 1 fi echo "Current recovery set copied to configured off-host remote." } @@ -638,7 +638,11 @@ if ! backup_recovery_volumes; then exit 1 fi -if ! apply_backup_retention || ! upload_current_backup_set; then +if ! backup_month="$(TZ="${BACKUP_TIMEZONE:-Europe/Oslo}" date +%Y-%m)"; then + echo "Could not determine the monthly recovery period. Aborting deploy." + exit 1 +fi +if ! apply_backup_retention "$backup_month" || ! upload_current_backup_set "$backup_month"; then exit 1 fi diff --git a/docs/release-readiness/11_BACKUP_RESTORE_ROLLBACK.md b/docs/release-readiness/11_BACKUP_RESTORE_ROLLBACK.md index 377b3668..215dcf1b 100644 --- a/docs/release-readiness/11_BACKUP_RESTORE_ROLLBACK.md +++ b/docs/release-readiness/11_BACKUP_RESTORE_ROLLBACK.md @@ -1,5 +1,13 @@ # Recovery implementation review and rehearsal plan +## RR-13 monthly recovery period across retention and upload + +19 November 2026, isolated release branch from `67b9b675194d3c26432a4da156f680d3368f767e`. A backup that crossed the local month boundary between retention and upload could omit the retained monthly set while reporting success. A failed calendar command could select the monthly root as the destination. A monthly set that disappeared before transfer could also produce a success message. All three cases were reproduced with synthetic files and a recording transfer stub. + +The gate now resolves the month once in the configured backup timezone, checks the command status and passes that same month to both operations. Upload requires the retained snapshot's completion marker. A previously completed snapshot for the month retains its existing stamp and contents. + +Final verification passed 33 backup methods (13.456 seconds), six rollback methods (0.380 seconds) and Bash syntax. One intermediate test run misplaced a pre-existing success assertion in a failure case. The assertion was restored to the successful upload case before the final run. The upload harness executes the real retention and upload functions over temporary files with pruning and network transfer stubbed. No off-host copy, remote encryption, recovery rehearsal or deployment was performed. Reverting this change affects script behaviour only. + ## RR-17 SQLite archive member format 18 November2026, isolated source10abd22f from2fb10f7b. Name-only verification accepted ten synthetic link/type/content/path cases that could not establish database bytes. Final29 backup methods and Bash syntax pass with the streaming Python standard-library path/type/signature verifier. No member is extracted. Supported root/dot/nested paths remain; Python3 was already a mandatory deployment dependency. Final native logs and precise intermediate fixture failures are retained in [format evidence](evidence/sqlite-archive-format/README.md). Earlier26/22/21 counts overlap29. This is not SQLite integrity, authoritative path selection, WAL/key/tombstone consistency, restore/RPO/RTO, actual container or production qualification. Original release worktree and A3 remain separate. diff --git a/scripts/test-deploy-backup.py b/scripts/test-deploy-backup.py index d4b0cf04..7868bdb2 100644 --- a/scripts/test-deploy-backup.py +++ b/scripts/test-deploy-backup.py @@ -16,6 +16,12 @@ ROOT = Path(__file__).resolve().parents[1] BASH = 'C:/Program Files/Git/bin/bash.exe' if os.name == 'nt' else shutil.which('bash') +def retention_and_upload_gate(source): + start = source.index('if ! backup_recovery_volumes; then') + after_recovery = source.index('\nfi\n', start) + 4 + return source[after_recovery:source.index('\nif [ "${DEPLOY_BACKUP_ONLY:-false}"', after_recovery)] + + class BackupConnectionTests(unittest.TestCase): def backup(self, connection): source = (ROOT / 'deploy/deploy.sh').read_text(encoding='utf-8') @@ -92,30 +98,35 @@ class BackupConnectionTests(unittest.TestCase): class BackupUploadTests(unittest.TestCase): def upload(self, fail_operation=''): source = (ROOT / 'deploy/deploy.sh').read_text(encoding='utf-8') - start = source.index('upload_current_backup_set() {') - function = source[start:source.index('\n}\n', start) + 3] - gate_start = source.index('if ! apply_backup_retention || ! upload_current_backup_set; then') - gate = source[gate_start:source.index('\nfi\n', gate_start) + 4] + functions = [] + for name in ['apply_backup_retention', 'upload_current_backup_set']: + start = source.index(name + '() {') + functions.append(source[start:source.index('\n}\n', start) + 3]) with tempfile.TemporaryDirectory() as temporary: folder = Path(temporary) for filename in ['jobtracker-db-synthetic.sql.gz', 'jobtracker-files-synthetic.tar.gz', 'jobtracker-db-older.sql.gz']: (folder / filename).touch() - (folder / 'monthly' / '2099-01').mkdir(parents=True) script = '\n'.join([ 'set -euo pipefail', 'BACKUP_DIR=' + shlex.quote(folder.as_posix()), 'BACKUP_STAMP=synthetic', 'BACKUP_RCLONE_REMOTE=fixture:encrypted/', 'FAIL_OPERATION=' + shlex.quote(fail_operation), - 'date() { echo 2099-01; }', - 'apply_backup_retention() { return 0; }', + 'date() {', + ' if [ "$FAIL_OPERATION" = clock ]; then return 2; fi;', + ' if [ "$FAIL_OPERATION" = rollover ] && [ -f "$BACKUP_DIR/tick" ]; then echo 2099-02;', + ' else touch "$BACKUP_DIR/tick"; echo 2099-01; fi;', + '}', + 'find() { return 0; }', 'rclone() {', ' printf "UPLOAD %s %s %s\\n" "$1" "$3" "$4";', ' if [ "$1" = "$FAIL_OPERATION" ]; then return 23; fi;', + ' if [ "$FAIL_OPERATION" = missing-monthly ] && [ -d "$BACKUP_DIR/monthly/2099-01" ]; then', + ' mv "$BACKUP_DIR/monthly/2099-01" "$BACKUP_DIR/unavailable-snapshot"; fi;', '}', - function, - gate, + *functions, + retention_and_upload_gate(source), 'echo "DEPLOY WOULD CONTINUE"', ]) result = subprocess.run([BASH, '--noprofile', '--norc'], input=script, @@ -137,6 +148,25 @@ class BackupUploadTests(unittest.TestCase): self.assertIn('DEPLOY WOULD CONTINUE', result.stdout) self.assertIn('Current recovery set copied to configured off-host remote.', result.stdout) + def test_month_rollover_uploads_the_retained_snapshot(self): + result, calls = self.upload('rollover') + self.assertEqual(0, result.returncode, result.stdout + result.stderr) + self.assertIn('UPLOAD copy fixture:encrypted/monthly/2099-01 --immutable', calls) + + def test_clock_failure_blocks_retention_and_upload(self): + result, calls = self.upload('clock') + self.assertNotEqual(0, result.returncode, result.stdout + result.stderr) + self.assertEqual([], calls) + self.assertNotIn('Monthly recovery set retained', result.stdout) + self.assertNotIn('DEPLOY WOULD CONTINUE', result.stdout) + + def test_missing_monthly_snapshot_stops_the_gate(self): + result, calls = self.upload('missing-monthly') + self.assertNotEqual(0, result.returncode, result.stdout + result.stderr) + self.assertEqual(2, len(calls)) + self.assertNotIn('Current recovery set copied', result.stdout) + self.assertNotIn('DEPLOY WOULD CONTINUE', result.stdout) + def test_daily_copy_failure_stops_before_remaining_files_and_monthly_copy(self): result, calls = self.upload('copyto') self.assertEqual(1, result.returncode, result.stdout + result.stderr) @@ -162,8 +192,7 @@ class BackupRetentionTests(unittest.TestCase): source = (ROOT / 'deploy/deploy.sh').read_text(encoding='utf-8') start = source.index('apply_backup_retention() {') function = source[start:source.index('\n}\n', start) + 3] - gate_start = source.index('if ! apply_backup_retention || ! upload_current_backup_set; then') - gate = source[gate_start:source.index('\nfi\n', gate_start) + 4] + gate = retention_and_upload_gate(source) fixtures = {'jobtracker-db-synthetic.sql.gz': b'database fixture', 'jobtracker-files-synthetic.tar.gz': b'files fixture'} with tempfile.TemporaryDirectory() as temporary: