Restore canary file from backup

Why Link to heading

To ensure you can successfully restore a file from a tar backup. A sha256sum only checks the integrity of the created archive, not it’s restorability.

Prerequisites Link to heading

  1. Create a playground:
mkdir archive-test
cd archive-test
  1. Create some content:
mkdir content
echo content1 >> content/file1
echo content2 >> content/file2
echo content3 >> content/file3
  1. Back up your content:
tar -cf backup1.tar content

Canary Link to heading

  1. Generate a random id:
export CANARY_ID=$(tr -dc "a-zA-Z0-9" < /dev/urandom | head -c 32)
  1. Save that id to a file:
echo $CANARY_ID > canary
  1. Add it to your archive:
tar -rf backup1.tar canary
  1. Rename your archive to contain the id:
mv backup1.tar backup1."$CANARY_ID".tar
  1. Remove the unarchived canary file:
rm -f canary
  1. Optionally compress/encrypt/checksum the archive with gzip, gpg, and sha256sum.

Restore Link to heading

  1. Extract the canary id:
export RESTORE_ID=$(ls backup1.$CANARY_ID.tar | cut -d '.' -f 2)
  1. Extract the canary file:
tar -xf backup1."$CANARY_ID".tar canary
  1. Compare the two values:
[ "$CANARY_ID" == "$RESTORE_ID" ] && echo "Match!" || echo "Fail!" && false

Cleanup Link to heading

Don’t forget to remove your test files after playing with this:

cd ..
rm -rf archive-test