|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Receive in reverse direction must not throw an error if it can find an |
| 4 | +# earlier "sent" parent. In general, shows a backup+sync setup between two (or |
| 5 | +# more) PCs with an external drive. |
| 6 | + |
| 7 | +source "$TEST_TOP/common" |
| 8 | + |
| 9 | +check_prereq mkfs.btrfs |
| 10 | +check_prereq btrfs |
| 11 | +check_global_prereq dd |
| 12 | + |
| 13 | +declare -a roots |
| 14 | +i_pc1=1 |
| 15 | +# An external drive used to backup and carry profile. |
| 16 | +i_ext=2 |
| 17 | +i_pc2=3 |
| 18 | +roots[$i_pc1]="$TEST_MNT/pc1" |
| 19 | +roots[$i_ext]="$TEST_MNT/external" |
| 20 | +roots[$i_pc2]="$TEST_MNT/pc2" |
| 21 | + |
| 22 | +setup_root_helper |
| 23 | +mkdir -p "${roots[@]}" |
| 24 | +setup_loopdevs 3 |
| 25 | +prepare_loopdevs |
| 26 | +for i in `seq 3`; do |
| 27 | + TEST_DEV="${loopdevs[$i]}" |
| 28 | + TEST_MNT="${roots[$i]}" |
| 29 | + run_check_mkfs_test_dev |
| 30 | + run_check_mount_test_dev |
| 31 | + run_check $SUDO_HELPER mkdir -p "$TEST_MNT/.snapshots" |
| 32 | +done |
| 33 | + |
| 34 | +run_check_update_file() |
| 35 | +{ |
| 36 | + run_check $SUDO_HELPER cp --reflink "${roots[$1]}/profile/$2" "${roots[$1]}/profile/staging" |
| 37 | + run_check $SUDO_HELPER dd if=/dev/urandom conv=notrunc bs=4K count=4 seek=$3 "of=${roots[$1]}/profile/staging" |
| 38 | + run_check $SUDO_HELPER mv "${roots[$1]}/profile/staging" "${roots[$1]}/profile/$2" |
| 39 | +} |
| 40 | +run_check_copy_snapshot_with_diff() |
| 41 | +{ |
| 42 | + _mktemp_local send.data |
| 43 | + run_check $SUDO_HELPER "$TOP/btrfs" send -f send.data -p "${roots[$1]}/.snapshots/$2" "${roots[$1]}/.snapshots/$3" |
| 44 | + run_check $SUDO_HELPER "$TOP/btrfs" receive -f send.data "${roots[$4]}/.snapshots" |
| 45 | +} |
| 46 | +run_check_backup_profile() |
| 47 | +{ |
| 48 | + run_check $SUDO_HELPER "$TOP/btrfs" subvolume snapshot -r "${roots[$1]}/profile" "${roots[$1]}/.snapshots/$3" |
| 49 | + run_check_copy_snapshot_with_diff "$1" "$2" "$3" "$i_ext" |
| 50 | + # Don't keep old snapshot in pc |
| 51 | + run_check $SUDO_HELPER "$TOP/btrfs" subvolume delete "${roots[$1]}/.snapshots/$2" |
| 52 | +} |
| 53 | +run_check_restore_profile() |
| 54 | +{ |
| 55 | + run_check $SUDO_HELPER "$TOP/btrfs" subvolume snapshot "${roots[$1]}/.snapshots/$2" "${roots[$1]}/profile" |
| 56 | +} |
| 57 | +run_check_copy_fresh_backup_and_replace_profile() |
| 58 | +{ |
| 59 | + run_check_copy_snapshot_with_diff "$i_ext" "$2" "$3" "$1" |
| 60 | + # IRL, it would be a nice idea to make a backup snapshot before deleting. |
| 61 | + run_check $SUDO_HELPER "$TOP/btrfs" subvolume delete "${roots[$1]}/profile" |
| 62 | + run_check_restore_profile "$1" "$3" |
| 63 | + # Don't keep old snapshot in pc |
| 64 | + run_check $SUDO_HELPER "$TOP/btrfs" subvolume delete "${roots[$1]}/.snapshots/$2" |
| 65 | +} |
| 66 | + |
| 67 | + |
| 68 | +run_check $SUDO_HELPER "$TOP/btrfs" subvolume create "${roots[$i_pc1]}/profile" |
| 69 | +run_check $SUDO_HELPER dd if=/dev/urandom bs=4K count=16 "of=${roots[$i_pc1]}/profile/day1" |
| 70 | +run_check $SUDO_HELPER "$TOP/btrfs" subvolume snapshot -r "${roots[$i_pc1]}/profile" "${roots[$i_pc1]}/.snapshots/day1" |
| 71 | +_mktemp_local send.data |
| 72 | +run_check $SUDO_HELPER "$TOP/btrfs" send -f send.data "${roots[$i_pc1]}/.snapshots/day1" |
| 73 | +run_check $SUDO_HELPER "$TOP/btrfs" receive -f send.data "${roots[$i_ext]}/.snapshots" |
| 74 | + |
| 75 | +run_check_update_file "$i_pc1" day1 2 |
| 76 | +run_check_backup_profile "$i_pc1" day1 day2 |
| 77 | + |
| 78 | +_mktemp_local send.data |
| 79 | +run_check $SUDO_HELPER "$TOP/btrfs" send -f send.data "${roots[$i_ext]}/.snapshots/day2" |
| 80 | +run_check $SUDO_HELPER "$TOP/btrfs" receive -f send.data "${roots[$i_pc2]}/.snapshots" |
| 81 | +run_check_restore_profile "$i_pc2" day2 |
| 82 | +run_check_update_file "$i_pc2" day1 3 |
| 83 | +run_check_backup_profile "$i_pc2" day2 day3 |
| 84 | + |
| 85 | +run_check_update_file "$i_pc2" day1 4 |
| 86 | +run_check_backup_profile "$i_pc2" day3 day4 |
| 87 | + |
| 88 | +run_check_copy_fresh_backup_and_replace_profile "$i_pc1" day2 day4 |
| 89 | +run_check_update_file "$i_pc1" day1 5 |
| 90 | +run_check_backup_profile "$i_pc1" day4 day5 |
| 91 | + |
| 92 | +run_check_copy_fresh_backup_and_replace_profile "$i_pc2" day4 day5 |
| 93 | +run_check_update_file "$i_pc2" day1 6 |
| 94 | +run_check_backup_profile "$i_pc2" day5 day6 |
| 95 | + |
| 96 | +run_check_umount_test_dev "${loopdevs[@]}" |
| 97 | +rmdir "${roots[@]}" |
| 98 | +rm -f send.data |
| 99 | +cleanup_loopdevs |
0 commit comments