| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 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' |
| 23 |
backup_root = base / 'backup' |
| 24 |
mirror = backup_root / 'home' |
| 25 |
incr = backup_root / 'incremental' / 'home' |
| 26 |
idir0 = incr / '0' |
| 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) |
| 58 |
makepath(idir) |
| 59 |
makepath(mirror) |
| 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 |
|
| 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 |
|
| 76 |
|
| 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) |
| 81 |
(src / 'sub' / 'file_c').unlink() |
| 82 |
make_text_file(src / 'newfile', lines=10) |
| 83 |
|
| 84 |
stale = b"STALE-EXCLUDED-CONTENT\n" |
| 85 |
(mirror / 'stale.tmp').write_bytes(stale) |
| 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 |
|
| 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") |