Debian Codegraph @

testsuite/backup-incremental_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Normal-operation test for --backup / --backup-dir, modelling the maintainer's
3 # long-standing incremental rsync backup script. Each run mirrors SRC -> MIRROR
4 # with -a --sparse --exclude-from=FILE --delete --delete-excluded and
5 # --backup --backup-dir=IDIR, where IDIR is a rotating incremental dir (wiped at
6 # the start of each run; two slots). Effect: the mirror is brought back in sync
7 # with the source, and every file about to be overwritten or deleted (including
8 # by --delete-excluded) is first relocated into that run's IDIR at its relative
9 # path. Local transfer, runs unprivileged.
10
11 import os
12
13 from rsyncfns import (
14 SCRATCHDIR, assert_not_exists, assert_trees_equal, make_data_file,
15 make_text_file, makepath, rmtree, run_rsync, test_fail,
16 )
17
18 base = SCRATCHDIR / 'backup-incremental'
19 rmtree(base)
20 base.mkdir()
21
22 src = base / 'src' # the "home server" data
23 backup_root = base / 'backup' # BACKUP_DIR
24 mirror = backup_root / 'home' # BACKUP_DIR/$name
25 incr = backup_root / 'incremental' / 'home' # BACKUP_DIR/incremental/$name
26 idir0 = incr / '0' # the two rotating slots
27 idir1 = incr / '1'
28 exclude_file = base / 'backup_excludes'
29 exclude_file.write_text("*.tmp\n")
30
31
32 def make_sparse(path, size=128 * 1024):
33 """A sparse file: a head, a hole, then a tail."""
34 with open(path, 'wb') as f:
35 f.write(b'SPARSE-HEAD\n')
36 f.seek(size - 4)
37 f.write(b'END\n')
38
39
40 def build_src():
41 rmtree(src)
42 makepath(src / 'sub')
43 make_text_file(src / 'file_a', lines=40)
44 make_data_file(src / 'file_b', 8192)
45 make_text_file(src / 'sub' / 'file_c', lines=30)
46 make_data_file(src / 'sub' / 'file_d', 8192)
47 os.symlink('file_a', src / 'link')
48 make_sparse(src / 'sparse')
49
50
51 def is_empty(d):
52 return not any(d.iterdir())
53
54
55 def backup_run(idir):
56 """One incremental backup, mirroring the script's backup_one()."""
57 rmtree(idir) # script: rm -rf $IDIR
58 makepath(idir) # script: mkdir -p $IDIR
59 makepath(mirror) # script: mkdir -p $BACKUP_DIR/$name
60 run_rsync('-a', '--sparse', f'--exclude-from={exclude_file}',
61 '--delete', '--delete-excluded',
62 '--backup', f'--backup-dir={idir}',
63 f'{src}/', f'{mirror}/')
64
65
66 # --- Phase 1: initial full backup into slot 0 -------------------------------
67 build_src()
68 backup_run(idir0)
69 assert_trees_equal(src, mirror, label='phase1 mirror')
70 if not is_empty(idir0):
71 test_fail(f"phase1: backup dir {idir0} is not empty after the first run "
72 f"(nothing should have been backed up): "
73 f"{[p.name for p in idir0.iterdir()]}")
74
75 # --- Phase 2: mutate the source, second backup into slot 1 ------------------
76 # Capture the about-to-change mirror contents (what --backup must preserve).
77 old_b = (mirror / 'file_b').read_bytes()
78 old_c = (mirror / 'sub' / 'file_c').read_bytes()
79
80 make_data_file(src / 'file_b', 9000) # overwrite (content changes)
81 (src / 'sub' / 'file_c').unlink() # delete from source -> --delete
82 make_text_file(src / 'newfile', lines=10) # brand-new file
83 # file_a is left unchanged on purpose.
84 stale = b"STALE-EXCLUDED-CONTENT\n"
85 (mirror / 'stale.tmp').write_bytes(stale) # excluded file already in mirror
86
87 backup_run(idir1)
88
89 assert_trees_equal(src, mirror, label='phase2 mirror')
90
91 if (idir1 / 'file_b').read_bytes() != old_b:
92 test_fail("phase2: overwritten file_b was not backed up with its old content "
93 f"into {idir1}/file_b")
94 if (idir1 / 'sub' / 'file_c').read_bytes() != old_c:
95 test_fail("phase2: deleted sub/file_c was not backed up at its relative path "
96 f"into {idir1}/sub/file_c")
97 if not (idir1 / 'stale.tmp').exists() or (idir1 / 'stale.tmp').read_bytes() != stale:
98 test_fail("phase2: --delete-excluded removal of stale.tmp was not captured by "
99 f"--backup into {idir1}/stale.tmp")
100 assert_not_exists(idir1 / 'newfile', label='phase2: new file must not be backed up')
101 assert_not_exists(idir1 / 'file_a', label='phase2: unchanged file must not be backed up')
102 if not is_empty(idir0):
103 test_fail(f"phase2: the previous slot {idir0} was disturbed by this run "
104 "(each run's backup dir must be independent)")
105
106 # --- Phase 3: no-op third run, rotating back to slot 0 ----------------------
107 backup_run(idir0)
108 assert_trees_equal(src, mirror, label='phase3 mirror')
109 if not is_empty(idir0):
110 test_fail(f"phase3: a no-change run backed something up into {idir0}: "
111 f"{[p.name for p in idir0.iterdir()]}")
112
113 print("incremental --backup/--backup-dir round-trip: mirror tracks source and "
114 "each run's backup dir captures exactly the overwritten/deleted files")